Condition template not working

I have made an automation that turns on some light in a clock and chooses a random color each time. Because the blue light is much less intense that the other colors I would like to increase brightness when color is blue.
I have added the condition: template in my action but nothing happens. I don´t get any error in the log file.
Someone has an idea what is wrong?

action:
  - service: light.turn_on
    entity_id: light.wallclock
    data_template:
      color_name: >
       {{ [
       "blue",
       "red",
       "green",
       "orange",
       "cyan",
       "magenta"
       ] |random }}
      brightness: 20
  - delay: 0:00:01
  - service: light.turn_on
    entity_id: light.wallclock
  - condition: template
    value_template: '{{ states.light.wallclock.attributes.color_name == "blue" }}'
  - service: light.turn_on
    data:
      entity_id: light.wallclock
      brightness: 50

You may have an issue with there not being enough time between setting the color and it being accessible in the object.

You could just build 2 automations, one that handles the color, the other that handles the brightness

....
  - service: light.turn_on
    entity_id: light.wallclock
    data_template:
      color_name: >
       {{ [
       "blue",
       "red",
       "green",
       "orange",
       "cyan",
       "magenta"
       ] |random }}

- alias: change brightness based on color
  trigger:
    - platform: state
      entity_id: light.wallclock
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.wallclock
        brightness: "{{ 50 if is_state_attr('light.wallclock','color_name','blue') else 20 }}"

Your other option is to incorporate the random component

sensor:
  - platform: random
    name: color
    min: 0
    max: 5
action:
  - service: light.turn_on
    entity_id: light.wallclock
    data_template:
      color_name: >
       {{ [
       "blue",
       "red",
       "green",
       "orange",
       "cyan",
       "magenta" ][states('random.color')|int] }}
      brightness: "{{ 50 if states('random.color')|int == 0 else 20 }}"

I have tried your first suggestion but it seems that the light keeps turning on also after turning it off. Could that be because the action triggers after each state change, including “off”?
I have also noticed that the LED driver for this wall clock somehow needs a change in brightness after it has been set the first time to react. Brightness -->20 nothing happens, then brightness -->21 then brightness is set to 20 in reality. Odd behaviour but that is strangely how this zigbee RGBW driver Works through Philips Hue Bridge with HA.

Yah, you should only fire the automation if the light is getting turned on.

trigger:
  - platform: state
    entity_id: light.light
    to: 'on'

I have also tried to insert a 10 sec delay in my own automation before the condition: template but it still does not increase the brightness when blue is active.

I’m not sure how scripts work when trying to access changes in states that occur throughout the script. That is why I tried to steer you towards the other solution, which uses a state object that drives the randomness. That way you know that ‘blue’ will be chosen prior to choosing it. It allows you to account for the color blue all throughout your script.

I have now tried this for brightness but it stays at 20, also when blue.
I have added the delay and the extra data template for getting the brightness change as described before.

  - alias: change brightness based on color
    trigger:
      - platform: state
        entity_id: light.wallclock
        to: 'on'
    action:
      - service: light.turn_on
        data_template:
          entity_id: light.wallclock
          brightness: "{{ 50 if is_state_attr('light.wallclock','color_name','blue') else 20 }}"
      - delay: 0:00:01
      - service: light.turn_on
        data_template:
          entity_id: light.wallclock
          brightness: "{{ 51 if is_state_attr('light.wallclock','color_name','blue') else 21 }}"

are you sure the color name is changing?

actually I don´t see “color_name” under attributes, but since it reacts in my automations I guess it has to be okay.
I also tried replacing color_name with rgb_color but still the same
(value_template: ‘{{ states.light.ur.attributes.rgb_color == [10,0,255] }}’

{
“rgb_color”: [
10,
0,
255
],
“max_mireds”: 500,
“hs_color”: [
242.588,
100
],
“brightness”: 21,
“friendly_name”: “Ur”,
“min_mireds”: 153,
“effect_list”: [
“colorloop”,
“random”
],
“xy_color”: [
0.137,
0.041
],
“supported_features”: 63
}

those are the attributes for wallclock? your value template is looking at ur. Is that on purpose?

id expect this to work:

      - service: light.turn_on
        data_template:
          entity_id: light.wallclock
          brightness: "{{ 50 if is_state_attr('light.wallclock','rgb_color',[10,0,255]) else 20 }}"

Yes these are the attributes.
Sorry about “ur” It is just the Danish Word for clock (wallclock). I translated it so it would make a little sense, but forgot the translation here :slight_smile:

I have now tried this but brightness no matter what color ends up at 21?

action:
  - service: light.turn_on
    data_template:
      entity_id: light.ur
      brightness: "{{ 50 if is_state_attr('light.ur','rgb_color',[10,0,255]) else 20 }}"
  - delay: 0:00:01
  - service: light.turn_on
    data_template:
      entity_id: light.ur
      brightness: "{{ 51 if is_state_attr('light.ur','rgb_color',[10,0,255]) else 21 }}"

Have you tried changing the color and checking the template inside the template editor?

I am not quite sure how to check the template inside the editor. I have inserted the template in the editor and see some result in the upper right side. This does not change when I change color to blue.

editor:
data_template:
entity_id: light.ur
brightness: “{{ 50 if is_state_attr(‘light.ur’,‘rgb_color’,[10,0,255]) else 20 }}”

upper right side:
data_template:
entity_id: light.ur
brightness: “20”

place this into the template editor after you turn the light blue

{{ state_attr('light.ur','rgb_color') }}

or

{{ states.light.ur.attributes.rgb_color }}

Hmm the response is: (10, 0, 255) as it should be. It seems that it is correct but when I try color_name there is no result. So I guess color_name can be used for setting the color but is not used for “feedback”

I should be possible to get anything/atribute that is shown in the dev-state on that entity_id into a message template for feedback or use it in a value_template somehow.

What does that page show on the light.ur?

you could also check if settings are applied correctly by customizing the light in the front-end to display those attributes:

light.world_map:
  templates:
    icon: >
      if (state === 'on') return 'mdi:map';
      return 'mdi:map-outline';
  extra_data_template: >
    if (attributes.hs_color) 
        return 'Hs: ' + attributes.hs_color;
    return null;
  state_card_mode: break-slider
  stretch_slider: true
  hide_control: false
  show_last_changed: true
  confirm_controls: true
  slider_theme:
    min: 0
    max: 255
    pin: true
    off_when_min: true
    report_when_not_changed: true
  extra_badge:
    - attribute: brightness
      unit: Br

20

just a bit more fun than checking the dev-state page :wink:

The device state page shows when blue is selected:

{
“rgb_color”: [
10,
0,
255
],
“max_mireds”: 500,
“hs_color”: [
242.588,
100
],
“brightness”: 21,
“friendly_name”: “Ur”,
“min_mireds”: 153,
“effect_list”: [
“colorloop”,
“random”
],
“xy_color”: [
0.137,
0.041
],
“supported_features”: 63
}

color_name is not there, so no way to show that in a template.

to be sure, could you screenshot the page?

But the strange thing is that I am using color_names for setting the color and it Works fine.