Calculate how long time a switch has been turned on during a specific period

Reading the documentation about persistency I couldn’t find an easier approach to calculate how long time during a certain period a switch has been turned on (or any item in a particular state).
If the history contains state changes of the switch, there must be a way to find out the duration when the switch is turned on, and sum the durations during a period. Has anybody done something similar?

The use-case: I’d like to get an overview of run time of compressor in my heat pump during the period when electricity is expensive.

Any suggestion is appreciated!

This should give you a starting point:

1 Like

There’s getAllStatesBetween (and Since). You can write code to sum up the durations between on and off.

1 Like

Thank you both! I found using averageSince/between seems to work well and it’s very simple to calculate things like this

1 Like