Automation trigger "3 days after a sensor, at 4:00 AM"

Hi,
i’m trying to set an automation that fires up 3 days after my HA latest reboot, but at a specific hour of the night.

My hardware is slow and unsupported and sometimes RAM gets filled and a restart of HA is needed.
Originally I did an automation that fires up every three nights at 4:00 AM but sometimes I restart HA manually for changing some config and the nightly restart becomes useless. So my idea was to create a trigger that adds 3 days to sensor.uptime PLUS… “some hours to reach 4 AM”.

Any help with the code?
Thanks :slight_smile:

Create a Calendar event.

system monitor has sensor.system_monitor_last_boot
create a trigger at your chosen reboot time and see if the last boot time is greater than 3 days.

by the way, if your issue is that it craps out when ram gets filled, system monitor has info about ram usage and available swap. instead of doing a random reboot at some time, you could trigger off low ramp/high swap or something more directly the problem.

no, i mean sensor.uptime
I don’t need to reboot my hardware, only HA process
But this is not important, I need to understand how to do something at sensor.uptime + 3 days + some hours until 4 AM

my actual test trigger is:
{{ (states(‘sensor.uptime’) | as_datetime + timedelta(days=+3)).isoformat() }}

But this will reboot my HA at bad hours

I wish to understand how to do better templates as it is a totally new language for me, my idea was to convert sensor.uptime to a simple YYYY-MM-DD trimming out the hours and minutes, but maybe there is a better approach.
So far I was able to create this:
{{ (states(‘sensor.uptime’) | as_datetime + timedelta(days=+3)).isoformat() }}

For instance I did a HA reboot (only my docker instance, not the whole hardare!) at 18:15 today (february 17) and I want to automatically restart HA on february 20 at 00:00.
If tomorrow I will restart it manually to adjust some configuration, the automation should restart HA on february 21 instead of 20

same problem, how to set a calendar event 3 days after a sensor state plus some hours to 4 AM?
i need some formula that transforms this:
{{ states('sensor.uptime') }}
which now is: 2024-02-17T20:41:17+00:00

to 2024-02-20T04:00:00+00:00

i get that you’re trying to do all the math to set a time and then prob create a trigger on that time. but i’m still thinking that’s overly complicated. does this do what you want and much simplier in 1 automation? no calendar event, no additional automation, etc.

trigger:
  - platform: time
    at: "04:00:00"
condition:
  - condition: template
    value_template: >
      {{(now() - as_datetime(states('sensor.system_monitor_last_boot'))).days >
      3  }}
action:
  - service: hassio.host_reboot

1 Like

to my second point, you said you need to reboot when the ram gets low. how about this to reboot when memory goes below 128k (or pick your threshold):

trigger:
  - platform: numeric_state
    entity_id:
      - sensor.system_monitor_memory_free
    below: 128
condition: []
action:
  - service: hassio.host_reboot
1 Like

thank you this solution works :slight_smile:
replaced with sensor.uptime and works for me

Hi,

I have used the following code to send notifications when a Zigbee device goes offline. To prevent messages when HA has been restarted, I used your template as a condition and changed it from “days” to “minutes”.
But it doesn’t work - what am I doing wrong?
Thank you for any help.

alias: Benachrichtigung_ZigbeeDeviceOffline
description: ""
triggers:
  - entity_id:
      - sensor.z2m_offline_devices
    to: null
    trigger: state
    from: null
conditions:
  - condition: template
    value_template: >
      {{(now() - as_datetime(states('sensor.system_monitor_last_boot'))).minutes
      > 3  }}
    enabled: true
actions:
  - data:
      title: Zigbee-Gerät offline
      message: >-
        Folgende Zigbee-Gräte sind offline: {{
        states("sensor.z2m_offline_devices") }}
    action: notify.ha_morschi
mode: single

You cannot invent your own attributes. A timedelta object, which would be the result of the preceding calculation, has no minutes attribute. Only days, seconds and microseconds because those are what it uses internally.

Thank you, but with seconds it still doesn´t work.

Is anything else wrong?

  - condition: template
    value_template: >
      {{ (now() - as_datetime(states('sensor.system_monitor_last_boot'))).total_seconds() > 180 }}

Well do you actually have a sensor.system_monitor_last_boot entity? I don’t, mine is called only sensor.last_boot and I am pretty sure I am not responsible for that shorter name. Substitute with that and the template works for me. Go to Settings > Devices & services > System Monitor to make sure the Last boot sensor is enabled and what its ID is.

1 Like

That will work. whats “Not working”?

You are totally right, didn´t check that setting - now it works.
Thank you!