So this intrigued me. After many failed attempts, and ultimately using ideas from @gumbo’s post (thx!), I came up with the following script. First a few comments.
I wanted to come up with an implementation that could flash both switch and light entities (since often lights are actually controlled by switch entities. At least they are for me.) Also, at least some lights (e.g., dimmers) can take time to ramp on or off by default. So the “flashing” needed to account for that. (FWIW, I did already have a script to flash a couple of lights, one of which is controlled by a z-wave dimmer that does have this ramping feature. In that case I actually change its configuration to turn on and off quickly at the start of the script and change it back to default at the end. I didn’t do that here.)
And here’s the script, which expects group.flash_on
to exist whose contents is the lights/switches that you want to flash.
flash_on:
alias: Flash on lights in group
sequence:
- condition: template
value_template: >
{{ states|selectattr('entity_id','in',
state_attr('group.flash_on','entity_id'))
|selectattr('state','eq','on')|list|count > 0 }}
- service: group.set
data_template:
object_id: flash_on_set
view: false
visible: false
entities: >
{% for e in states|selectattr('entity_id','in',
state_attr('group.flash_on','entity_id'))
|selectattr('state','eq','on') -%}
{% if not loop.first %},{% endif %}{{ e.entity_id }}
{%- endfor %}
- service: homeassistant.toggle
entity_id: group.flash_on_set
- delay: 00:00:03
- service: homeassistant.toggle
entity_id: group.flash_on_set
- delay: 00:00:03
- service: homeassistant.toggle
entity_id: group.flash_on_set
- delay: 00:00:03
- service: homeassistant.toggle
entity_id: group.flash_on_set
A little about how this works. First it checks to make sure at least one of the lights in the group is on. It does this by taking all the states in the system, selecting only those that are for entities that are in group.flash_on
, and then of those selecting only the ones that are on, and lastly getting a count of them.
Next, assuming at least one of the subject lights were on, it does the same thing, but instead of taking a count, it builds a list of the corresponding entity_id’s, and uses that to create another group - group.flash_on_set
- that it will use to flash the lights that were on. The delay between each toggle gives time for lights that ramp slowly on and off. Obviously adjust or remove those to match your situation.