Automation question - templating (turn off automation when light turns red)

hey,

quick question: I want an easy automation that turns off my bedroom lights automation when I turn one of my bedroom lights to red. I tried this with the UI configurator:

alias: Schlafzimmer Lights (Fix)
description: ''
trigger:
  - platform: template
    value_template: >-
      "{{ is_state_attr('light.nachttisch_name' , 'rgb_color' , (255, 66, 8))
      }}"
    id: red
condition: []
action:
  - condition: trigger
    id: red
  - service: automation.turn_off
    data: {}
    target:
      entity_id: automation.schlafzimmer_lights_ui
mode: single

If I turn on “light.nachttisch_name” to the correct RGB values, nothing happens (i.e. the automation does not turn off…)

Any ideas what I am doing wrong?

Attributes of the light:

Try changing (255, 66, 8) to [255, 66, 8]. (I.e., change from a tuple to a list.)

Hi @pnbruckner, thank you for taking a look here. I tried that and changed it to the following:

alias: Schlafzimmer Lights (Fix)
description: ''
trigger:
  - platform: template
    value_template: >-
      "{{ is_state_attr('light.nachttisch_name' , 'rgb_color' , [255, 66,
      8])}}"
    id: red
condition: []
action:
  - condition: trigger
    id: red
  - service: automation.turn_off
    data: {}
    target:
      entity_id: automation.schlafzimmer_lights_ui
mode: single

But still no change, the automation does not trigger if I set the lights to the RGB values.

Here is the state of the light and the automation trace.


image

You use both >- and ".

either:

    value_template: >-
      {{ is_state_attr('light.nachttisch_name' , 'rgb_color' , [255, 66,
      8])}}

or

    value_template: "{{ is_state_attr('light.nachttisch_name' , 'rgb_color' , [255, 66,
      8])}}"
2 Likes

Thank you @Hellis81 for that! I fixed that, but still no avail :frowning: nothing happens:

- id: '1656514096118'
  alias: 'Schlafzimmer Lights (Fix)'
  description: ''
  trigger:
    - platform: template
      value_template: "{{ is_state_attr('light.nachttisch_name' , 'rgb_color' , [255, 66, 8])}}"
      id: red
  condition: []
  action:
    - condition: trigger
      id: red
    - service: automation.turn_off
      data: {}
      target:
        entity_id: automation.schlafzimmer_lights_ui
  mode: single

This seems like an easy automation, I do not know why this is not working :frowning:

In developer tools → templates, what does this return:

{{ is_state_attr('light.nachttisch_name' , 'rgb_color' , [255, 66, 8]) }}
{{ state_attr('light.nachttisch_name' , 'rgb_color') }}
{{ states('light.nachttisch_name') }}

?

hey @Hellis81; this is the results:

{{ is_state_attr('light.nachttisch_name' , 'rgb_color' , [255, 66, 8]) }}

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

{{ states('light.nachttisch_name') }}

sooo…false, the current values and on for the light…this seems strange to me :open_mouth:

Ok…
What about:

{{ state_attr('light.nachttisch_name' , 'rgb_color') | join(",") == "255,66,8" }}

I just noticed there was spaces Infront of 66 and 8. Try and remove them from the previous template also. It shouldn’t matter but let’s see.

Express the RGB value as a tuple instead of a list. Notice how the template reports true when the value is compared with a tuple.

- id: '1656514096118'
  alias: 'Schlafzimmer Lights (Fix)'
  description: ''
  trigger:
    - platform: template 
      value_template: "{{ is_state_attr('light.nachttisch_name', 'rgb_color', (255, 66, 8)) }}"
  condition: []
  action:
    - service: automation.turn_off
      target:
        entity_id: automation.schlafzimmer_lights_ui
  mode: single

NOTE

Instead of creating an automation to turn off another automation, just add a Template Condition to the other automation.

With the following template, the automation executes its action only if the light’s RGB value is not 255, 66, 8.

  condition:
    - platform: template 
      value_template: "{{ not is_state_attr('light.nachttisch_name', 'rgb_color', (255, 66, 8)) }}"
1 Like

hey @123, thank you so much! this did the trick. Funny, I changed it from a tuple to a list because of the first comment, the change from @Hellis81 then did the trick I guess but I did not change it back.

Also, thank you so much for your additional help. I am new to templating and also only did very basic automations for my lights as of so far, so I really appreciate it. I will try to add it as a condition!

Thank you everyone for the help!

The issue in the first post was the >- and " at the same time.
So the thread made a complete circle :slight_smile:

1 Like