Sensor that shows value of energy produced for the past hour

I’m trying to make a sensor that shows me the cost of my energy demand. I have a high and low tariff, for which I already have a working code.
Now I need to create a sensor that shows me the amount of energy used the last hour (so the value of that sensor only changes every full hour).
I would then multiply the tariff-sensor with the “energy of the past hour” sensor to get the cost of my energy consumption of the past hour.
I tried to find a fitting helper, but nothing really does what I want.
Has anyone an idea of how to solve that?