Last active
October 23, 2025 07:33
-
-
Save patricksmms/1dda70fd8d504771274c8197b48ff47a to your computer and use it in GitHub Desktop.
[Home assistant script] Light: Apply attributes and restore power
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| alias: "Light: Apply attributes and restore power" | |
| mode: parallel | |
| max: 50 | |
| fields: | |
| lights: | |
| name: Lights target | |
| description: Entities, devices or areas containing lights | |
| selector: | |
| target: | |
| entity: | |
| domain: light | |
| brightness: | |
| name: Brightness | |
| required: false | |
| selector: | |
| number: | |
| min: 1 | |
| max: 255 | |
| mode: slider | |
| rgb_color: | |
| name: RGB color | |
| required: false | |
| selector: | |
| color_rgb: {} | |
| color_temp: | |
| name: Color temperature (mireds) | |
| required: false | |
| selector: | |
| number: | |
| min: 140 | |
| max: 650 | |
| color_temp_kelvin: | |
| name: Color temperature (kelvin) | |
| required: false | |
| selector: | |
| number: | |
| min: 1500 | |
| max: 6500 | |
| step: 50 | |
| color_name: | |
| name: Color name | |
| required: false | |
| selector: | |
| text: {} | |
| effect: | |
| name: Effect | |
| required: false | |
| selector: | |
| text: {} | |
| expand_members: | |
| name: Expand light-group members | |
| description: If a light group is provided, apply to each member and restore per member | |
| default: true | |
| selector: | |
| boolean: {} | |
| restore_delay_ms: | |
| name: Delay before restoring power (ms) | |
| default: 300 | |
| selector: | |
| number: | |
| min: 0 | |
| max: 5000 | |
| step: 50 | |
| mode: slider | |
| sequence: | |
| - variables: | |
| tgt: "{{ lights }}" | |
| ids: > | |
| {% set ns = namespace(v=[]) %} | |
| {% if tgt.entity_id is defined %} | |
| {% set e = tgt.entity_id %} | |
| {% set ns.v = ns.v + (e if e is iterable and e is not string else [e]) %} | |
| {% endif %} | |
| {% if tgt.area_id is defined %} | |
| {% for a in (tgt.area_id if tgt.area_id is iterable and tgt.area_id is not string else [tgt.area_id]) %} | |
| {% set ns.v = ns.v + area_entities(a) %} | |
| {% endfor %} | |
| {% endif %} | |
| {% if tgt.device_id is defined %} | |
| {% for d in (tgt.device_id if tgt.device_id is iterable and tgt.device_id is not string else [tgt.device_id]) %} | |
| {% set ns.v = ns.v + device_entities(d) %} | |
| {% endfor %} | |
| {% endif %} | |
| {{ ns.v | select('match','^light\\.') | list | unique | list }} | |
| expand: "{{ expand_members | default(true) }}" | |
| entities: > | |
| {% set ns = namespace(v=[]) %} | |
| {% for e in ids %} | |
| {% set members = state_attr(e, 'entity_id') if expand else none %} | |
| {% if members %} | |
| {% for m in members %} | |
| {% if m is string and m.startswith('light.') %} | |
| {% set ns.v = ns.v + [m] %} | |
| {% endif %} | |
| {% endfor %} | |
| {% else %} | |
| {% set ns.v = ns.v + [e] %} | |
| {% endif %} | |
| {% endfor %} | |
| {{ ns.v | unique | list }} | |
| - repeat: | |
| for_each: "{{ entities }}" | |
| sequence: | |
| - variables: | |
| was_on: "{{ is_state(repeat.item, 'on') }}" | |
| - choose: | |
| - conditions: "{{ rgb_color is defined and rgb_color is not none }}" | |
| sequence: | |
| - service: light.turn_on | |
| target: | |
| entity_id: "{{ repeat.item }}" | |
| data: | |
| rgb_color: "{{ rgb_color }}" | |
| brightness: "{{ brightness | default(omit) }}" | |
| effect: "{{ effect | default(omit) }}" | |
| - conditions: "{{ color_temp is defined and color_temp is not none }}" | |
| sequence: | |
| - service: light.turn_on | |
| target: | |
| entity_id: "{{ repeat.item }}" | |
| data: | |
| color_temp: "{{ color_temp }}" | |
| brightness: "{{ brightness | default(omit) }}" | |
| effect: "{{ effect | default(omit) }}" | |
| - conditions: "{{ color_temp_kelvin is defined and color_temp_kelvin is not none }}" | |
| sequence: | |
| - service: light.turn_on | |
| target: | |
| entity_id: "{{ repeat.item }}" | |
| data: | |
| color_temp_kelvin: "{{ color_temp_kelvin }}" | |
| brightness: "{{ brightness | default(omit) }}" | |
| effect: "{{ effect | default(omit) }}" | |
| - conditions: "{{ color_name is defined and color_name is not none }}" | |
| sequence: | |
| - service: light.turn_on | |
| target: | |
| entity_id: "{{ repeat.item }}" | |
| data: | |
| color_name: "{{ color_name }}" | |
| brightness: "{{ brightness | default(omit) }}" | |
| effect: "{{ effect | default(omit) }}" | |
| - conditions: [] # no color descriptor provided | |
| sequence: | |
| - service: light.turn_on | |
| target: | |
| entity_id: "{{ repeat.item }}" | |
| data: | |
| brightness: "{{ brightness | default(omit) }}" | |
| effect: "{{ effect | default(omit) }}" | |
| - choose: | |
| - conditions: | |
| - condition: template | |
| value_template: "{{ not was_on }}" | |
| sequence: | |
| - delay: | |
| milliseconds: "{{ restore_delay_ms | default(300) | int }}" | |
| - service: light.turn_off | |
| target: | |
| entity_id: "{{ repeat.item }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment