Could you help my with my lights automation?

Hello friends! I’m practically new to home assistant but I’m trying to implement some automations for my house.

I’m trying to turn on a series of lights (Sonoff integration, seen as light entity) when my security camera system detects activity (Reolink integrated cameras, people detection sensor seen as binary sensor).

This is the integration I’m trying to improve:

alias: Luci Terrazza - Automazione serale
description: >-
  Accensione e spegnimento dopo 3 min automatico delle luci patio di notte
  quando rileata attività dalla telecamera patio.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.patio_persona
    to: "on"
    from: "off"
condition:
  - condition: sun
    before: sunrise
    before_offset: "1200"
    after: sunset
    after_offset: "1200"
  - condition: device
    type: is_off
    device_id: b4c90a06d2a287674ae370e54f77dcc1
    entity_id: light.luci_terrazza
    domain: light
action:
  - service: notify.notify
    data:
      title: Automazione Attivata
      message: >-
        L'automazione per l'accensione delle Luci Terrazza è stata attivata dal
        sensore {{ trigger.to_state.attributes.friendly_name }} alle {{
        now().strftime('%H:%M:%S') }}.
  - service: light.turn_on
    target:
      entity_id: light.luci_terrazza
    data: {}
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.patio_persona
        from: "on"
        to: "off"
        for:
          minutes: 3
  - service: light.turn_off
    target:
      entity_id: light.luci_terrazza
    data: {}
  - delay:
      seconds: 10

I would like that the automation starts at night when motion is detected, but only if the light is off (for exemple: if I turn manually on the light because I need to stay in the area for a long period, the automations mustn’t start). After the automation turns on the light, I would like that the light stay on for 3 minutes after the binary sensor doesn’t detect activity and, if the sensor detects new activity during these 3 minutes, the timer must stop and restart from 3 minutes when the sensor goes newly off.

Aside from some false positives (attributable to incorrect camera detection), with the automation I wrote about it often happens that the automation stops and the light stays on until I turn it off manually.

How can I fix it?

Well why does your automation stop? Have you looked at the traces?

From the last five traces, the only ones I can read now, there are two kinds of error:

I think that the error in the first screen (related to the wait_for_trigger function) causes the light to stay on but I also can’t understand the second one.

The second one is because of automation modes.
Let’s say the motion sensor is on for 5 minutes, off for 2 minutes, then on again.

This triggers the automation, but it doesn’t turn off the lights because there wasn’t no motion for 3 minutes.
However since there was motion again, it triggers the automation again - but with the way that it is currently set up (Single), if an automation is already running, it doesn’t let any more run, so it gets cancelled.

There’s many other ways to make a lights automation, including having two separate or one combined that triggers on no motion for a duration instead of waiting for no motion, and having one that uses the Restart mode with a fixed delay to handle keeping the lights on for a duration.

The following single automation turns the light on/off based on motion. When motion is detected the light is turned on only if it’s currently off. Similarly it turns the light off only if it’s currently on. The notification is sent only when the light is turned on. It relies exclusively on triggers and works correctly even with mode: single.

alias: Luci Terrazza - Automazione serale
trigger:
  - platform: state
    entity_id:
      - binary_sensor.patio_persona
    to: "on"
    from: "off"
    variables:
      is_true: "{{ is_state('light.luci_terrazza', 'off') }}"
  - platform: state
    entity_id:
      - binary_sensor.patio_persona
    to: "off"
    from: "on"
    for:
      minutes: 3
    variables:
      is_true: "{{ is_state('light.luci_terrazza', 'on') }}"
condition:
  - condition: sun
    before: sunrise
    before_offset: "1200"
    after: sunset
    after_offset: "1200"
  - condition: template
    value_template: "{{ is_true }}"
action:
  - service: 'light.turn_{{ trigger.to_state.state }}'
    target:
      entity_id: light.luci_terrazza
  - condition: "{{ trigger.to_state.state == 'on' }}"
  - service: notify.notify
    data:
      title: Automazione Attivata
      message: >-
        L'automazione per l'accensione delle Luci Terrazza è stata attivata dal
        sensore {{ trigger.to_state.attributes.friendly_name }} alle {{
        now().strftime('%H:%M:%S') }}.
1 Like

Thank you for the clarification! I had read about the operation of the modes in the HA guides but hadn’t connected it to being the cause of the second error.

Thank you 123!
I tried your version of the automation, and it seems to work well! After being detected by the sensor, the automation starts, and by getting detected again after 2 minutes, the 3-minute timer for the “off” state restarts smoothly.
I still have a lot to learn, especially when it comes to templates! :sweat_smile:

I would greatly appreciate your help with a very similar automation. :pray:

In the garden surrounding my house, I have a series of cameras with which I’m trying to control the activation of garden lights. I would like some automations, as done with the one you proposed, to handle the timed activation and deactivation of lights based on the state of certain motion sensors. The automations I’ve written (one for each light entity, as they are located in different areas of the garden and need to be triggered by different cameras) are mostly like the following:

alias: Lampioncini Giardino Stalla - Automazione serale
description: >-
  Accensione e spegnimento dopo 3 min automatico delle luci giardino stalla di
  notte quando rileata attività dalle telecamere B2 a C2. L'automazione non
  viene sospesa dall'accensione manuale della luce (se a luce accesa manualmente
  viene rilevata attività dopo 3 minuti la luce si spegne lo stesso).
trigger:
  - platform: state
    entity_id:
      - binary_sensor.b2_veicolo
    to: "on"
    from: "off"
  - platform: state
    entity_id:
      - binary_sensor.b3_veicolo
    to: "on"
    from: "off"
  - platform: state
    entity_id:
      - binary_sensor.c1_veicolo
    to: "on"
    from: "off"
  - platform: state
    entity_id:
      - binary_sensor.c2_veicolo
    to: "on"
    from: "off"
  - platform: state
    entity_id:
      - binary_sensor.b2_persona
    to: "on"
    from: "off"
  - platform: state
    entity_id:
      - binary_sensor.b3_persona
    to: "on"
    from: "off"
  - platform: state
    entity_id:
      - binary_sensor.c1_persona
    to: "on"
    from: "off"
  - platform: state
    entity_id:
      - binary_sensor.c2_persona
    to: "on"
    from: "off"
condition:
  - condition: sun
    before: sunrise
    before_offset: "1200"
    after: sunset
    after_offset: "1200"
  - condition: device
    type: is_off
    device_id: d37471fee17711f26e5d5ef5759f9360
    entity_id: 610237bb822c63e3ba44943d25871157
    domain: light
action:
  - service: notify.notify
    data:
      title: Automazione Attivata
      message: >-
        L'automazione per l'accensione dei Lampioncini Giardino Stalla è stata
        attivata dal sensore {{ trigger.to_state.attributes.friendly_name }}
        alle {{ now().strftime('%H:%M:%S') }}.
  - service: light.turn_on
    target:
      entity_id: light.lampioncini_giardino_stalla
    data: {}
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.b2_veicolo
        from: "on"
        to: "off"
        for:
          minutes: 3
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.b3_veicolo
        from: "on"
        to: "off"
        for:
          minutes: 3
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.c1_veicolo
        from: "on"
        to: "off"
        for:
          minutes: 3
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.c2_veicolo
        from: "on"
        to: "off"
        for:
          minutes: 3
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.b2_persona
        from: "on"
        to: "off"
        for:
          minutes: 3
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.b3_persona
        from: "on"
        to: "off"
        for:
          minutes: 3
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.c1_persona
        from: "on"
        to: "off"
        for:
          minutes: 3
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.c2_persona
        from: "on"
        to: "off"
        for:
          minutes: 3
  - service: light.turn_off
    target:
      entity_id: light.lampioncini_giardino_stalla
    data: {}
  - delay:
      seconds: 10

These automations are causing similar issues for me, as they start but don’t stop.
I think it’s because once some sensors detect activity, HA waits for a double state change (from OFF to ON, and then from ON to OFF) on all the sensors specified in the trigger, not just those that have actually detected activity since the automation started. Am I right?
Is there a way to adapt the layout you proposed to this use case?

Use the same principle that I employed in my first example. However, this version of the automation will be monitoring multiple binary_sensors so there’s a small chance that the automation can be triggered while it’s still busy executing actions due to a previous trigger. To avoid interrupting the automation while it’s busy, set the automation’s mode to parallel.

alias: Lampioncini Giardino Stalla - Automazione serale
mode: parallel
trigger:
  - platform: state
    entity_id:
      - binary_sensor.b2_veicolo
      - binary_sensor.b3_veicolo
      - binary_sensor.c1_veicolo
      - binary_sensor.c2_veicolo
      - binary_sensor.b2_persona
      - binary_sensor.b3_persona
      - binary_sensor.c1_persona
      - binary_sensor.c2_persona
    to: "on"
    from: "off"
    variables:
      is_true: "{{ is_state('light.lampioncini_giardino_stalla', 'off') }}"
  - platform: state
    entity_id:
      - binary_sensor.b2_veicolo
      - binary_sensor.b3_veicolo
      - binary_sensor.c1_veicolo
      - binary_sensor.c2_veicolo
      - binary_sensor.b2_persona
      - binary_sensor.b3_persona
      - binary_sensor.c1_persona
      - binary_sensor.c2_persona
    to: "off"
    from: "on"
    for:
      minutes: 3
    variables:
      is_true: "{{ is_state('light.lampioncini_giardino_stalla', 'on') }}"
condition:
  - condition: sun
    before: sunrise
    before_offset: "1200"
    after: sunset
    after_offset: "1200"
  - condition: template
    value_template: "{{ is_true }}"
action:
  - service: 'light.turn_{{ trigger.to_state.state }}'
    target:
      entity_id: light.lampioncini_giardino_stalla
  - condition: "{{ trigger.to_state.state == 'on' }}"
  - service: notify.notify
    data:
      title: Automazione Attivata
      message: >-
        L'automazione per l'accensione dei Lampioncini Giardino Stalla è stata
        attivata dal sensore {{ trigger.to_state.attributes.friendly_name }}
        alle {{ now().strftime('%H:%M:%S') }}.

NOTE

The two automations are nearly identical and the only difference is that each one uses different binary_sensors to control a different light. If you wish, it’s possible to combine the two automations into one by simply using a variable in the trigger to identify which light is to be controlled.

alias: Example - Automazione serale
mode: parallel
trigger:
  - platform: state
    entity_id:
      - binary_sensor.patio_persona
    to: "on"
    from: "off"
    variables:
      light_id: light.luci_terrazza
      is_true: "{{ is_state(light_id, 'off') }}"
  - platform: state
    entity_id:
      - binary_sensor.b2_veicolo
      - binary_sensor.b3_veicolo
      - binary_sensor.c1_veicolo
      - binary_sensor.c2_veicolo
      - binary_sensor.b2_persona
      - binary_sensor.b3_persona
      - binary_sensor.c1_persona
      - binary_sensor.c2_persona
    to: "on"
    from: "off"
    variables:
      light_id: light.lampioncini_giardino_stalla
      is_true: "{{ is_state(light_id, 'off') }}"
  - platform: state
    entity_id:
      - binary_sensor.patio_persona
    to: "off"
    from: "on"
    for:
      minutes: 3
    variables:
      light_id: light.luci_terrazza
      is_true: "{{ is_state(light_id, 'on') }}"
  - platform: state
    entity_id:
      - binary_sensor.b2_veicolo
      - binary_sensor.b3_veicolo
      - binary_sensor.c1_veicolo
      - binary_sensor.c2_veicolo
      - binary_sensor.b2_persona
      - binary_sensor.b3_persona
      - binary_sensor.c1_persona
      - binary_sensor.c2_persona
    to: "off"
    from: "on"
    for:
      minutes: 3
    variables:
      light_id: light.lampioncini_giardino_stalla
      is_true: "{{ is_state(light_id, 'on') }}"
condition:
  - condition: sun
    before: sunrise
    before_offset: "1200"
    after: sunset
    after_offset: "1200"
  - condition: template
    value_template: "{{ is_true }}"
action:
  - service: 'light.turn_{{ trigger.to_state.state }}'
    target:
      entity_id: '{{ light_id }}'
  - condition: "{{ trigger.to_state.state == 'on' }}"
  - service: notify.notify
    data:
      title: Automazione Attivata
      message: >-
        L'automazione per l'accensione dei {{ state_attr(light_id, 'friendly_name') }} è stata
        attivata dal sensore {{ trigger.to_state.attributes.friendly_name }}
        alle {{ now().strftime('%H:%M:%S') }}.

@123

Sorry if I bother you. :sweat_smile:
I haven’t had the time to check the correct functioning of the two versions of the automation for multiple sensors yet. However, I’ve just noticed that the automation for the single sensor you suggested generates two issues that I didn’t notice this morning when I tested it. Considering the very similar structure, I believe these issues may also occur in the multi-sensor automation.

A) When the light is turned off, the binary sensor detects a false positive for 2-3 seconds (likely due to some reflection or movement on the balcony), causing the automation to trigger again and the light to turn back on immediately after turning off. Would it be possible to add a delay of 5-10 seconds at the end of all actions? I tried adding the following as the last action:

  - delay:
      seconds: 10

but it has no effect. The light continues to turn back on immediately after automatic shutoff.

B) If I manually turn on the light (via Alexa, the switch, or any other method) and the sensor detects movement in the area, the automation still triggers and turns off the light after 3 minutes of inactivity. Since this is a living area of the house even at night, I would like the automation not to intervene when the light is manually turned on but only work when the camera detects people with the light off. For this reason, in the first version of my automation, I had set the condition to check the status of the light device (OFF). Is there a way to implement this condition without altering the conditions you set in your version of the automation?

Thank you very much for the great help you’re providing! :pray:

Are your security cameras being used to detect motion? Can you adjust their sensitivity to avoid producing false positives?

Adding a delay at the end of the automation may be effective if mode is single but definitely not if it’s anything else like parallel.

The challenge is to determine the difference between a false-positive state-change and a valid one. From what you have told me, I don’t see how the automation can differentiate between the two. They are both simply a state-change from off to on. The best option would be to eliminate the false-positives.

Although it’s possible to achieve it, I don’t currently have additional free time for your project.

Thank you very much nonetheless! :slightly_smiling_face:
Starting from your automation versions, I’ll try to address the issues.
If anyone else has any ideas for automation, I would appreciate it if they could lend a hand.
Anyway, if I manage to solve it, I’ll let you know!

EDIT:

Perhaps, after several attempts and bug fixes, I have found a solution to the problem. I have implemented the use of a boolean variable whose state is manually changed upon activation and deactivation of the automation, and I have defined conditions that link the turning on and off of the light to the state of the variable. I have added a 10-second interval at the end to prevent it from auto-triggering due to false positives when the light is turned off. Additionally, I have reduced the sensitivity of the cameras enough to eliminate false positives (or at least minimize them). I am testing the automation for all outdoor lights, and I will let you know if there are any further modifications needed. In the meantime, I am attaching the code I’m using:

alias: Luci Terrazza - Automazione serale (beta 2)
description: >-
  Accensione e spegnimento dopo 3 min automatico delle luci patio di notte
  quando rilevata attività dalla telecamera patio. L'automazione non viene
  sospesa dall'accensione manuale della luce (se la luce è accesa manualmente e
  viene rilevata attività dopo 3 minuti, la luce si spegne lo stesso).
trigger:
  - platform: state
    entity_id: binary_sensor.patio_persona
    to: "on"
    from: "off"
    variables:
      is_true: "{{ is_state('light.luci_terrazza', 'off') }}"
  - platform: state
    entity_id: binary_sensor.patio_persona
    to: "off"
    from: "on"
    for:
      minutes: 3
    variables:
      is_true: "{{ is_state('light.luci_terrazza', 'on') }}"
condition:
  - condition: sun
    before: sunrise
    before_offset: "1200"
    after: sunset
    after_offset: "1200"
  - condition: template
    value_template: "{{ is_true }}"
action:
  - choose:
      - conditions:
          - >-
            {{ states('input_boolean.accensione_automatica_luci_terrazza') ==
            'on' }}
          - "{{ is_state('light.luci_terrazza', 'off') }}"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.luci_terrazza
            data: {}
          - service: notify.notify
            data:
              title: Automazione Attivata
              message: >-
                L'automazione per l'accensione delle Luci Terrazza è stata
                attivata dal sensore {{
                trigger.to_state.attributes.friendly_name }} alle {{
                now().strftime('%H:%M:%S') }}.
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.accensione_automatica_luci_terrazza
            data: {}
      - conditions:
          - >-
            {{ states('input_boolean.accensione_automatica_luci_terrazza') ==
            'off' }}
          - "{{ is_state('light.luci_terrazza', 'on') }}"
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.luci_terrazza
            data: {}
          - delay:
              seconds: 10
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.accensione_automatica_luci_terrazza
            data: {}
mode: single

Please consider marking my post above with the Solution tag. It solves the problem mentioned in your topic’s first post and adding the Solution tag will help other users find it more quickly. It will be useful for most users because the false positives you are experiencing are unique to your situation.


The “10 second” delay you added to the end of automation is what your motion detector should be doing (not your automation). It’s commonly known as a “cooldown” period and helps to avoid ‘false positives’.

One strategy may be to create a Template Binary Sensor, to act as a virtual motion detector for your camera-based motion detector, and experiment with its delay_on and delay_off options to create a cooldown period.


The use of an Input Boolean, to keep track of who controlled the light (the automation or the user) is a common software pattern. However the one small drawback is that an Input Boolean can also be controlled from the UI (by a user). Hiding the Input Boolean from the dashboard is generally effective. However, what’s better is a Trigger-based Template Binary Sensor, controlled by a custom event that only your automation uses, because its value cannot be changed via the dashboard.

Certainly! Post tagged as a solution to the topic.

Thank you for the additional tips on improving the automation. As you guessed, I have created corresponding auxiliary input booleans for various outdoor light automation, but I haven’t added them to the dashboard (to view their status and potentially change it, I need to go to the section related to helpers). Additionally, I’ve created a dedicated automation that resets their state at 10 in the morning to anticipate any potential errors that might block the automation during the night.

Indeed, a dynamic approach based on the state of template binary sensors would be an even more elegant solution, but I’m unsure how to set up the code and adapt the automations… :sweat_smile:

1 Like