How to enable an automation if motion/presence was detected the day before?

My idea is to have the automation be triggered at a certain time every day. In the condition, I would setup a template where it would check if a certain entity’s state was detected the day before. If so, the action would enable another automation I created.

I tried googling and this is what google came up with.

Template: {{ states('your_presence_entity') | state_attr('last_changed') | timestamp_custom('%Y-%m-%d') == (as_timestamp(now()) | timestamp_custom('%Y-%m-%d') | date_modify('day', -1)) }} 
Template: {{ states('your_motion_sensor_entity') | state_attr('last_changed') | timestamp_custom("%Y-%m-%d") == (now() | timestamp_custom("%Y-%m-%d") |  as_timestamp - 86400) }}

Rather than enabling and disabling automations just use conditions in the automations.

1 Like

Gah, that looks like Gemini or OpenAI generated code.

Here is a sample for a template that will trigger whenever the state of a binary sensor (a human presence radar, in this case) has remained off for 24 hours or longer:

{{  
   states["binary_sensor.entranceway_radar_mmwave"].last_changed | as_timestamp
   <
   (now() | as_timestamp - 86400)
   and
   is_state("binary_sensor.entranceway_radar_mmwave", "off")
}}

Note that, if your sensor has at any point been unavailable, the last changed value will be reset. Therefore, this can be fragile. I know of no templaty way to ask the history integration if a sensor has remained in a specific state for so long.

Most robust technique, probably, is to make a date/time helper that is reset by an automation every time the binary sensor transitions specifically from on to off (so it can ignore any unavailable or unknown state changes), and another automation that triggers on comparison between said date/time helper and now() - 86400. Actually, you could probably combine both triggers into a single automation, and use a Choose building block to perform either task.

Agree with both Tom and Rudd.

But what have you tried? Anything? Posting a response from Google to us is just … umm … us Googling by proxy something we did not Google.

Oh, if you are handy with SQL, you could query the recorder: SQL - Home Assistant

You will have to set up a template sensor that triggers every minute or every hour to update the sensor value.