Automatyion: turn ON light after door opens

I want to do this.

I have a bedroom door is usually open during the day, and closed at night. The door now has a window/door sensor. In front of the door there is a corridor with a smart bulb.

Goal is

  • at night when door opens turn ON corridor light
  • if light ON for more then 3 minutes then turn OFF
  • when door closes turn OFF corridor light.

Below is my code, but is not working, do not know why

  - alias: "Turn ON corridor light at night"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.door_window_sensor_158d000126a2fb
        to: 'on'
    condition:
      - condition: numeric_state
        entity_id: sun.sun
        value_template: '{{ state.attributes.elevation }}'
        below: -5
    action:
      - service: light.turn_on
        entity_id: light.gateway_light_286c0785aeab
      - delay: '00:02:30'
      - service: light.turn_off
        entity_id: light.gateway_light_286c0785aeab
#
#
  - alias: "Turn OFF corridor light at night"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.door_window_sensor_158d000126a2fb
        to: 'off'
    condition:
      - condition: numeric_state
        entity_id: sun.sun
        value_template: '{{ state.attributes.elevation }}'
        below: -5
    action:
      - service: light.turn_off
        entity_id: light.gateway_light_286c0785aeab

does your door sensor report ‘on’ and ‘off’?

your initial_state should be whatever it is when the door is closed and your to should be whatever it is when the door is open.

yes the sensor reports correctly.

initial_state is the initial_state of the automation, should not affect the automation itself

The initial state you have only turns the automation on, which by default it is, so that does nothing, really.

If you are not commited to the sun elevation, you can try this:

    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.door_window_sensor_158d000126a2fb
        to: 'on'
  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
    action:
      - service: light.turn_on
        entity_id: light.gateway_light_286c0785aeab
      - delay: '00:02:30'
      - service: light.turn_off
        entity_id: light.gateway_light_286c0785aeab

Your automation to open it will turn on the light, and turn it off after 2 minutes and 30 seconds, regardless of what happens. Isn’t that what you want? It will turn it off after 2:30. The second automation will turn it off immediately.

Is the problem that it never triggers?

Now it works, but with an issue.

I have to open the door two times (open, close, open) and then the light turns ON. The door sensor shows the status correctly two times …

Did you change anything? What is your current automation?

yes, this has its reason: if you close the door supposedly the person is back in the room to sleeep.

if the door stays open for too long it suppose the person forgot, and left, so after 2.30 the light should go off automatically

no I haven’t changed my automatiobn. DO not know why before was not starting: the sun should have been already low enough

How many times does the log say that the automation executed? You should “tail” the OZW_log.txt file in /home/homeassistant/.homeassistant directory to see if that device is being turned on or off via Z-wave:

tail -f OZW_Log.txt

Run that on the commandline and then execute your automation and see what it says...

Not zwave, they are zigbee (xiaomi window/door and xiaomi gateway light)