Turn off Device one hour after it's turned on, but just if it's turned on manually

I currently have an automation set up to control a device, this is working fine.
For the time when this device is turned off and not triggered by this automation, I want the device to turn off after one hour.

How can I set up the condition to check if the device was turned on via automation and vice versa.

Maybe create a dummy device, configure it in the first automation and check for it’s state in the second automation. But I think there has to be a simpler solution.

Has anyone of you faced a similar issue?

Thanks!

Check out this example of storing and using context:

1 Like

qq: Do timers survive HA being restarted/rebooted/power outages?

no the don’t

as an escape I always set a datetime helper,
it’s more work and you need 2 automations

Cool; so you set the datetime helper instead of the timer, and trigger based on the datetime helper?

My method will survive a restart as triggered template sensors are restored. Also it only requires one automation.

I use this

  action:
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.sms_last_send
    data:
      timestamp: '{{ now().timestamp() }}'
  trigger:
  - platform: template
    value_template: '{{as_timestamp(now()) >= as_timestamp(states(''input_datetime.sms_last_send''))
      + (160*86400) }}'

but maybe @tom_l 's method is better

1 Like

IIUC garage_switch_on_context is effectively a tiny state machine maintained by a template sensor rather than a helper. That’s actually a pretty nice pattern.

@tom_l this is GENIOUS!