Use template trigger to turn on music at a certain RGB value

Hello everyone,

For this specific usecase I would like to turn on the music on a Sonos speaker when a Hue lightstrip has a certain RGB Color. I’ve setup the following automation:

alias: WERKPLEK_Dirk - Speel muziek bij inschakelen 'Werken' scene
description:
triggers:
  - trigger: template
    value_template: >-
      RGB Color:{{ state_attr('light.hue_bulb_bureau_dirk','rgb_color') ==
      (255,214, 181) }}
    enabled: true
conditions:
  - condition: state
    entity_id: input_boolean.kill_switch
    state: "off"
actions:
  - action: media_player.media_play
    target:
      entity_id: media_player.sonos_symfonisk_slaapkamer
    data: {}
mode: single

The problem is that the trigger isn’t activated when the light is set to RGB color 255,214, 181. I’ve tested the template code in Developer tools > Template and over there I see a change from ‘false’ to ‘true’. Any Idea why this automation isn’t triggered?

Hi Dirk, did you check the automation traces?

Pretty sure the rgb_color attribute would be a list, but you are comparing it against a tuple. Change the parenthesized value (255,214,181) to one that is bracketed [255,214,181].

Also why do you have a “RGB Color:” prefix in your template? For a trigger template to work properly it must only result in a value which when returned to HASS can be interpreted as a boolean, which it never will with that prefix included.

If all else fails then this should work:

{{ (state_attr('light.hue_bulb_bureau_dirk', 'rgb_color') |list)[0] == 255 and  (state_attr('light.hue_bulb_bureau_dirk', 'rgb_color') |list)[1] == 214 and  (state_attr('light.hue_bulb_bureau_dirk', 'rgb_color') |list)[2] == 181  }} 

Actually… this works.

{{ (state_attr('light.hue_bulb_bureau_dirk', 'rgb_color') |list)  == [255,214,181]}} 

meaning:

triggers:
  - trigger: template
    value_template: >-
      {{ (state_attr('light.hue_bulb_bureau_dirk', 'rgb_color') |list)  == [255,214,181]}} 
    enabled: true

Hello @Hellis81,

Thank you for your input. Thanks to that, I’ve adjusted my YAML code:

alias: WERKPLEK_Dirk - Speel muziek bij inschakelen 'Werken' scene
description:
triggers:
  - trigger: template
    value_template: >-
      {{ (state_attr('light.hue_bulb_bureau_dirk', 'rgb_color') |list)  ==
      [255,214,181]}} 
    enabled: true
conditions:
  - condition: state
    entity_id: input_boolean.kill_switch
    state: "off"
actions:
  - action: media_player.media_play
    target:
      entity_id: media_player.sonos_symfonisk_slaapkamer
    data: {}
mode: single

When I set the lightstrip to RGB 255,214,181, the automation doesn’t get triggered.
This is what I see in the Developer tools > States when I filter on light.hue_bulb_bureau_dirk:

min_color_temp_kelvin: 2000
max_color_temp_kelvin: 6535
min_mireds: 153
max_mireds: 500
effect_list: None, candle, fire, prism, sparkle, opal, glisten, unknown, unknown, unknown, unknown, sunrise, sunset
supported_color_modes: color_temp, xy
effect: None
color_mode: color_temp
brightness: 206
color_temp_kelvin: 4347
color_temp: 230
hs_color: 26.722, 28.925
rgb_color: 255, 214, 181
xy_color: 0.401, 0.359
mode: normal
dynamics: none
icon: hue:bulb-classic
friendly_name: Bureau Dirk
supported_features: 44

Any idea what there is still wrong with the template trigger?

Are you sure it’s not the condition that blocks it?
Is a trace created?

Or

What if you make a state trigger on attribute RGB color and use this template as the condition?

alias: WERKPLEK_Dirk - Speel muziek bij inschakelen 'Werken' scene
description:
triggers:
  - trigger: template
    value_template: >-
      {{ is_state_attr('light.hue_bulb_bureau_dirk', 'rgb_color', (255,214,181)) }}
    enabled: true
conditions:
  - condition: state
    entity_id: input_boolean.kill_switch
    state: "off"
actions:
  - action: media_player.media_play
    target:
      entity_id: media_player.sonos_symfonisk_slaapkamer
    data: {}
mode: single

Thanks everybody, I’ve found the solution; remove the “RGB:” like @Mayhem_SWE suggested.

So this YAML code is working now:

alias: WERKPLEK_Dirk - Speel muziek bij inschakelen 'Werken' scene
description:
triggers:
  - trigger: template
    value_template: >-
      {{ state_attr('light.hue_bulb_bureau_dirk','rgb_color') ==(255,214, 181)
      }}
    enabled: true
conditions:
  - condition: state
    entity_id: input_boolean.kill_switch
    state: "off"
actions:
  - action: media_player.media_play
    target:
      entity_id: media_player.sonos_symfonisk_slaapkamer
    data: {}
mode: single

???
My trigger did not have that.

That’s right, but the |list and [] in your trigger shouldn’t be used. At least; I didn’t work for me.

It worked on my bulb

1 Like