Enable automation, execute code after 10 seconds of activation

Hi Guys,

i have an automation that is disabled by default. If i enable that automation it should execute the sequence when the automation is enabled for 10 seconds and than disables it self.

this is my automation:

alias: Ganglicht automatisch ausschalten
description: ""
trigger:
  - alias: Wenn die Automatisation eine bestimmte Zeit lang aktiv ist
    platform: state
    entity_id:
      - automation.disable_floorlight
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 10
condition: []
action:
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.grossraum
    alias: Licht im Gang ausschalten
  - alias: Automatisierung wieder beenden
    service: automation.turn_off
    metadata: {}
    data:
      stop_actions: false
    target:
      entity_id: automation.disable_floorlight
mode: restart

The action will not take place. But the message “run” is displayed (in german “ausgeführt”).

This is behind a click on the message “run”:

variables:
  trigger:
    id: "0"
    idx: "0"
    alias: Wenn die Automatisation eine bestimmte Zeit lang aktiv ist
    platform: state
    entity_id: automation.disable_floorlight
    from_state:
      entity_id: automation.disable_floorlight
      state: "off"
      attributes:
        id: "1707771353464"
        last_triggered: "2024-02-19T23:16:38.461101+00:00"
        mode: restart
        current: 0
        friendly_name: Ganglicht automatisch ausschalten
      last_changed: "2024-02-19T23:17:22.294759+00:00"
      last_updated: "2024-02-19T23:17:22.294759+00:00"
      context:
        id: 01HQ1THS7PTSR8BZ38MAC70MCW
        parent_id: null
        user_id: null
    to_state:
      entity_id: automation.disable_floorlight
      state: "on"
      attributes:
        id: "1707771353464"
        last_triggered: "2024-02-19T23:16:38.461101+00:00"
        mode: restart
        current: 0
        friendly_name: Ganglicht automatisch ausschalten
      last_changed: "2024-02-19T23:17:26.597630+00:00"
      last_updated: "2024-02-19T23:17:26.597630+00:00"
      context:
        id: 01HQ1THXE58TMWTVSDA4GYJXPQ
        parent_id: null
        user_id: a83f5e7aa5e84a639fbfb0b6ec492796
    for:
      __type: <class 'datetime.timedelta'>
      total_seconds: 10
    attribute: null
    description: state of automation.disable_floorlight
context:
  id: 01HQ1THXE58TMWTVSDA4GYJXPQ
  parent_id: null
  user_id: a83f5e7aa5e84a639fbfb0b6ec492796

Why?

That’s quite interesting. I am able to replicate the same behavior. What you have found seems to be a bug in the UI of the automation editor. The automation should not trigger. And you will find that if you exit out of the automation editor and disable/re-enable that same automation from the automation list, your automation will not trigger.

The automation should not trigger because it needs to be enabled first before the trigger can be armed, and only then can it react to state changes. Since the automation’s state is already on before the trigger is armed, it will not see the state changing to on and so this will not work.

I’m not sure what your end goal is, but I’m certain there are better methods of achieving it than what you’ve outlined.

What is the purpose of disabling the automation?

This automation should disable my floor lights after a defined period of time. This instance is running in an office. When you go to home and you leave your room with switching off your lights, the floor lights should automatically switch on and switch off after 2 Minutes.

My first solution was:

  1. Create an automation disabled by default
  2. Disable the automation before enabling it to reset the “last” changed timestamp
  3. Enable the automation by pressing the rooms lights button for switch off
  4. After automation is enabled for 2 Minutes, switch off floor lights
  5. Disable automation

But for now, i will go with a helper variable, so changing the variable with current timestamp and listen with an automation for that change. And if the change resists about 2 minutes, disable the floor lights.

So automation can always stay enabled.

Is this the logic and sequence you’re looking to create?

  • When office lights are turned off: turn on floor lights, wait 2 minutes, then turn off floor lights.
alias: Floor lights on during departure
trigger:
  - platform: state
    entity_id:
      - light.office_lights
    to: "off"
action:
  - service: light.turn_on
    target:
      entity_id: light.floor_lights
  - delay:
      minutes: 2
  - service: light.turn_off
    target:
      entity_id: light.floor_lights
mode: single

I am not sure if that will do the correct thing, because we have 5 rooms, where on exit of that room the floor lights should stay on for 2 minutes.

Perhaps mode must be “restart”, so if person of room 1 exits and the timer starts for 2 minutes, but after 1m 30s a person of room 2 exits, so the timer must restart.

But with my helper variable as datetime i am able to set explicit a time when the lights should go off this is a cool feature, in case some one is a bit slower in walking, then we can increase the timer to 3 minutes for his room.

By the way, changing the method how the trigger will work did the trick.

For those they are interested in my working example - here are the code snippets:

DateTime-Helper when the lights in the floor should turned off

input_datetime:
  disable_floorlights_at:
    name: Ausschalten der Flurlichter um
    has_date: true
    has_time: true

Automation Script when Button for Room Lights on/off was pressed

alias: Raum 02 - Lichtschalter
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.plusi4_r2_input_3_input
    from: "off"
    to: "on"
    alias: Lichtschalter gedrückt wurde
condition: []
action:
  - alias: >-
      Ganglicht einschalten, wenn es dunkel ist und das Licht im Raum
      ausgeschaltet wird
    if:
      - alias: Alle Bedingungen müssen erfüllt sein
        condition: and
        conditions:
          - condition: state
            entity_id: input_boolean.current_darkness
            state: "on"
            enabled: true
            alias: Es ist dunkel
          - condition: state
            entity_id: light.power_switch_room_02_light_switch
            state: "on"
            enabled: true
            alias: Das Licht ist gerade noch an
          - condition: state
            entity_id: light.grossraum
            state: "off"
            alias: Die Lichter im Gang sind aus
    then:
      - service: light.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: light.grossraum
        alias: Licht im Gang einschalten
      - alias: Licht im Gang automatisch wieder ausschalten
        service: input_datetime.set_datetime
        target:
          entity_id: input_datetime.disable_floorlights_at
        data:
          datetime: |
            {{ (now().timestamp() + 2*60 + 10)
               | timestamp_custom('%Y-%m-%d %H:%M:%S') }}
  - service: light.toggle
    target:
      entity_id: light.power_switch_room_02_light_switch
    data: {}
    alias: Licht im Raum an/aus
mode: single

Automation to turn off floor lights

alias: Ganglicht automatisch ausschalten
description: ""
trigger:
  - platform: time
    at: input_datetime.disable_floorlights_at
condition: []
action:
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.grossraum
    alias: Licht im Gang ausschalten
mode: single