Handling target input in template

Hi all, I’m working on a blueprint for managing all the lights in a room with a single remote, in this case a Tradfri puck. I’m aware of the already existing blueprints but my plans reach further, as I want to use the arrow buttons to cycle through the lights. However I didn’t end up there yet as I’m facing problems with the target input selector I am using:

blueprint:
  name: Full room light controller
  domain: automation
  input:
    lights:
      name: Target lights
      description: The lights to control
      selector:
        target:
          entity:
            domain: light

I use this to select an area, but I’ve also tried manually selecting devices (which would be okay) but that won’t work either. The problem is here, where I need to get an average brightness of all the lights selected by the target. Note that this code isn’t too functional, I removed other math and service calls so that I can focus on getting this value right.

action:
- variables:
    target_light: !input lights

- choose:

  # Device selector (right)
  - conditions:
    - condition: template
      value_template: '{{ trigger.to_state.attributes.action == "arrow_right_click" }}'
    sequence:
    - service: logbook.log
      data:
        name: Find brightness level
        message: 'Average brightness: {{ expand(target_light) | selectattr("attributes.brightness", "defined") | map(attribute="attributes.brightness") | average() | round(0) }}'

However in the trace:

Error: Error rendering data template: StatisticsError: fmean requires at least one data point

Which leads me to believe that expand(target) leads to nothing. In a previous try I tried to join(",") the state values but that didn’t show anything either. Now I know the docs says target is supposed to be used by service calls, but I swear that I’ve read somewhere in there that you can use it to expand targets as well. I’ve been plowing through the docs all morning but I can not find it again, so I guess I am mistaken. Is there any other way to expand a target input into devices or entities for further processing?

Use the trace function for the automation to see what target_light is set to during run. If it’s a list of entity_id’s then your expand should work. However, keep in mind that lights will not have a brightness attribute when off. So if all your lights are off, you’ll be feeding an empty generator to average. Secondly, remove the () from average. It’s a filter, the () is implied. I.e.

{{ arg1 | average }}

is functionally equivalent to

{{ average(arg1) }}

Notice how you do not include the () when using it as a filter unless you plan on adding more arguments.

At a minimum, I would change your template to this assuming that target_light is a list of entity_id’s:

        message: >
           {% set brightness = expand(target_light) | selectattr("attributes.brightness", "defined") | map(attribute="attributes.brightness") | list %}
           {% if brightness %}
              Average brightness: {{ brightness[0] if brightness | count == 1 else brightness | average }}
           {% else %}
              Average brightness: 0
           {% endif %}