I am trying to set up an automation that changes my light color based on TV input, Please help!

I am trying to set up an automation for my lamp to where it will change to a certain color when a certain input is selected on my TV. I have a TCL TV and currently it will change to red whenever the TV is ‘playing’. As I am trying to do it per input, I plan on doing an automation per. Here is my current code that turns the lamp red whenever the TV starts playing:

- id: '1547176144175'
  alias: TCL Netflix
  trigger:
  - entity_id: media_player.49_tcl_roku_tv
    platform: state
    to: 'playing'
  condition:
  - condition: template
    value_template: '{{ states.media_player.49_tcl_roku_tv.attributes.source.Netflix }}'
      source: Netflix
  action:
  - service: script.1547175970405 

and here is a picture of the state attributes that I am trying to use (source and netflix)

Base your action on your roku source inside your action. You don’t even need to worry about conditions if you don’t want to. You’ll just need to use a service template.

So, the condition part of your automation doesn’t look correct. I think you want:

  condition:
    condition: template
    value_template: "{{ is_state_attr('media_player.49_tcl_roku_tv', 'source', 'Netflix') }}"

Having said that, I don’t see why you’d want multiple automations. Why not just have one automation that sets the color based on the source? Something like:

- id: '1547176144175'
  alias: TCL Netflix
  trigger:
    entity_id: media_player.49_tcl_roku_tv
    platform: state
    to: 'playing'
  action:
    service: light.turn_on
    entity_id: light.my_light
    data_template:
      color_name: >
        {{ {'Home': 'black',
            '4K Spotlight': 'blue',
            ...
            'Netflix': 'red',
            ...}
              [trigger.to_state.attributes.source] }}
1 Like

I didn’t know you could do this!

I’ve gotten an error here and I’m not sure why:

  alias: TCL Netflix
  trigger:
  - entity_id: media_player.49_tcl_roku_tv
    platform: state
    to: 'playing'
  condition:
  - condition: template
    value_template: "{{ is_state_attr('media_player.49_tcl_roku_tv', 'source', 'Netflix') }}"
  action:
  - service: light.turn_on
    - entity_id: light.lamp
      data_template:
        color_name: {{ {'Xbox': 'green', 'Switch': 'magenta', 'Netflix': 'red', 'Wii U': 'blue'} [trigger.to_state.attributes.source] }}

service, entity, data should all be lined up

As @forsquirel said, service, entity_id and data_template need to line up. Also, there should only be a dash on the first one (or, in this case, since the action only has one step, no dash is required.) Go back and review how I wrote it.

EDIT: Your next problem is you didn’t quote the color_name template correctly. Either use multi-line YAML (like I wrote it), or put double-quotes around it.

E.g., here is one way to fix what you wrote:

  action:
  - service: light.turn_on
    entity_id: light.lamp
    data_template:
      color_name: "{{ {'Xbox': 'green', 'Switch': 'magenta', 'Netflix': 'red', 'Wii U': 'blue'} [trigger.to_state.attributes.source] }}"

EDIT 2: Also, you don’t seem to have entries for all the possible source values. If it ever is one that you don’t have, it will cause an error. So, I’d suggest this instead:

  action:
  - service: light.turn_on
    entity_id: light.lamp
    data_template:
      color_name: >
        {% set map = {'Xbox': 'green', 'Switch': 'magenta', 'Netflix': 'red', 'Wii U': 'blue'} %}
        {% if trigger.to_state.attributes.source in map %}
          {{ map[trigger.to_state.attributes.source] }}
        {% else %}
          purple
        {% endif %}