Help with Automation - Motion Lights Auto On/Off - Not turning off

I am having an issue with my motion lights script. Lights consistently turn on as expected, but most times lights do not turn off as they should. Anyone have any idea what the issue is? Here is the automation:

################################################
########## Motion Lights Auto On/Off ###########
################################################
  - id: motion_lights
    alias: Motion Lights
    trigger:
      - platform: state
        entity_id: 
          - binary_sensor.computer_room_motion
          - binary_sensor.living_room_motion
          - binary_sensor.kitchen_motion_sensor
          - binary_sensor.dining_room_motion_sensor
          - binary_sensor.craft_room_motion_sensor
          - binary_sensor.half_bathroom_motion_sensor
          - binary_sensor.poop_closet_motion
          - binary_sensor.master_bath_motion
          - binary_sensor.guest_bathroom_motion
          - binary_sensor.master_bedroom_motion
        to: 'on'
      - platform: state
        entity_id:
          - binary_sensor.guest_bathroom_motion
          - binary_sensor.master_bath_motion
          - binary_sensor.dining_room_motion_sensor
        to: 'off'
        for:
          minutes: 20
      - platform: state
        entity_id:
          - binary_sensor.computer_room_motion
          - binary_sensor.craft_room_motion_sensor
          - binary_sensor.living_room_motion
          - binary_sensor.master_bedroom_motion
        to: 'off'
        for:
          minutes: 15
      - platform: state
        entity_id:
          - binary_sensor.kitchen_motion_sensor
          - binary_sensor.half_bathroom_motion_sensor
          - binary_sensor.poop_closet_motion
        to: 'off'
        for:
          minutes: 2
    condition:
      ### Ensure Motion Lights aren't disabled
      - condition: state
        entity_id: input_boolean.block_all_motion_lights
        state: 'off'
      ### Ensure it is dark outside for certain lights
      - condition: template
        value_template: >
          {% set trigger_entity = trigger.entity_id %}
          {% if trigger_entity == 'binary_sensor.computer_room_motion' or trigger_entity == 'binary_sensor.living_room_motion' %}
            {% if is_state("input_boolean.dark_outside", "on") %}
              {% if trigger_entity == 'binary_sensor.living_room_motion' %}
                {% if is_state("sensor.harmony", "PowerOff") %}
                  true
                {% else %}
                  false
                {% endif %}
              {% else %}
                true
              {% endif %}
            {% else %}
              false
            {% endif %}
          {% else %}
            true
          {% endif %}
      - condition: template
        value_template: >
          {% set trigger_entity = trigger.entity_id %}
          {% if trigger_entity == 'binary_sensor.craft_room_motion_sensor' and is_state("input_boolean.disable_craft_room_motion", "on") %}
            false
          {% else %}
            true
          {% endif %}
      - condition: template
        value_template: >
          {% set trigger_entity = trigger.entity_id %}
          {% if trigger_entity == 'binary_sensor.master_bedroom_motion' and is_state("input_boolean.disable_master_bedroom_motion", "on") %}
            false
          {% else %}
            true
          {% endif %}
    action:
      ### Determine light/group that goes with motion detector
      - service_template: 'homeassistant.turn_{{trigger.to_state.state}}'
        data_template:
          entity_id: >
            {% set trigger_entity = trigger.entity_id %}
            {% set to_state = trigger.to_state.state %}
            {% if trigger_entity == 'binary_sensor.computer_room_motion' %}
              group.computer_room
            {% elif trigger_entity == 'binary_sensor.kitchen_motion_sensor' %}
              {% if to_state == 'off' %}
                group.kitchen
              {% else %}
                switch.kitchen_light
              {% endif %}
            {% elif trigger_entity == 'binary_sensor.dining_room_motion_sensor' %}
              group.dining_room
            {% elif trigger_entity == 'binary_sensor.craft_room_motion_sensor' %} 
              group.craft_room
            {% elif trigger_entity == 'binary_sensor.half_bathroom_motion_sensor' %} 
              {% if to_state == 'off' %}
                group.half_bath_light_fan
              {% else %}
                group.half_bath
              {% endif %}
            {% elif trigger_entity == 'binary_sensor.poop_closet_motion' %} 
              switch.poop_closet_light
            {% elif trigger_entity == 'binary_sensor.master_bath_motion' %} 
              {% if to_state == 'off' %}
                group.master_bath
              {% else %}
                switch.master_bath_light
              {% endif %}
            {% elif trigger_entity == 'binary_sensor.guest_bathroom_motion' %} 
              group.guest_bath
            {% elif trigger_entity == 'binary_sensor.master_bedroom_motion' %} 
              switch.master_light
            {% elif trigger_entity == 'binary_sensor.living_room_motion' %}
              switch.living_room_light
            {% else %}
              input_boolean.test1
            {% endif %}

Holy crap! :exploding_head:

I realize that sometimes templating is necessary to achieve a desired outcome when there is no other simpler solution.

However, the key concepts in that sentence are “necessary” and “no other simpler solution”. I think in this case the templating (while very impressive! :+1:) makes the automation WAY more complicated.

IMHO, it would be easier to break this up into different automations that you (or someone else) can troubleshoot when it doesn’t work right.

I’d be surprised if you’ll find anybody wiling to dive into that wall of text, but you never know. We do have some pretty good templating guru’s on here. :smiley:

1 Like

I think you are right. This was really a “let’s try and put as much as possible into a single automation” kind of thing. Trying to get away from having so many separate automations for similar tasks.

It seems like using trigger.to_state.entity_id instead of trigger.entity_id has improved things. I don’t really understand why.

Wow, that reminds me of C or Java programmers that jam as much as they can into one statement then expect some poor sod to figure it out later.

I am seeing the same issue, that is I have a motion detector that is able to turn the light on but I can’t see how to have if auto-off some period after no activity. Do you have a simplified example or can you point to one in the docs or somewhere?

Thanks

Yeah, I was on a kick of consolidating separate automations at the time. I ended up using separate automations for each room and everything seems to work as expected now. Here are a couple examples:

Motion in the half bathroom automatically turns on the lights. After no motion for 5 minutes, I turn off the light and fan:

  - id: half_bath_motion_lights
    alias: Half Bath Motion Lights
    trigger:
      - platform: state
        entity_id: binary_sensor.half_bathroom_motion_sensor
        to: 'on'
      - platform: state
        entity_id: binary_sensor.half_bathroom_motion_sensor
        to: 'off'
        for:
          minutes: 5
    condition:
      - condition: state
        entity_id: input_boolean.disable_all_motion_lights
        state: 'off'
    action:
      - service_template: 'homeassistant.turn_{{trigger.to_state.state}}'
        data_template:
          entity_id: >
            {% if trigger.to_state.state == 'off' %}
              group.half_bath_light_fan
            {% else %}
              group.half_bath
            {% endif %}

The computer room has multiple motions sensors (which are grouped together) and I only want the lights to automatically turn on when it is dark outside.

  - id: computer_room_motion_lights
    alias: Computer Room Motion Lights
    trigger:
      - platform: state
        entity_id: binary_sensor.computer_room_motion
        to: 'on'
      - platform: state
        entity_id: binary_sensor.computer_room_motion_desk
        to: 'on'
      - platform: state
        entity_id: group.computer_room_master_motion
        to: 'off'
        for:
          minutes: 10
    condition:
      - condition: state
        entity_id: input_boolean.disable_all_motion_lights
        state: 'off'
      - condition: state
        entity_id: input_boolean.dark_outside
        state: 'on'
      - condition: state
        entity_id: input_boolean.disable_computer_room_motion
        state: 'off'
    action:
      - service_template: 'homeassistant.turn_{{trigger.to_state.state}}'
        data:
          entity_id: group.computer_room

Thanks! My use of motion detectors is much more limited but I have it going now. Your example did help.