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 %}