Automation: Compare time values

Just a quick question:
I have a battery powered sensor, which sends a measurement to home assistant all 3 minutes. Unfortunately this sensor isn’t capable in sensing it’s own battery level, so i don’t get notified when the battery runs low. I usualle notice it when there are no more data points received, which can take some days, since i don’t look constantly on the data.

So i’d like to create an automation like “if the last data point is received more than an hour ago, send an email”. But i am unsure how to achieve this. Basically i would create something like this:

automation:
  - alias: "Send email if sensor isn't sending data points"
    trigger:
    - platform: numeric_state
      entity_id: sensor.battery_powered
      value_template: 'Now - {{ state.last_changed }}'
      above: 1 Hour
    action:
      // send emails

The problem is, that i have no idea how to get Now as a value to compare with, and that i have no idea how Home Assistant compares time values. The time trigger isn’t the right trigger, i think. Maybe the offset feature like the sun trigger could be something, but this isn’t in the numeric state trigger, i suppose.

On a second hand: Say i have successfully implemented an action. The time difference exceeds 1 hour, and an email is sent. However, i do not take actions immediately to replace the batteries. How does Home Assistant check for automation triggers? Say, after 1 hour and 10 minutes, the difference is still above 1 hour, would it send another email, or would the automation be in some kind of “running” or “triggered” state and not fire again until the time difference would fall below 1 hour and then rising up again?

Here is some code that should help.
put this in the template editor and play with it.

#datetime
{{now()}}
#timestamp
{{as_timestamp(now())}}
add 1 hour (3600 seconds)
{{as_timestamp(now())+ (1*60*60)}}
display as local time
{{(as_timestamp(now())+ (1*60*60))  | timestamp_local}}
display just the hour
{{(as_timestamp(now())+ (1*60*60))  | timestamp_custom("%H",true)}}
2 Likes

@simonszu have you got it working? Your post made me realize I need a trigger like this as well on one of my sensors.

@sjee unforunately not. The sensor was connected via Homematic, and i discovered that even when the sensor itself doesn’t send any values, the homematic bridge keeps sending datagrams to HASS with the content “undefined”, but with a quite current state.last_changed, so i just have to check if the sensor switches to undefined.

However, i have changed the batteries, and everything’s working fine again. So i didn’t implement anything yet.

I think you just have to check for {{ as_timestamp(now()) - as_timestamp(states(sensor.mysensor.last_changed)) > 3600 }} which converts now and the last receive time from the sensor into unix timestamps (which are the number of seconds) and if the difference is > 3600, the last receive time is more than 1 hour ago.

2 Likes

@simonszu thanks that should work. To avoid continuous triggering I have a couple of scripts which are triggered on time (and presence), in this case I will add the time_difference template as a condition. But you can also use a binary sensor.