How to access the actual entity inside service template

Hello,

I’m creating a new automation and i would like to turn on light only if their are already turned on (to only change the colors of turned on lights and keep the turned off lights off)

How can i access the actual light inside my service template please?

alias: Couleur des lumières en fonction du soleil
description: ''
trigger:
  - platform: sun
    event: sunrise
  - platform: sun
    event: sunset
condition: []
action:
  - service: |
      {% if states(this_light) == "on" %} # Here.. how can i get the actual entity's state inside this loop ?
        light.turn_on
      {% endif %}
    entity_id: 
      - light.hue_color_lamp_1
      - light.hue_color_lamp_2
mode: single

I have found a way but it’s really ugly…
Maybe someone here have a better example?

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(',')
              }}
      rgb_color: [255, 226, 145]

You don’t need to template service just entity_id.

The template for entity_id must select all (Hue) lights that are currently on. Here is a first-approximation of what I mean (it will require refinement):

    entity_id: >
      {{ states.light | selectattr('state', 'eq', 'on')
         | map(attribute='entity_id') | list | join (', ') }}

If you have a few Hue lights (and other lights you have aren’t Hue) then the first refinement is to replace states.light. For example:

    entity_id: >
      {{ [states.light.hue_color_lamp_1, states.light.hue_color_lamp_2]
         | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list | join (', ') }}

If you have an existing group of Hue lights then you can do this:

    entity_id: >
      {{ expand('group.my_hue_lights') | selectattr('state', 'eq', 'on')
         | map(attribute='entity_id') | list | join (', ') }}

EDIT

While composing my reply, you have effectively come to the same conclusion.

FYI: Your original idea of using a for-loop in service would have never worked.

Note: Be sure to test your template when all lights are off. In that situation, the template returns nothing and the service call expects something. In that case, I believe the template should return ‘none’.

1 Like

As an alternative, since you’re using Philips Hue, you can create a mega group that includes all the Hue lights you have and then send commands to this group without state payload and only lights that are on would change (while an usual payload would be {"on":true, "sat":155, "bri":180,"hue":5000} for on and {"on":false} for turning the group off you need only {"sat":155, "bri":180,"hue":5000}). This is similar behavior to using a Hue dimmer with a group (brightness_up and brightness_down would only affect lights that are on).

This works using PUT method with /api/$user_id$/groups/$group_number$/action (replace $user_id$ and $group_number$) at http://hue_ip/debug/clip.html

The advantage would be that all the lights would change the exact instance (vs. identifying all bulbs/groups that are on and then changing them individually). Using the mega group in HA as a regular light would serve no purpose since it would turn on all lights.

Unfortunately, the above would be able to control only Philips Hue lights.

1 Like