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.
- trigger:
- platform: state
entity_id:
- binary_sensor.sump_pump_1_pumpout_event
from: "off"
to: "on"
sensor:
- name: "sump_pump_1_cycle_interval_now"
state: >
{{ ((as_timestamp(now()) - as_timestamp(states.binary_sensor.sump_pump_1_pumpout_event.last_changed)) | float(1) ) |int|timestamp_custom('%-M min %-S sec') }}
attributes:
friendly_name: Sump Pump 1 Cycle Interval
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 @finity put 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.