How to filter / test a service call target group

I’m building a Blueprint to help me control my various lights with various motion sensors.

I’m setting the Blueprint up to take a domain light target, which could be an area or set of lights. I would like to read the state of these lights similar to a group of lights and/or filter them down to only lights that are currently on, so I can do a service call to light.turn_on to change the brightness of those lights that are already on, without turning on the other lights in the target group.

Is this possible? If so, how can I achieve that? Thank you!

So I researched and tried a couple of things.

  1. Using scene.apply isn’t possible, because of the way that it needs to be invoked:
// Real way of having to invoke scene.apply
service: scene.apply
data:
  entities:
    some_light:
      brightness: 50
    some_other_light:
      brightness: 50

// How it would have to be invokeable to make it possible to use it with `targets`
service: scene.apply
data:
  target: my_target
  apply:
    brightness: 50
  1. Using expand to “resolve” the target entities
- variables:
  lights: !input my_target
- service: system_log.write
  data:
    message: "{{ expand(lights) }}"
    level: info

It might be useful to expand (pun intended) the functionality to include expanding targets to a list of entities that match the target criteria.

  1. Manually “parsing” the target object.
    It wasn’t clear to me how to go from area_id or device_id to entity_id. If this is possible I would love to understand how.

Any advice on how else to achieve this greatly appreciated.

A (relatively) recent update introduced area_entities and device_entities which make it now possible to manually “pick apart” a target object like so:

{% set target = { 'entity_id': ['light.kitchen_main_lights', 'light.office_corner_lamp'], 'area_id': 'kitchen' } %}
{% set entities = expand(target.entity_id) %}
{% set area_entities = (target.area_id if (target.area_id is not string) else [target.area_id]) | map('area_entities') | expand %}
{% set device_entities = (target.device_id if (target.device_id is not string) else [target.device_id]) | map('device_entities') | expand %}
{{ expand(entities + area_entities + device_entities) | count }}

Note that this will expand groups into their components. I don’t think there’s a way around that.

Of course, it still would be awesome to just enable us to pass a target object/mapping to expand to achieve the same, e.g.

{{ expand(target) | count }}