Get integer difference of sensor state between hours x and y

I have a sensor on the MELCloud integration that reports total kWh used by a airconditioner. The sensor only counts up. I would like the create a template/whatever sensor that displays the kWh used in the last 24hrs starting at 6 in the morning.
I have all my sensor values in mysql.

Have you looked at the utility meter integration?