Flashing only lights that are on

Curious if anyone’s found a way to do this. I’ve seen examples on flashing a group of lights, or individual lights, however I’d like to do something just a little different. I’d like to flash only lights that are already on, leaving the off lights off. Only if there are lights on.

Anyone attempt something like this before?

You can check the state of the light before flashing e.g. if it is already on or not.

Where and how are you planning to do this automation?

That won’t really work on it’s own. If the light is on, I flip it off… repeat for all lights. Then going around again, how do I know which ones go back on?

I used the following and it seemed to do what you want. It flashed the lights that were on and left the others alone.

- alias: Flash those lights
  trigger:
    platform: time
    at: '15:18'
  action:
    service: light.turn_on
    data_template: 
      entity_id: > 
       {%- for device in dict(states.light|groupby("state"))["on"] %}{%- if loop.first %}{% else %}, {% endif %}{{device.entity_id | lower }}{%- if loop.last %}{% endif %}{%- endfor  %}
      flash: short

So this intrigued me. :slight_smile: 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.

2 Likes

Wow, that’s a really interesting way of doing it! That’s what I was hoping was possible. This is definitely cookbook material. I can see there being other uses for something like this.

1 Like