I’m new to HA and I don’t yet have an understanding of how YAML\Jinja2 is structured or how HAS stores\retrieves its data. So please bear with me.
I have several dozen binary door sensors. Currently, I send out an alert using a custom notification routine each time the binary state changes, if the change occurs during a specific time (For example, if a loading bay door is opened\closed at any time, or if an internal door is closed outside of normal hours).
The alert tells me what the current state is and what the time that the alert was generated. It’s pretty basic.
Insert message here
{{ states(‘binary_sensor.sensor_SensorNameGoes Here’) }}
Insert message here
{{ now().strftime( ‘%d-%m-%y %H:%M’) }}
I’d like to update the alert notification so that it can access the history in HA and have the message tell me how long the binary sensor has been in the current state as well as the time that it changed state. Ideally, I’d like to compile this into one long strong that can be sent out as a text notification.
For example "Date\Time - Loading Bay Door 1 has been opened. Door was last (opened\Closed on Date\Time, and has been opened\closed for HH:MM:SS.
What is te best way to go about this?
You could use states.binary_sensor.your_sensor.last_updated
. However this will get reset when you restart HA, so will be inaccurate in this edge case.
An automation that triggers on a state change to
open and not_from
unknown/unavailable, which stores the current time in a helper, would be one option. Given you have lots to track, you could store the time as attributes in a single sensor, but that’s fairly involved. It would be a bit easier (imo) if you’re using Node Red.
That’s if I’m understanding what you’re after. I’m assuming you want to repetitively send a message saying the door is still open, and how long it has been open. Your example doesn’t quite make sense because you have “has been opened” but “was last opened/closed”, and you should not be getting triggers for “open” to “open”. I was thinking this was a typo and the first should be “closed/opened”, but it wouldn’t make sense to repetitively send closed messages, so clearly I don’t fully understand.
Hi, no I’m only looking for a message on change of state, as some doors are left open for aong time and if I had a repeat message it would be spamming me all the time.
I’m only interested if a door was opened and closed in quick succession, or if it was left open or closed for a long period.
I’m managing reminders to close separately. If a door is left open that shouldn’t be it will upload the relevant cctv to show who opened it, based on the time stamps. That’s being handled by motioneye so I don’t need to code anything for it in HA, there is just an automation that calls a python script.