Start timer when lights turns on by traditional switch or from dashboard

Hello everyone!
New HA user here. I have this automation to turn on the lights by the motion sensor and turn them it off after a delay when motion is clean.

alias: Kitchen motion light
description: ""
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.kitchen_light
    id: Kitchen Timer Finished
  - type: no_motion
    platform: device
    device_id: d522ab083e3e5dd398148
    entity_id: binary_sensor.0x00124b002_occupancy
    domain: binary_sensor
    id: Kitchen Motion Stopped
  - type: motion
    platform: device
    device_id: d522ab083e3e5dd3981489568
    entity_id: binary_sensor.0x00124b00291_occupancy
    domain: binary_sensor
    id: Kitchen Detected
  - platform: event
    event_type: state.on
    event_data:
      entity_id: light.kitchen
    id: Lights On
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Kitchen Motion Stopped
        sequence:
          - service: timer.start
            data:
              duration: "60"
            target:
              entity_id: timer.kitchen_light
      - conditions:
          - condition: trigger
            id: Kitchen Detected
        sequence:
          - service: timer.cancel
            data: {}
            target:
              entity_id: timer.kitchen_light
          - service: light.turn_on
            data:
              brightness_pct: 79
            target:
              entity_id: light.kitchen
      - conditions:
          - condition: trigger
            id: Kitchen Timer Finished
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.kitchen
mode: single

How can I update it to start the timer in case lights turned on by traditional switch or from the dashboard? I tried to change it in this way but it was not working.

alias: Kitchen motion light
description: ""
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.kitchen_light
    id: Kitchen Timer Finished
  - type: no_motion
    platform: device
    device_id: d522ab083e3e5dd39814895681
    entity_id: binary_sensor.0x00124b00291_occupancy
    domain: binary_sensor
    id: Kitchen Motion Stopped
  - type: motion
    platform: device
    device_id: d522ab083e3e5dd39814895681c
    entity_id: binary_sensor.0x00124b0029_occupancy
    domain: binary_sensor
    id: Kitchen Detected
  - platform: event
    event_type: state.on
    event_data:
      entity_id: light.kitchen
    id: Lights On
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Kitchen Motion Stopped
        sequence:
          - service: timer.start
            data:
              duration: "60"
            target:
              entity_id: timer.kitchen_light
      - conditions:
          - condition: trigger
            id: Kitchen Detected
        sequence:
          - service: timer.cancel
            data: {}
            target:
              entity_id: timer.kitchen_light
          - service: light.turn_on
            data:
              brightness_pct: 79
            target:
              entity_id: light.kitchen
      - conditions:
          - condition: trigger
            id: Kitchen Timer Finished
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.kitchen
      - conditions:
          - condition: state
            entity_id: light.kitchen
            state: "on"
            for:
              hours: 0
              minutes: 1
              seconds: 0
        sequence:
          - service: timer.start
            data:
              duration: "60"
            target:
              entity_id: timer.kitchen_light
mode: single

Your current automation triggers instantly when you turn the light on however in your final choose condition you have listed the condition of the light having been on for 1 min - this will obviously never be the case if the automation was triggered by the light on trigger.

EDIT: Notice also that they way you have your triggers set, when motion is detected and the light turns this would cause the automation to want to run again and may therefore cause warnings in the HA logs of automation already running as you have it set to single mode.

I would use the state event, of light on for 1 minute if thats what you want:

- platform: state
  entity_id: light.kitchen
  from: "off"
  to: "on"
  for:
    minutes: 1

Then remove the conditions from the event action and use the trigger ID like you have on the others:

- conditions:
    - condition: trigger
      id: Lights On
  sequence:
    - service: timer.start
      data:
        duration: "60"
      target:
        entity_id: timer.kitchen_light

This also means by moving the condition of 1 minute to the trigger it won’t try to trigger the automation again as soon as motion switches the light on as it will only trigger again after 1 minute.

1 Like

Thanks.

Your current automation triggers instantly when you turn the light on

Could you help me to understand which part of the code is responsible to trigger instantly when the light turns on? My assume was this automation will trigger based on the triggers that are defined for motion start, stop and timer finish

This trigger is in both your above automations.

1 Like

Are you using Node-Red?