Automation to activate lighting when it's dark is not triggered

Over the past days, I installed Home Assistant and explored the docs. So far, I integrated my Unifi Protect cameras and a HomeMaster MiniPLC (ESPHome device - ESP32). This evening, I tried setting up my first automation for the following: when I go into the garden when it is dark, turn on the lighting at the exterior of my garden house and turn it of again after a certain delay.

I based myself on the “Motion-activated Light” blueprint and came up with the following setup:

alias: Verlichting Tuinhuis als het donker is
description: Verlichting Tuinhuis als het donker is
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.tuinhuis_camera_tuinhuis_person_detected
    from:
      - 'off'
    to:
      - 'on'
conditions:
  - condition: state
    entity_id: binary_sensor.tuinhuis_camera_tuinhuis_is_dark
    state:
      - 'on'
actions:
  - action: light.turn_on
    metadata: {}
    target:
      entity_id: light.tuinhuis_homemaster_tuinhuis_licht_toog
    data: {}
  - wait_for_trigger:
      - trigger: state
        entity_id:
          - binary_sensor.tuinhuis_camera_tuinhuis_person_detected
        from:
          - 'on'
        to:
          - 'off'
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - action: light.turn_off
    metadata: {}
    target:
      entity_id: light.tuinhuis_homemaster_tuinhuis_licht_toog
    data: {}
mode: restart

When I go into the garden when it’s dark, I get a notification from the Unifi Protect app that a person is detected, however Home Assistant does nothing. Nothing ends up in the tracing except for my manual trigger to test the actions.

The entity is_dark from my Unifi camera shows as On in Home Assistant, I verified the entity names multiple times, but to no avail. Am I still missing something?

Ringo

Then you need to debug the trigger. There should be a Trace for every time binary_sensor.tuinhuis_camera_tuinhuis_person_detected changes from “off” to “on”… even if the “is_dark” condition doesn’t pass. Double check the history of the “person detected” sensor to make sure it’s actually changing state as expected.


FWIW, while the construction of the automation does work, most of us would not use a wait_for_trigger in this type of automation. Having an automation languish in a Wait, when it isn’t necessary, serves no purpose. Assuming that the “person detected” sensor is changing states, I would probably use something like:

alias: Verlichting Tuinhuis als het donker is
description: Verlichting Tuinhuis als het donker is
triggers:
  - id: "on"
    trigger: state
    entity_id:
      - binary_sensor.tuinhuis_camera_tuinhuis_person_detected
    from: 'off'
    to: 'on'
  - id: "off"
    trigger: state
    entity_id:
      - binary_sensor.tuinhuis_camera_tuinhuis_person_detected
    from: 'on'
    to: 'off'
    for: "00:03:00"
conditions: []
actions:
  - if:
      - condition: trigger
        id: "on"
    then:
      - condition: state
        entity_id: binary_sensor.tuinhuis_camera_tuinhuis_is_dark
        state: 'on'
      - action: light.turn_on
        metadata: {}
        target:
          entity_id: light.tuinhuis_homemaster_tuinhuis_licht_toog
        data: {}
    else:
      - action: light.turn_off
        metadata: {}
        target:
          entity_id: light.tuinhuis_homemaster_tuinhuis_licht_toog
        data: {}
mode: restart

@Didgeridrew thank you for the improved version of this automation. That’s how I learn to do it better. :folded_hands:

Strange thing is that I verified the trigger and the is_dark property multiple times and they responded correctly. Out of the blue, my (old version of the) automation started triggering this morning. It was still dark and the light went on. I don’t recall changing anything after I reported my problem here on the forum. Very strange…

Let’s keep an eye on this improved version and see if it remains working stable.

Thanks!