Trigger automation based on time since specific state?

I want to trigger an automation if one of my device tracker entities have the state ‘not_home’ for 7 days. But don’t know how to do that, since I need to be able to restart homeassistant, and not reset the trigger/condition. Is there any way of doing this?

Use this documentation as a guide:

You’re going to want to use in your trigger:

for:
  hours: 168

That was the first I thought of, but doesn’t that reset when homeassistant restarts?

Yes, but all other options will do that too unless you write to disk, I.E create a text file. You could attempt to use the history_statistics component:

1 Like

I thought of that too, without figuring out how to do it.
But with a closer look I of course could just use count, and set the sensor for 7 days, then set the automation to trigger when the count reaches 0 :slight_smile:
Thank you

See this thread where we tried to find a workaround.
Basically, a script or an automation “last triggered” survive a restart. Depending on some options several “input_XXX” objects have their status reminded.

So you could either use a dummy automation, triggered when the device goes into “not_home” status, and then use its last_triggered attribute to fire the automation after 7 days, along with the condition that the device is not at home (meaning its been home for 7 days).

Or you could use an input_number, set it to 7 when device goes not_home, then find a rule to decrease this value (every day at midnight for instance) depending on the precision you expect. Your automation starts when the number reaches 0.

Or you can try the input_datetime component that also supports the restore_state function, set it to now()+7 days when device goes not home, and then trigger the automation when this day/time is reached. Never tried this component though.

1 Like

You could probably also use a MQTT topic to store the date/time. Probably want to store the as_timestamp(now()). That does not get reset on boot.

1 Like

Thanks both of you! I was thinking of using last_triggered to, to find it just was ‘null’ of all my automations. But I just now realized I have no automations written in the recorder, so it would of course show that upon restart :stuck_out_tongue: (I just record entities I have enabled in recorder)
I see there is a lot of ways to achieve this, thank you for this. I will check now if my statistic sensor works as intended, if not, use one of the ways you guys have described :blush:

I went with the mqtt sensor, that worked perfectly :slight_smile:
The problem with using the automation`s attribute last_triggered, is that it survive a restart, but doesn’t survive a “reload automation”. But if you print last_triggered to an mqtt sensor, that isn’t a problem :slight_smile:

I also use a command line “as_timestamp(now())”-sensor with the template trigger, since using as_timestamp(now()) as template trigger does not work (will never trigger).

The statistic sensor did also work, but since I also wanted to make a sensor that showed me the time, I went with the above solution.