Turn off if device is on for X minutes even after restart

Hi all,
I’m newbie and trying out my hand at automation. Apologies if it has been asked or answered before.

I have a heater connected to smart plug. I have set an automation which turns it off after 45 minutes. It has worked beautifully and in most cases. However, if I restart Home Assistant, it stops working.

Is there way to extend this to continue after reboot? I’m thinking to setup a helper to record the time when smart plug is turned on but not sure how to restart other automation without trigger and also it doubles up automations (I plan to include more such timers for different room).

alias: Matter Plug 2 - Auto Off - 45 Min
description: ""
triggers:
  - trigger: state
    entity_id:
      - switch.matter_plug_2
    to: "on"
    for:
      hours: 0
      minutes: 45
      seconds: 0
    from: "off"
conditions: []
actions:
  - action: switch.turn_off
    target:
      entity_id:
        - switch.matter_plug_2
    data: {}
mode: single

Hey

To overcome this problem, you need to create the automation to start a Timer instead of using the time in the automation. The delay in the automation does not transfer during the reboot.

This is example, when I switch on my backdoor light it will stay on for 10 Minutes then switch off.

alias: Automation - Backdoor - Light - Timer
description: ""
mode: single
triggers:
  - entity_id:
      - switch.back_door_light_switch_1
    to: "on"
    trigger: state
  - event_type: timer.finished
    event_data:
      entity_id: timer.backdoor_light
    id: timer_complete_backdoor_light
    trigger: event
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - timer_complete_backdoor_light
        sequence:
          - action: switch.turn_off
            metadata: {}
            data: {}
            target:
              device_id: 740c089ef7abebf88c95f4d3c95c1666
      - conditions:
          - condition: state
            entity_id: timer.backdoor_light
            state: idle
        sequence:
          - metadata: {}
            data:
              duration: "00:10:00"
            target:
              entity_id: timer.backdoor_light
            action: timer.start

You can create the timer during the automation configuration, but would recommend doing separately: Setting → Devices → Helpers → Create a Helper
The select Timer and give it a name and “tick” the restore box, as that will restore the timer to is state before the reboot / reload.

2 Likes

Thanks @BambamNZ works perfectly. I was worried about timer being finished when HA restarts (or power failure) but seems it is triggered as soon as HA boots up.

I’ve worked little more on the automation and added few more features. I also (tried) created a blueprint to replicate it across rooms & switches.

  • On switch on, start timer is not already running
  • On timer finishing off, turn off the switch
  • If Switch is turned off halfway the timer, cancel timer
  • Allow for multiple queued (in case & not sure it I can test it)
alias: Matter Plug 2 - Auto Off - 45 Min Timer
description: ""
triggers:
  - trigger: state
    entity_id:
      - switch.matter_plug_2
    to: "on"
    id: turned_On
  - trigger: event
    event_type: timer.finished
    event_data:
      entity_id: timer.matter_plug_2
    id: timer_matter_plug_2_finished
  - trigger: state
    entity_id:
      - switch.matter_plug_2
    to: "off"
    id: turned_Off
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - timer_matter_plug_2_finished
          - condition: state
            entity_id: switch.matter_plug_2
            state: "on"
        sequence:
          - action: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.matter_plug_2
      - conditions:
          - condition: trigger
            id:
              - turned_On
          - condition: state
            entity_id: timer.matter_plug_2
            state: idle
        sequence:
          - action: timer.start
            metadata: {}
            data:
              duration: "00:45:00"
            target:
              entity_id: timer.matter_plug_2
      - conditions:
          - condition: trigger
            id:
              - turned_Off
          - condition: state
            entity_id: timer.matter_plug_2
            state:
              - active
              - paused
        sequence:
          - action: timer.cancel
            metadata: {}
            data: {}
            target:
              entity_id: timer.matter_plug_2
mode: queued
max: 5
1 Like