Automation for doorbell, light after xx minutes off

Hello,

I am having problems with making an automation for my doorbell switch.
If I presse the button the light goes on and must be switched off after xx seconds.

I have made this in Yaml but it does not work.

alias: Deurbel_plafond lamp
description: “”
trigger:

  • type: running
    platform: device
    device_id: 17932d1f68da72f5a5519f50d62fa668
    entity_id: 9162590024c0e2dee1a537272686b0be
    domain: binary_sensor
    for:
    hours: 0
    minutes: 0
    seconds: 0
    condition:
    action:
  • type: turn_on
    device_id: fce17433983168e8582bc3d795443ba4
    entity_id: 5ffdf9350cf7ccab9faeff85d7332f1f
    domain: light
  • if:
    • condition: state
      entity_id: light.plafondlamp_gang_licht
      state: “on”
      for:
      hours: 0
      minutes: 0
      seconds: 30
      then:
    • type: turn_off
      device_id: fce17433983168e8582bc3d795443ba4
      entity_id: 5ffdf9350cf7ccab9faeff85d7332f1f
      domain: light
      mode: single

What I am doing wrong?

First of all, not formatting your code correctly for a forum post. See here:

It’s really important: there could be lots of errors hiding in there that the formatting doesn’t show, and it’s much easier for us to read and diagnose when properly presented.

The problem seems to be in how you think the if: statement works. It does not wait there until it’s true: it evaluates once (as false, because the light has only just turned on) then continues. Have a look at the various ways to wait:

I’d strongly recommend using entities and state triggers rather than device IDs. Is light.plafondlamp_gang_licht the same as 5ffdf9350cf7ccab9faeff85d7332f1f?

Something like this:

trigger:
  - platform: state
    entity_id: binary_sensor.[DOORBELL ENTITY ID]
    to: 'on'
action:
  - service: light.turn_on
    entity_id: light.plafondlamp_gang_licht
  - delay: 30
  - service: light.turn_off
    entity_id: light.plafondlamp_gang_licht

At a guess, though it’s hard to be sure as that code isn’t formatted, your problem is that you check to see if the light has been on for 30 seconds when the doorbell is pressed. If at that point it has been on for 30 seconds the light will be turned off.

If you just want it turned off 30 seconds after it was turned on by the doorbell then you’d want to have an input boolean that was turned on when you turned on the light. Then when the light (or boolean) has been on for 30 seconds turn off the light. Another automation can turn off the boolean whenever the light is turned off (see here).

Thanks for the help, here is the code in the right format :

alias: Deurbel_plafond lamp
description: ""
trigger:
  - type: running
    platform: device
    device_id: 17932d1f68da72f5a5519f50d62fa668
    entity_id: 9162590024c0e2dee1a537272686b0be
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition: []
action:
  - type: turn_on
    device_id: fce17433983168e8582bc3d795443ba4
    entity_id: 5ffdf9350cf7ccab9faeff85d7332f1f
    domain: light
  - if:
      - condition: state
        entity_id: light.plafondlamp_gang_licht
        state: "on"
        for:
          hours: 0
          minutes: 0
          seconds: 30
    then:
      - type: turn_off
        device_id: fce17433983168e8582bc3d795443ba4
        entity_id: 5ffdf9350cf7ccab9faeff85d7332f1f
        domain: light
mode: single

I will look at the entities

1 Like

It is working now!!

alias: Deurbel_plafond lamp
description: ""
trigger:
  - type: running
    platform: device
    device_id: 17932d1f68da72f5a5519f50d62fa668
    entity_id: 9162590024c0e2dee1a537272686b0be
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition: []
action:
  - type: turn_on
    device_id: fce17433983168e8582bc3d795443ba4
    entity_id: 5ffdf9350cf7ccab9faeff85d7332f1f
    domain: light
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: fce17433983168e8582bc3d795443ba4
    entity_id: 5ffdf9350cf7ccab9faeff85d7332f1f
    domain: light
mode: single

Thanks for pointing me in the right direction and the help!

1 Like