Need help triggering an automation when a timestamp is X days old

Hello all,

I am trying to get a low-battery warning for my various Insteon Leak sensors, which talk to HA through MQTT. In addition to wet/dry, they each connect daily for a ‘heartbeat’. This shows up in HA as a timestamp entity. I pulled one battery out and sure enough, the timestamp of that sensor’s heartbeat still shows the last day it was successful. So, I need an automation that notifies me when any of those heartbeat sensors’ entities states gets to be more than 2 days ago. But it’s not formatted nicely, so I am stuck.

Right now, the State of the Entity i’m playing with says this “2022-04-22T22:48:32+00:00”.

Can anyone help me make an automation that constantly monitors those entities and fires whenever that timestamp strays further than 2 days from current time?

my bad. thanks for the tip on where to put it. Is there a way to delete this thread?

no reason to delete the thread. just click the “edit” pencil by the title and change the category there.

but to answer your question, you can set up a template trigger for an automation using this as the value_template:

{{ as_timestamp(now()) + 84600 * 2 > as_timestamp(states('sensor.your_sensor_id')) }}

Also, you might want to have a look on the alert integration instead of an automation. It will regularly remind you about the situation.

Oh, right. One would need to add the condition you described above as a template sensor before setting up the alert.

I love the idea of using Alert instead of a single iOS notification action. Thank you for mentioning it.

So far I have 3 Template Trigger suggestions so I set up a single new automation with all 3 as different Triggers, each with a TriggerID. I’m going to pull out a sensor’s battery and wait 2 days to see which triggers fire.

Once I have confirmation that the triggers work, i’ll set up the Alert Integration as the Action for this Automation by creating a new Helper Input Boolean that gets changed to “on” to trigger the Alert. (Hope I have that right)

Thank you for the suggestions. After reviewing the help pages, I put together the following chunk of code which I intend to add to the bottom of my configuration.yaml file. Does this look right?

template:
  - binary_sensor:
      - name: "Leak Sensor Battery Low"
        state: "{{ now() - states('sensor.ls_attic_heartbeat') | as_datetime > timedelta(days=2) }}"
        device_class: battery

alert:
  ls_battery:
    name: Leak Sensor Network Detected a problem!
    done_message: Leak Sensor Network Condition Cleared!
    entity_id: binary_sensor.leak_sensor_battery_low
    repeat: 2
    can_acknowledge: true
    skip_first: false
    notifiers:
      - mobile_app_joshs_13_pro

Question: How do I add more than one leak sensor to this alert? I have about a dozen sensor.ls_whatever_heartbeat devices scattered around the house. How do I set this up to trigger for any/all of them?

Sorry, that is beyond me. I dont understand how to ensure that all my leak sensors are included.

I get lost at the value_template in that thread you linked. I dont even see anything in my HA entities that shows that these heartbeats are device_class: battery - if that’s even the right question to be asking…

(post deleted by author)

I am asking for help. Maybe point me in the right direction to show me what I need to learn…? It’s the value_template bit, no?

I didn’t ask you to re-engineer anything. I asked you to help me understand how it works. If you cant be bothered, why are you so active on this forum?

That was a really unhelpful, toxic response when I am just trying to learn. It’s not like there’s good documentation for this.

I wouldn’t even mind listing them each one at a time. Would that make it easier than trying to learn how the value_template bit automatically grabs each ls sensor I am concerned about?

Solution found.
I got 3 suggested “template”-based automation triggers. I made an automation with all 3 and found only 1 works. So here’s that: {{ now() - states(‘sensor.ls_attic_heartbeat’) | as_datetime > timedelta(days=2) }}

In addition, I created a text helper. So I have 1 automation that lists a dozen triggers, each Templates with the code above (swapping in the names of each sensor). That automation both sets my text helper to trigger my new Alert - and it sends me a notification directly since that direct automation can actually identify which sensor died.

As a result, whenever any of my sensors miss a daily check-in, I get a notification identifying the dead battery and I get a repeat of that notification (Alert) every hour until I do something about it.