Basic auto Light on when there is motion refuses to turn on Light

i know my light is working fine because i can manually call the service On or Off in Dev tab:

BUT when i set it up in an automation, the Living Room Light refuses to turn on even though i see it as executed

here’s my code when i used the GUI to set things up. what seems to be the problem?

alias: auto living room door light to on when motion or door is opened
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.besense_home_security_motion_detection
    to: "on"
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.light_living_room
    data:
      brightness_pct: 100
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.door_living_room_contact
          - binary_sensor.besense_home_security_motion_detection
        to: "off"
        for:
          hours: 0
          minutes: 10
          seconds: 0
    timeout:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.light_living_room
mode: single

Your timeout could be a problem. Since it is at 0, it would go right past the ‘wait for trigger’ and move to the off action. So your light would be turning on and then immediately turning off. Looking at the history of the light will give you a better clue.

Triggered doesn’t mean that the whole automation has been executed. Look at the debug trace.

Not sure what the timeout part is doing. Remove all of it, and I think it will work.

Remove this

    timeout:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0

i removed and happy to say it works.

final question please. will this turn off the light when both triggers are OFF or only 1 trigger?
i want to have them both to be OFF for the light to turn off.

  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.door_living_room_contact
          - binary_sensor.besense_home_security_motion_detection
        to: "off"

Triggers are always OR. So if one changes to ‘off’, the automation action continues.
Use a trigger template instead.

As mention a trigger template can do this.

If you are not familiar with YAML, then a more simple implementation will be to make 2 conditions checking they are “off”. Conditions are “AND”. Can easily be made in the GUI.

i looked at the template guidance but very confused how to add multiple entities. the example was just 1 entity
here’s my yaml:

alias: auto dining room lights on
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.motion_dome_dining_home_security_motion_detection
      - binary_sensor.keypad_v2_home_security_motion_detection
      - binary_sensor.dining_person_motion
      - binary_sensor.kitchen_person_motion
    to: "on"
condition: []
action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.light_dining_2
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.motion_dome_dining_home_security_motion_detection
          - binary_sensor.keypad_v2_home_security_motion_detection
          - binary_sensor.dining_person_motion
          - binary_sensor.kitchen_person_motion
        to: "off"
        for:
          hours: 0
          minutes: 10
          seconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.light_dining_2
mode: single

so any sensors will turn on the lights.
but to turn off lights, ALL of them must be off. as of now, lights turn off when any of sensors go off. :frowning:

if i have to guess, would it be like this?

      - platform: template
        value_template: "{% if is_state('binary_sensor.motion_dome_dining_home_security_motion_detection','binary_sensor.keypad_v2_home_security_motion_detection','binary_sensor.dining_person_motion','binary_sensor.kitchen_person_motion' 'off') %}true{% endif %}"
        to: "off"
        for:
          hours: 0
          minutes: 10
          seconds: 0

Always test templates in Developer Tools —> Template if you aren’t sure. If you copy your suggested code into Dev Tools, you will get a syntax error.

Try this instead:


platform: template
value_template: >-
  {% set motion = expand(['binary_sensor.one','binary_sensor.two','binary_sensor.three'])
  |selectattr('state', 'eq', 'off')
  |sort(attribute='last_changed', reverse=true) %}
  {{ now() - motion[0].last_changed > timedelta(minutes=10) if motion != [] else false }}

Also, you don’t see a field for: in the automation editor after selecting template as trigger platform. If you want use this, create a group and set it to all: true.

all - boolean (optional, default: false)
Only available for binary_sensor, light and switch groups. Set this to true if the group state should only turn on if all grouped entities are on.
https://www.home-assistant.io/integrations/group/#all

This way you can use platform: state as trigger platform in combination with for:
However (also with regard to any wait_for_trigger), please note that the for option will not survive a HA restart or reload of that automation. To avoid that, use a helper (e.g. input_number) and alter the for: field like that:

for:
  minutes: "{{ states('input_number.name_of_your_helper') |int(10) }}"