I need some help with an automation. I want to create an alert for me when my watersensor is offline for more than 24h.
Background: I have a DCH161 from DLink. There is a custom integration for it, but when I use it I lose the DLinik integration warning. I see that the device report itselft ony a day to the daylink server and goes in standby for the rest of the time. So I can create an automation that when the sensor is at home it reacts, but what to I need to do, to do something, when the sensor is offline for longer thatn 24h?
Be advised that the drawback of using long periods of time in for, like 24 hours, is that its countdown is reset whenever Home Assistant restarts or automations are reloaded (if you compose/modify automations with the Automation Editor, the moment you click the Save button it will automatically reload all automations).
The alternative (that is more immune to restarts/reloads) is to periodically compute the time difference between the current time and the last time the sensor was updated. Basically it checks if the timedelta exceeds 24 hours.
This concept has been successfully used to detect “stale” sensors (i.e. sensors whose values haven’t changed in a long time).
Exactly what taras said. I wouldn’t use a for trigger.
I’d use history stats and calulate off that.
- platform: history_stats
name: Pool Alarm Time
entity_id: binary_sensor.pool_swg_alarm_on
state: 'on'
type: time
start: "{{ as_timestamp( now() ) - 10440 }}"
end: "{{ now() }}"
and then you can calculate the time it was in that state.
Alternatively, if you don’t care about the amount of time, use a timer and select the restore option when you create it… Then start a timer when its in a failed state, and cancel it when it is fine. Alarm when it ‘finishes’.
The only issue with this though is if that ‘error’ state flips to unknown when restarting temporarily. Correct?
Great Idea, will try that out. I can use that trigger. The drawbacks are not such an issue, There are always longer periods a week, when I wont work on HA. I just need a notification when the device is unvailable for a longer period of time.
On the other hand the second solution is even nicer. I will try out both. Thanks a lot!!
Good point; I haven’t tested it but it makes sense that it would serve to fool the calculation. However, I believe it would also influence the History Stats sensor’s calculation as well so it’s tough to mitigate. Although History Stats is cumulative so a short state-change during a restart isn’t likely to upset a count of many hours (I think).
I would use myFritzbox status, whether device is present or not. As you can see in the chart, theres a small timeframe each day, where the device connects to the network.
If you calculate off of the update time, you’re device could be offline 99% of the time and you won’t get notifications.
I have an alarm for the pool salt that does this at times when the pool filter needs to be cleaned. So I use history_stats to determine how long it has been in a state rather than relying on a last update. This way I can say “Has my pool been alarming more than 60 minutes in the past 4 hours” and alert me that I need to clean the filter.
It appears that restarts do affect the suggested use of a History Stats sensor but in a subtle way.
I created a History Stats sensor to monitor a binary_sensor that has been off since midnight June 20 (so it’s been off for over 24 hours). The History Stats sensor looks at the past 10 hours so you would expect it to report 10 given that the target entity has been off for the past 10 hours (and even longer than that). However, it reports 9.97.
I believe the reason it isn’t 10 is because, to load the new History Stats sensor, I restarted Home Assistant (at least three or four times because I was tweaking the configuration). During a restart, the binary_sensor’s state toggles from off to unknown to off. Those brief intervals of unknown subtract from the total time the entity has been off in the past 10 hours. So the result is 9.97. It won’t report 10 unless 10 hours pass without a single restart (or changing to another state).