The only problem is here.
After HA restart the monitored sensor is not actually updated - but it’s last_changed says that it was changed recently (because HA restarted).
So the binary_sensor flag is not set on startup.
My automation is triggered when the sensor is really changed.
The flag is restored after HA restart and may be toggled by the automation.
If the specified timeout is 5 minutes, it may not be a problem - the flag will be set in 5 minutes after HA restart.
If the timeout is bigger, that it may be critical.
if you want this to persist over a startup, store the last changed in an input_datetime or in a MQTT sensor that’s restored on startup. Otherwise, what you’re describing is how everything in HA works. Last changed for all entities are set to the startup time of HA upon restart.
Or, add a ‘just started’ template binary_sensor that’s on for the first 6 minutes of a reboot and use that as a condition in your automation or the other binary sensor.
Let’s assume I have 10 monitored sensors.
Here is my method.
I can use my automation from the 1st post which sets an input_boolean helper - 10 automations.
10 input_boolean helpers (toggled by that automation).
10 binary_sensor alert flags - states are equal to those input_boolean helpers.
Hard to say what is more complex/cumbersome.
Probably my automation is more complex… But it seems to work)))
Could you have a look at that automation?
I am not getting the idea , need some more time to understand.
One more thing.
Since - as you reminded - the problem “all sensors update last_changed on HA startup” - probably I will need a common mechanism to “restore a real last_changed” after HA restart for many sensors (not only mentioned here).
no, but it makes it so you don’t need helpers as you can just dynamically create anything on the fly and have it load into your system and persist restarts.
I have scripts that do this, I think @123 has a “copy/paste” solution that might work right out of the box without much editing. All mine are locked into a rigid plan that would require heavy edits.
123 sorry for the @ but you seem to have a good solution for this and I could not find it.
Fedot’s solution is basically what I have suggested to others but using datetime objects in the subtraction instead of timestamps (slightly more compact template).
What you might be thinking of is this automation that periodically checks for stale sensors.
The requirement was to check every 4 hours for any sensor or binary_sensor that hasn’t been updated in several hours. It uses a Time Pattern Trigger in order to control the polling interval (to avoid checking every minute as in a template employing now()). It can be easily adapted to check every few minutes and could be made to check on startup as well (add the appropriate homeassistant startup trigger).