Apply scene to only lights already on

I already use a light-by-light approach to see if a particular light is already on, but can I do this group/area/label-based? Only apply a scene to those lights which are already on?

If this is the service ‘Scene Apply’, may I have an example of what the yaml would look like… Thanks!

You can certainly select only the lights that are on in an area, group or with certain labels with a template. Scenes do not support templates though. So you would have to use a script instead.

I recently changed all my scenes to scripts. It simplified my automations considerably.

1 Like

Thanks for the reply.

Would you please show me an example of the code used to dynamically identify which lights are on.

I’ve always been confused by the Scene element… Seems they’re very limiting, but then requires to have the entities already identified for them.

Here’s an example of a service call that turns off all lights that are on in three areas. If you’re dealing with labels instead of areas, use label_entities instead of area_entities.

  - service: light.turn_off
    target:
      entity_id: >
        {{ ['kitchen', 'dining', 'family']
          | map('area_entities') | sum(start=[])
          | select('match', 'light') | expand
          | selectattr('state', 'eq', 'on')
          | map(attribute='entity_id') | list }}

Template for a floor.

{{ floor_areas('downstairs') 
  | map('area_entities') | sum(start=[])
  | select('match', 'light') | expand
  | selectattr('state', 'eq', 'on')
  | map(attribute='entity_id') | list }}

Template for a Light Group. For a legacy Group (the kind defined in YAML exclusively) replace light.test_light_group with the entity_id of your legacy Group.

{{ state_attr('light.test_light_group', 'entity_id')
  | expand | selectattr('state', 'eq', 'on')
  | map(attribute='entity_id') | list }}
3 Likes

I’ve found that calling the service to labelled entities no longer ‘requires’ the selector in the first place.

for lights and switches , I didnt bother anymore since some time, but media_players tend to be unavailable here because of main power not being on, so I had that templated.

otherwise the volume setting, or playing a file on those would return an error.

using those services to labelled entities no longer requires that, so those selectors have become even simpler

      service: media_player.play_media
      target:
        label_id: intercom_speler

same would go for toggling lights

      service: light.turn_off
      target:
        area_id:
          - kitchen
          - dining
          - family

(btw only tts service seems not yet updated for this…)

Thanks. So using your example, it’s ’ | selectattr(‘state’, ‘eq’, ‘on’)’ that is determining whether the light is on or not. I’ll see if I can integrate that into what I’m trying to do.

I think you’re missing the point of my inquiry… I want to change the bulb color of only those lights that are presently on and labelled with a specific label. If I send the scene to all, all lights will be turned on.

You’re right. I missed that.
Sorry, please ignore …

They’re used by other automation platforms and so HA had to have something similar as people are used to them.

Also it is easy. You only have to tell HA which entity and what state you want it in. No worrying about service calls, the scene does that for you.

But yes it is limited in what it can do.

Creating scenes “on the fly” are useful for making momentary changes then restoring some state though.

1 Like

Funny thing is… I actually had several ‘Scenes’ created, but didn’t realize, not one was active :grinning: If they had more options, I would probably want to integrate them. Since I have a limited number of actual devices routinely used, not sure how it matters the number of service calls made.