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
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):
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’.
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.