Having trouble configuring my hallway nightlight automation

Hi, I put together an automation that turns on the hallway light in case my young kids get up and need to use the bathroom at night.

I have it doing the following:

When one of two motion sensors are triggered, and if the hallway light is off, if one of the sensors reads 5 lux or below and it is between 11pm and 5am, then it will turn the hallway light on to 1%.

Then it waits 5 minutes until after both sensors no longer sense motion, then it turns the hallway lights off.

The problem I am having is that even during those hours, after we put the kids to bed and leave the hallway, it turns the lights off (runs the second part of the automation) - so that portion is not basing on the conditions of the earlier part of the automation.

SO, do I need to put the same conditions in for the second part of that script? Or is there a better way to write this one? The YAML is below. Thank you in advance for any suggestions you may have.

-David

alias: Upstairs Hallway Light Nightlight
description: ""
trigger:
  - type: occupied
    platform: device
    device_id: 68faaf0f5a59cb30107c523211b8999b
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
    domain: binary_sensor
  - type: occupied
    platform: device
    device_id: c1b63420a875d0684491d17dc34c277a
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
    domain: binary_sensor
condition:
  - condition: and
    conditions:
      - condition: device
        type: is_off
        device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
        entity_id: light.upstairs_hallway_lights
        domain: light
      - type: is_illuminance
        condition: device
        device_id: 68faaf0f5a59cb30107c523211b8999b
        entity_id: sensor.signify_netherlands_b_v_sml003_illuminance
        domain: sensor
        below: 5
      - condition: time
        after: "23:00:00"
        before: "05:00:00"
        enabled: true
action:
  - type: turn_on
    device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
    entity_id: light.upstairs_hallway_lights
    domain: light
    brightness_pct: 1
  - wait_for_trigger:
      - type: not_occupied
        platform: device
        device_id: 68faaf0f5a59cb30107c523211b8999b
        entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
        domain: binary_sensor
        for:
          hours: 0
          minutes: 5
          seconds: 0
  - wait_for_trigger:
      - type: not_occupied
        platform: device
        device_id: c1b63420a875d0684491d17dc34c277a
        entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
        domain: binary_sensor
        for:
          hours: 0
          minutes: 5
          seconds: 0
  - type: turn_off
    device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
    entity_id: light.upstairs_hallway_lights
    domain: light
mode: single

Oh, and one other part - I currently have this as a separate automation, unless it makes more sense to add it to the one above, but when either motion detector is tripped, and when the master bedroom is closed (door sensor), it turns a switch on so we know one of the kids is up.

Thanks!!

alias: Master Bedroom notification light
description: ""
trigger:
  - type: occupied
    platform: device
    device_id: c1b63420a875d0684491d17dc34c277a
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
    domain: binary_sensor
  - type: occupied
    platform: device
    device_id: 68faaf0f5a59cb30107c523211b8999b
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
    domain: binary_sensor
condition:
  - condition: and
    conditions:
      - condition: device
        type: is_off
        device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
        entity_id: light.upstairs_hallway_lights
        domain: light
      - type: is_not_open
        condition: device
        device_id: c13bd69b3d14a51fbd090b8fa67278c4
        entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
        domain: binary_sensor
      - condition: time
        after: "22:00:00"
        before: "05:00:00"
        weekday:
          - sat
          - fri
          - thu
          - wed
          - tue
          - mon
          - sun
action:
  - type: turn_on
    device_id: 2f38853dd14a6815e0962791aa95badb
    entity_id: switch.signify_netherlands_b_v_lom010_switch
    domain: switch
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - type: turn_off
    device_id: 2f38853dd14a6815e0962791aa95badb
    entity_id: switch.signify_netherlands_b_v_lom010_switch
    domain: switch
mode: single

Are you saying that you leave the hallway light on at full power after putting them to bed and it turns off, which it shouldn’t?

FYI, you don’t need the “condition: and” under the condition section, as it defaults to checking all conditions.

But I’d recommend putting the two wait_for_triggers into one,

  - wait_for_trigger:
      - condition: and
        conditions:
         - type: not_occupied
           platform: device
           device_id: 68faaf0f5a59cb30107c523211b8999b
           entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
           domain: binary_sensor
           for:
             hours: 0
             minutes: 5
             seconds: 0
         - type: not_occupied
           platform: device
           device_id: c1b63420a875d0684491d17dc34c277a
           entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
           domain: binary_sensor
           for:
             hours: 0
             minutes: 5
             seconds: 0

Also not sure if that before and after would work in the time condition, as its saying less than 5 AND more than 23. Try:

- condition: template
  value_template: "{{5<now().hour<=23}}"

Yes, correct. When we put them to bed, we leave the hallway light on, until we go to bed. I don’t want anything triggering (either on or off) until the hallway light is turned off at night and the light level is low.

Your suggestions makes sense, though - I will give that a try!

Thanks so much!!

1 Like

Whoops, the time template should actually be

- condition: template
  value_template: "{{now().hour<5 or now().hour>=23}}"

I believe your automation may be triggering before you turn on the hallway light to put them to bed, so it’s turning off after 5min of unoccupied.

Maybe replace the “device… is_off” in the condition section with (the below check is based on a scale of 0-256, not 0-100 percent)

- condition: numeric_state
  entity_id: light.upstairs_hallway_lights
  attribute: brightness
  below: 5

When I use the following code (copied from your example), I get this error message:

Message malformed: required key not provided @ data['action'][1]['wait_for_trigger'][0]['platform']
  - wait_for_trigger:
    - condition: and
      conditions:
      - type: not_occupied
        platform: device
        device_id: 68faaf0f5a59cb30107c523211b8999b
        entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
        domain: binary_sensor
        for:
          hours: 0
          minutes: 5
          seconds: 0
      - type: not_occupied
        platform: device
        device_id: c1b63420a875d0684491d17dc34c277a
        entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
        domain: binary_sensor
        for:
          hours: 0
          minutes: 5
          seconds: 0

Not sure what that means…

Thanks!!
-David

OK, seems like it doesn’t like:

  - condition: and
      conditions:

I was able to remove the second “wait for trigger” but can’t add the Condition AND

Please let me know if you have any ideas.

Thanks!

I usually use templates, but yeah it apparently doesn’t like “and” under wait_for_trigger.

This should work:

  - wait_for_trigger:
      - platform: template
        value_template: >-
          {{states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.state=='off' &&
          as_timestamp(now())-as_timestamp(states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.last_changed)>300
          && states.binary_sensor.signify_netherlands_b_v_sml003_occupancy_2.state=='off' &&
          as_timestamp(now())-as_timestamp(states.binary_sensor.signify_netherlands_b_v_sml003_occupancy_2.last_changed)>300}}

checks that both sensors are unoccupied (off) and have been for at least 300 seconds (5min)

Argh, it still isn’t liking that. Here is my full updated code as of right now. This has all the changes minus the last one you gave me…

alias: Upstairs Hallway Light Nightlight
description: ""
trigger:
  - type: occupied
    platform: device
    device_id: 68faaf0f5a59cb30107c523211b8999b
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
    domain: binary_sensor
  - type: occupied
    platform: device
    device_id: c1b63420a875d0684491d17dc34c277a
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
    domain: binary_sensor
condition:
  - condition: numeric_state
    entity_id: light.upstairs_hallway_lights
    attribute: brightness
    below: 5
  - type: is_illuminance
    condition: device
    device_id: 68faaf0f5a59cb30107c523211b8999b
    entity_id: sensor.signify_netherlands_b_v_sml003_illuminance
    domain: sensor
    below: 5
  - condition: template
    value_template: "{{now().hour<5 or now().hour>=23}}\""
action:
  - type: turn_on
    device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
    entity_id: light.upstairs_hallway_lights
    domain: light
    brightness_pct: 1
  - wait_for_trigger:
      - type: not_occupied
        platform: device
        device_id: 68faaf0f5a59cb30107c523211b8999b
        entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
        domain: binary_sensor
        for:
          hours: 0
          minutes: 5
          seconds: 0
      - type: not_occupied
        platform: device
        device_id: c1b63420a875d0684491d17dc34c277a
        entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
        domain: binary_sensor
        for:
          hours: 0
          minutes: 5
          seconds: 0
  - type: turn_off
    device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
    entity_id: light.upstairs_hallway_lights
    domain: light
mode: single

template didn’t like &&, had to switch to “and”.

here is full revisions (removed an extra quotation you added in time template too)

alias: Upstairs Hallway Light Nightlight
description: ""
trigger:
  - type: occupied
    platform: device
    device_id: 68faaf0f5a59cb30107c523211b8999b
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
    domain: binary_sensor
  - type: occupied
    platform: device
    device_id: c1b63420a875d0684491d17dc34c277a
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
    domain: binary_sensor
condition:
  - condition: numeric_state
    entity_id: light.upstairs_hallway_lights
    attribute: brightness
    below: 5
  - type: is_illuminance
    condition: device
    device_id: 68faaf0f5a59cb30107c523211b8999b
    entity_id: sensor.signify_netherlands_b_v_sml003_illuminance
    domain: sensor
    below: 5
  - condition: template
    value_template: "{{now().hour<5 or now().hour>=23}}"
action:
  - type: turn_on
    device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
    entity_id: light.upstairs_hallway_lights
    domain: light
    brightness_pct: 1
  - wait_for_trigger:
      - platform: template
        value_template: >-
          {{states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.state=='off' and
          as_timestamp(now())-as_timestamp(states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.last_changed)>300
          and states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.state=='off' and
          as_timestamp(now())-as_timestamp(states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.last_changed)>300}}
  - type: turn_off
    device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
    entity_id: light.upstairs_hallway_lights
    domain: light
mode: single

OK, no errors there. That looks good…thank you so much for this! I’ll let it run and make sure it works as intended.

One last question for you. I would like to put the indicator light in on this same automation, if possible - instead of making it a separate automation. Would you suggest that, or just leave it as it’s own automation?

This part would turn on a switch for 30 seconds in the master bedroom, only when the master bedroom door sensor is closed, and when the above automation runs. It is just so we know when the kids get up at night.

alias: Master Bedroom notification light
description: ""
trigger:
  - type: occupied
    platform: device
    device_id: c1b63420a875d0684491d17dc34c277a
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
    domain: binary_sensor
  - type: occupied
    platform: device
    device_id: 68faaf0f5a59cb30107c523211b8999b
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
    domain: binary_sensor
condition:
  - condition: and
    conditions:
      - condition: device
        type: is_off
        device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
        entity_id: light.upstairs_hallway_lights
        domain: light
      - type: is_not_open
        condition: device
        device_id: c13bd69b3d14a51fbd090b8fa67278c4
        entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
        domain: binary_sensor
      - condition: time
        after: "22:00:00"
        before: "05:00:00"
        weekday:
          - sat
          - fri
          - thu
          - wed
          - tue
          - mon
          - sun
action:
  - type: turn_on
    device_id: 2f38853dd14a6815e0962791aa95badb
    entity_id: switch.signify_netherlands_b_v_lom010_switch
    domain: switch
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - type: turn_off
    device_id: 2f38853dd14a6815e0962791aa95badb
    entity_id: switch.signify_netherlands_b_v_lom010_switch
    domain: switch
mode: single

I’m sure this part is more complex than it needs to be, especially since it is currently its own automation.

Thanks again.

You can simply insert it into former automation (all transplanted code is contained within the ‘if’ action)

alias: Upstairs Hallway Light Nightlight
description: ""
trigger:
  - type: occupied
    platform: device
    device_id: 68faaf0f5a59cb30107c523211b8999b
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
    domain: binary_sensor
  - type: occupied
    platform: device
    device_id: c1b63420a875d0684491d17dc34c277a
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
    domain: binary_sensor
condition:
  - condition: numeric_state
    entity_id: light.upstairs_hallway_lights
    attribute: brightness
    below: 5
  - type: is_illuminance
    condition: device
    device_id: 68faaf0f5a59cb30107c523211b8999b
    entity_id: sensor.signify_netherlands_b_v_sml003_illuminance
    domain: sensor
    below: 5
  - condition: template
    value_template: "{{now().hour<5 or now().hour>=23}}"
action:
  - type: turn_on
    device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
    entity_id: light.upstairs_hallway_lights
    domain: light
    brightness_pct: 1
  - if:
      - type: is_not_open
        condition: device
        device_id: c13bd69b3d14a51fbd090b8fa67278c4
        entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
        domain: binary_sensor
    then:
      - type: turn_on
        device_id: 2f38853dd14a6815e0962791aa95badb
        entity_id: switch.signify_netherlands_b_v_lom010_switch
        domain: switch
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - type: turn_off
        device_id: 2f38853dd14a6815e0962791aa95badb
        entity_id: switch.signify_netherlands_b_v_lom010_switch
        domain: switch
  - wait_for_trigger:
      - platform: template
        value_template: >-
          {{states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.state=='off' and
          as_timestamp(now())-as_timestamp(states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.last_changed)>300
          and states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.state=='off' and
          as_timestamp(now())-as_timestamp(states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.last_changed)>300}}
  - type: turn_off
    device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
    entity_id: light.upstairs_hallway_lights
    domain: light
mode: single

Because it’s and and not &&.

I would also strongly suggest you avoid using all these device triggers and conditions and rather use entities and states directly. It will be a lot cleaner, simpler and more maintainable. There are various posts on that particular topic if you want to know more.

Agreed. So, for example, this:

trigger:
  - type: occupied
    platform: device
    device_id: c1b63420a875d0684491d17dc34c277a
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
    domain: binary_sensor
  - type: occupied
    platform: device
    device_id: 68faaf0f5a59cb30107c523211b8999b
    entity_id: binary_sensor.signify_netherlands_b_v_sml003_occupancy
    domain: binary_sensor

becomes this:

trigger:
  - platform: state
    entity_id: 
      - binary_sensor.signify_netherlands_b_v_sml003_occupancy
      - binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
    to: 'on'
1 Like

Hmmm, I will paste my code below, but something is broken now - the motion detectors trigger, but the lights don’t turn on. Any help would be appreciated!!

alias: Upstairs Hallway Light Nightlight
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.signify_netherlands_b_v_sml003_occupancy
      - binary_sensor.signify_netherlands_b_v_sml003_occupancy_2
    to: "on"
condition:
  - condition: numeric_state
    entity_id: light.upstairs_hallway_lights
    attribute: brightness
    below: 5
  - type: is_illuminance
    condition: device
    device_id: 68faaf0f5a59cb30107c523211b8999b
    entity_id: sensor.signify_netherlands_b_v_sml003_illuminance
    domain: sensor
    below: 5
  - condition: template
    value_template: "{{now().hour<5 or now().hour>=23}}"
action:
  - type: turn_on
    device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
    entity_id: light.upstairs_hallway_lights
    domain: light
    brightness_pct: 1
  - if:
      - type: is_not_open
        condition: device
        device_id: c13bd69b3d14a51fbd090b8fa67278c4
        entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
        domain: binary_sensor
    then:
      - type: turn_on
        device_id: 2f38853dd14a6815e0962791aa95badb
        entity_id: switch.signify_netherlands_b_v_lom010_switch
        domain: switch
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - type: turn_off
        device_id: 2f38853dd14a6815e0962791aa95badb
        entity_id: switch.signify_netherlands_b_v_lom010_switch
        domain: switch
  - wait_for_trigger:
      - platform: template
        value_template: >-
          {{states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.state=='off'
          and
          as_timestamp(now())-as_timestamp(states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.last_changed)>300
          and
          states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.state=='off'
          and
          as_timestamp(now())-as_timestamp(states.binary_sensor.signify_netherlands_b_v_sml003_occupancy.last_changed)>300}}
  - type: turn_off
    device_id: 6ec14e2f80a802049b7f8ddb81cf18b2
    entity_id: light.upstairs_hallway_lights
    domain: light
mode: single

Look at the automation trace and see what’s stopping it. My guess is the illuminance sensor is above 5 as it’s the daytime; and it’s also not between 23:00 and 05:00.

I guess the problem with the first condition is that when the light is off, there is no brightness attribute, so this won’t ever trigger unless the hall light is on at 1%.

Change the numeric_state condition back to your original is_off condition and it’ll bring your code back to the way it originally worked before, but with some updated parts and the second automation added in.

Without knowing all the events that are happening in your house, it’s hard to figure out how to stop your original problem of ‘turning off the hall lights when you want them to stay on.’ I still think you were triggering the automation before you turned on the hall lights to put them to bed, so the numeric_state attribute condition could be used to check if you split the code into two automations (one to turn on the lights, one to turn off), or using a bunch of if-else.

Like the guy above said, would have to look at the traces and full logbook and know what calls are being made to manually turn on your lights, etc.