I’m trying to calculate the time between the last two pumpouts of my sump pit. I want this to calculate when the pumpout occurs, so it updates every pumpout.
I have a binary sensor that is on when the pump is pumping and off when it isn’t. I wrote the following template sensor (which took me forever to figure out!), but unfortunately this gives a time of almost zero as the last_changed is updated as the sensor is triggered.
I need some way to store the last changed time and then find the difference from the new last changed time. In Node Red it has old.state and new.state, but I can’t figure out the javascript in the function node. This solution is to set up an SQL database, and I tried to do that but couldn’t figure out the basic parameters from the docs.
In plain English, or psuedo-code as @finityput it here, I want the trigger to cause HA to store the last_changed time as time_2 and store now() as time_1. Then subtract the two and return the result. Not sure how to do this with a template sensor and how to avoid having the last_changed simply always be now() since the thing is triggered by the change in the first place!
I’ve searched around and found similar questions, but the setups seem to be different enough that I can’t make solutions work for my setup. So I’m officially stuck and would appreciate any help.
The following Trigger-based Template Sensor records the last 5 time intervals (where an interval represents the time when the pump wasn’t running) in its cycles attribute as a list in reverse chronological order (most recent is first).
The sensor’s state is simply the most recent time when the pump turned on. The information you want is in the cycles attribute (which is unaffected by a restart).
There was a missing > after cycles but after I added that (took me forever because I initially tried < and it didn’t work!) there were no errors and allowed me to restart.
I’ve tested it using Developer Tools and it is giving me the correct time between on events. But the entity card shows me the literal times as well. The only Attributes are Friendly name and Cycles. How do I snag just the duration value? I want to show the duration time as (%-M min %-S sec) when it’s all said and done, like this:
I’m thinking there’s a way to set up an attribute Duration? I messed around with it a little bit, but I am so unfamiliar with the syntax I couldn’t get it to work and guess-and-test would take forever.
Also, is this line still needed since I’m not dealing with ranking the durations or anything? {{ ((new + current) | sort(reverse=true, attribute='duration'))[:5] }}
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.