Hi:
Trying to automate heating and would like to ask if there is a way to get timestamp of previous state of a switch. I’m aware that the api allows to get timestamp of last state changed, however I’d like to get timestamp of the previous state (the state before last state).
Use-case:
When the heating switch is changed to OFF, I’d like to see when it was turned ON, and calculate the heating hours and temperature raise between ON and OFF.
Hi @jimtng :
Thank you! I thought it was a simple thing but actually couldn’t make it to work.
I have tried your suggestion of using an old enough time for getAllStatesSince, unfortunately this returns all updates (not changes), and I’m not sure how to go backwards to find a change in the history:
2024-11-23 15:07:20.711 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'SmartHeating-6' failed: rrd4j does not allow querys with
out a begin date, unless order is descending and a single value is requested in SmartHeating
Can you elabrate on your 2nd point? I’m not sure I understand it?
Oh I didn’t think about the same state being persisted too. In that case you’d need to check for the state changes by looping through the array starting from the end.
While this can be done through persistence, as shown here, it’s going to require some extra work if you are using rrd4j. Since you are using rrd4j, you might consider a non-persitence approach.
For example, you can keep an extra couple of Items, one to store the time of the current state and one to store the time of the previous state. When the Item changes, move the state of the current timestamp to the previous timestamp and record now with the current time. Soemthing like this (Rules DSL since that appears to be what you are using):
rule "Record timestamps"
when
Item MyItem changed
then
MyItem_PreviousTimestamp.postUpdate(MyItem.CurrentTimestamp.state)
MyItem_CurrentTimestamp.postUpdate(now)
end