Home Assistant/HEMS help

Is there an easy way to create / combine custom variables in Home Assistant? eg with the Octopus Agile tariff is there a way to create a new variable which provides the average rate for the previous 3days? This could then be used to set a customised threshold for turning on/off say a Shelly switched load? At the moment I have set up a static threshold for an Automation based on Agile, but if this could be further refined to take advantage of dip in elec prices after (say) a longer run of higher prices that might be useful.
See (for current static version of this automation)