I have a light automation script that checks a couple of things before choosing whether to turn a light on/off or do nothing. I’d like to use change this script so I can pass it a group of lights.
Here’s the original script.
script:
light_automation:
sequence:
# only run if light automation is enabled
- condition: state
entity_id: input_boolean.light_automation
state: 'on'
# don't run if light in use by occupant
- condition: template
value_template: >-
{% if ignore_user_state == 'true' %}
true
{% elif is_state('binary_sensor.' + light_name, 'off') %}
true
{% else %}
false
{% endif %}
# only run if new state is on and light automation switch is on (sun has set)
# or light is turning off
- condition: template
value_template: >-
{% if new_state == 'on' and is_state('binary_sensor.auto_light_on','on') %}
true
{% elif new_state == 'on' and is_state('binary_sensor.auto_light_on','off') %}
false
{% else %}
true
{% endif %}
# turn off light user state automation
- service: automation.turn_off
data_template:
entity_id: "automation.set_{{ light_name }}_user_state"
- service_template: >-
{% if new_state == 'on' %}
light.turn_on
{% else %}
light.turn_off
{% endif %}
data_template:
entity_id: "light.{{ light_name }}"
brightness_pct: "{{ brightness }}"
# delay to allow state changes to register
- delay: '0:00:02'
# turn light user state automation back on
- service: automation.turn_on
data_template:
entity_id: "automation.set_{{ light_name }}_user_state"
Here is how I currently call the script.
service: script.light_automation
data_template:
entity_id: light.living_room_pot_lights
new_state: 'on'
brightness: 50
ignore_user_state: 'true'
Here is what I have so far. Where I’m stuck is with the data template section.
{%- for entity_id in states.group.outside_doors.attributes.entity_id -%}
{% set domain,name = entity_id.split('.') -%}
{%- if ignore_user_state == 'true' or is_state('binary_sensor.' + name , 'off') %}
{%- if new_state == 'off' or (new_state == 'on' and is_state('binary_sensor.auto_light_on', ‘on’)) %}
{% if new_state == 'on' %}
light.turn_on
{% else %}
light.turn_off
{% endif %}
{% else %}
script.null_script
{% endif %}
{% else %}
script.null_script
{% endif %}
{% else %}
script.null_script
{%- endfor %}
WHAT DO I DO WITH THIS??
data_template:
entity_id: "light.{{ name }}"
brightness_pct: "{{ brightness }}"