Run automation after input changed x hours ago

Hi
I have a couple of things i want to impliment.
I have movement sensors in rooms that are occupied when the kids come. I would like to turn of the heating to these rooms after 12 hours since movement detector last activated. Also turn on when the movement sensor is activated.
I ‘could’ use a 12 hour timer but as this wont survive reboots id prefer to check say every 10 mins what the timestamp of the last detection was.
Any suggestions please?

James

I use this

  {{  (as_timestamp(now())-as_timestamp(states.binary_sensor.pir_kokken.last_changed))   > 1000}}´

1000 is seconds

Great thanks, is that the trigger?

I’d suggest that every time the sensor turns off, you set an input_datetime for +12 hours.

That will survive restarts and reboots, which the template suggested won’t.

Then you can use that input_datetime in a time trigger, and also have a startup trigger, then check to see if the time has passed the target:

trigger:
- platform: time
  at: input_datetime.whale_idle
  id: time
- platform: homeassistant
  event: start
  id: start
condition:
- condition: or
  conditions:
  - condition: trigger
    id: time
  - condition: time
    after: input_datetime.whale_idle
action:
...
1 Like

yes i use it as a trigger, but it restart when you restart HA

Ok thats great info
Couple of things

When you say it will restart on HA restart does that mean the last_change value is lost on restart?

also if using the other method it appears when it triggers it sets a new trigger in the future to turn the heating off, if there is another trigger of the moevement sensor will that remove the previous turn off trigger?

Many Thanks

If you have an automation like this, then it’ll keep setting it forwards 12 hours:

trigger:
- platform: state
  entity_id: binary_sensor.petunia_movement
  to: 'off'
action:
- service: input_datetime.set_datetime
  data:
    entity_id: input_datetime.whale_idle
    timestamp: "{{ as_timestamp(now() + timedelta(hours=12)) }}"

Every time it stops detecting movement the turn off input_datetime is set forwards 12 hours.

Superb thanks
Does this go in the configuration.yaml file?

James

Both of the code snippets I’ve shared are parts of an automation.