Templated light colour selection in automation

Hello, I created the following action in an automation which turns on a light in a different colour based upon the trigger (rain detected - or dry). I can see that it’s formatted incorrectly, but the odd thing is that it was working and had been for a few days and then stopped. I’ve worked around it in a different way now, but it would be good to know where I’ve gone wrong as this templated solution is more elegant (or would be, if it worked!).

service: light.turn_on
data: |
  {% if states('trigger.to_state','on') %} 
    {"rgb_color": "[0,0,255]"}
  {% else %}
    {"rgb_color": "[255,165,0]"}
  {% endif %} 
  brightness: 133
target:
  entity_id:
    - light.ceiling_2_local
    - light.bedroom_local
    - light.lobby_light
    - light.hallway_3_local
enabled: true

TVMIA

Try this version.

service: light.turn_on
data:
  rgb_color: "{{ [0,0,255] if trigger.to_state.state == 'on' else [255,165,0] }}"
  brightness: 133
target:
  entity_id:
    - light.ceiling_2_local
    - light.bedroom_local
    - light.lobby_light
    - light.hallway_3_local
enabled: true

That makes a lot more sense, is more elegant still and - it works!

Thank you very much

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.