A pump is in a “well” which is small, and have not too much water in it so i have to pump the water out to a remote storage tank.
Two measurements must be made, one is the flow checking which tells if the pump sucks water or air (this is the level check, because there is no space to put anything into this well except the pump), and an ultrasonic sensor which controls if the storage tank is filled or not.
If the flow rate is low, it must be turned off for a specified time (till the well restores its water level), thats why i need a timer.
Without this condition its working for years without any serious problems, but sometimes it got crazy because:
if i turn on the pump, it takes 5-10 secs to reach the good, measurable flow rate.
If i turn off the pump, the water flows back, so the sensor shows for 10 secs lower flow rate.
So after the switch is switched, the next 10 seconds contains not real values, so in this case the trigger must be skipped.
The pump in the well can be started manually, and automatically, so the regular switch check seems to be the most elegant option. (and its home assistant reboot proof)
It is probably easiest to have an input_datetime which is set to the value when the switch was last turned on (which is also the officially recommended way of knowing a last-changed that survives reboots, Automation Trigger - Home Assistant)
Create an input_datetime and an automation that sets its value to now() when the switch is turned on
Example:
- alias: Pump is turned on
trigger:
platform: state
entity_id: switch.wellpump
to: 'on'
action:
- service: input_datetime.set_datetime
target:
entity_id: input_datetime.wellpump_started
data:
datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
To make it even easier you can create a templated sensor that calculates the number of seconds the pump has been on (difference in seconds between now() and the value in your input_datetime, and in case your date is inproperly formatted it returns 0 (now() - now())):
Using now() will cause templates to be refreshed at the start of every new minute.
This means that you might see values like 5 seconds, then 65 seconds, then 125 seconds.
There are other ways of evaluating time as well if you want it more finegrained, like for example using a templated sensor based on date/time (Time & Date - Home Assistant)
He mentioned being reboot proof as well, but I agree that for is definitely the most elegant solution if it is not a requirement. The odds of restarting HA during the 10 seconds the pump is starting up are pretty slim
Wouldn’t the pump be unavailable at startup then go to on → for: 10 seconds?
But I still believe there are something strange with the normal turn off or the turn on automations given that he needs all these conditions and feel the need for a time pattern.
I did some tests on my own switches in a running HA and it never seems to go to unavailable when starting up (HA 2022.7), if they were on they kept the state on during the entire reboot. It also seems that any running for will be stopped during reboot (like in the case here Automation recovery after HA restart - #2 by 123)
But I definitely agree that your first suggestion is the best (not considering reboots), something like