Help with Entity ID Template in Automation Action to Change Only Lights That Are On

I’m working on an automation that will change the brightness and temperature of lights during the day. I looked at other pre-built options (e.g., Flux and Circadian), but I wanted to see if I could build my own. When the Deconz daylight changes (slightly modified by the time), I want lights to change temp and brightness, but only if they’re already on. There are a few conditions, but the most relevant one for this automation is that a light must be on. I control my lights using Hue groups to smooth out lights coming on together, so I want to filter out the individual bulbs.

The problem is in the data template for entity ID in the action section. I have two selectattr() filters that should filter lights that are both on and Hue groups. When I include only one of these, it passes. But when I have both, it fails. I’ve been banging my head against the wall on this for 24 hours. After digging through posts from @petro and @pnbruckner, and reading the Jinja documentation on filters, I don’t see a reason why this fails.

What’s annoying is that when I test the template in dev tools, it produces the correct list of entity ids.

Here’s the code that’s causing an error:

- alias: 'Lights - Adjust if On'
  description: 'If lights are on, adjust the brightness based on time'
  hide_entity: true
  trigger:
    - platform: event
      event_type: state_changed
      entity_id: sensor.daylight
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: 'group.people'
        state: 'on'
      - condition: state
        entity_id: 'input_boolean.good_night'
        state: 'off'
      - condition: template
        value_template: "{{is_state('group.all_lights','on')}}"
  action:
    - service: light.turn_on
      data_template:
        entity_id: >
          {{ states.light
            | selectattr('attributes.is_hue_group')
            | selectattr('state','eq','on')
            | map(attribute='entity_id')
            | join(',')
            }}
        brightness: >
          {% set time=(states('sensor.time').split(':')[0] |int * 60) + (states('sensor.time').split(':')[1] |int) %}
          {%- if is_state('sensor.daylight', 'night_end') -%}
            30
          {%- elif is_state('sensor.daylight', 'nautical_dawn') -%}
            30
          {%- elif is_state('sensor.daylight', 'dawn') -%}
            143
          {%- elif is_state('sensor.daylight', 'sunrise_start') -%}
            254
          {%- elif is_state('sensor.daylight', 'sunrise_end') -%}
            254
          {%- elif is_state('sensor.daylight', 'golden_hour_1') -%}
            254
          {%- elif is_state('sensor.daylight', 'solar_noon') -%}
            254
          {%- elif is_state('sensor.daylight', 'golden_hour_2') -%}
            254
          {%- elif is_state('sensor.daylight', 'sunset_start') -%}
            254
          {%- elif is_state('sensor.daylight', 'sunset_end') -%}
            254
          {%- elif is_state('sensor.daylight', 'dusk') -%}
            254
          {%- elif is_state('sensor.daylight', 'nautical_dusk') -%}
            254
          {%- elif is_state('sensor.daylight', 'night_start') and time >= 0 and time < 480 -%}
            143
          {%- elif is_state('sensor.daylight', 'night_start') and time >= 480 and time < 600 -%}
            254
          {%- elif is_state('sensor.daylight', 'night_start') and time >= 600 and time < 1110 -%}
            254
          {%- elif is_state('sensor.daylight', 'night_start') and time >= 1110 and time < 1350 -%}
            254
          {%- elif is_state('sensor.daylight', 'night_start') and time >= 1350 and time < 1440 -%}
            143
          {%- elif is_state('sensor.daylight', 'night_start') -%}
            254
          {%- elif is_state('sensor.daylight', 'nadir') -%}
            143
          {%- else -%}
            254
          {%- endif -%}
        color_temp: >
          {% set time=(states('sensor.time').split(':')[0] |int * 60) + (states('sensor.time').split(':')[1] |int) %}
          {%- if is_state('sensor.daylight', 'night_end') -%}
            500
          {%- elif is_state('sensor.daylight', 'nautical_dawn') -%}
            500
          {%- elif is_state('sensor.daylight', 'dawn') -%}
            466
          {%- elif is_state('sensor.daylight', 'sunrise_start') -%}
            154
          {%- elif is_state('sensor.daylight', 'sunrise_end') -%}
            154
          {%- elif is_state('sensor.daylight', 'golden_hour_1') -%}
            154
          {%- elif is_state('sensor.daylight', 'solar_noon') -%}
            181
          {%- elif is_state('sensor.daylight', 'golden_hour_2') -%}
            181
          {%- elif is_state('sensor.daylight', 'sunset_start') -%}
            370
          {%- elif is_state('sensor.daylight', 'sunset_end') -%}
            370
          {%- elif is_state('sensor.daylight', 'dusk') -%}
            370
          {%- elif is_state('sensor.daylight', 'nautical_dusk') -%}
            370
          {%- elif is_state('sensor.daylight', 'night_start') and time >= 0 and time < 480 -%}
            500
          {%- elif is_state('sensor.daylight', 'night_start') and time >= 480 and time < 600 -%}
            154
          {%- elif is_state('sensor.daylight', 'night_start') and time >= 600 and time < 1110 -%}
            181
          {%- elif is_state('sensor.daylight', 'night_start') and time >= 1110 and time < 1350 -%}
            370
          {%- elif is_state('sensor.daylight', 'night_start') and time >= 1350 and time < 1440 -%}
            466
          {%- elif is_state('sensor.daylight', 'nadir') -%}
            466
          {%- else -%}
            370
          {%- endif -%}

Here’s the error:
Invalid config for [automation]: [entity_id] is an invalid option for [automation]. Check: automation->trigger->0->entity_id.

Any ideas?

It’s pointing to your trigger. Your trigger is formatted incorrectly, you’re using an event trigger but you don’t have event data. Also, you’re adding entity_id which is not a valid option for an event trigger. This is ultimately what your error is saying. It seems like you just want to trigger off state changes, and all you need is change this:

    - platform: event
      event_type: state_changed
      entity_id: sensor.daylight

To this:

    - platform: state
      entity_id: sensor.daylight
1 Like

That’s it. I was so focused on the complicated action section that I blew past the other part. There’s so much to learn and remember. Thanks, @petro