Multiple possible states = trigger different outcomes?

Hi, how would you go about an automation with multiple states/triggers? As in, if app A is running, set the bulb to color A, if app B=color B, all the way through app Z… Some sort of look up table?

Any ideas? Thanks!

Yes, you can use a dictionary in a templated variable:

trigger:
 - platform: state
   entity_id: however_youre_getting_the_apps
   to: 
condition: []
action:
 - variables:
     app_colors: >-
       {% set d = {
       'app1': '[255,0,0]',
       'app2': '[255,0,255]',
       'app3': '[255,255,0]'
       } %}
       {{ d.get( trigger.to_state.state ) }}
 - service: light.turn_on
   target:
     entity_id: light.ledliststair
   data:
     rgb_color: "{{app_colors}}"

Or a map:

trigger:
  - platform: state
    entity_id: however_youre_getting_the_apps
    to: 
variables:
    app: "{{ trigger.to_state.state }}"
    colors:
      app1: '[255,0,0]'
      app2: '[255,255,0]'
      app3: '[255,0,255]'
    app_color: "{{ colors.get(app) }}"
    valid_state: "{{ app_color is not none }}"
condition:
  - condition: template
    value_template: "{{ valid_state }}"
action:
  - service: light.turn_on
    target:
      entity_id: light.ledliststair
    data:
      rgb_color: "{{app_color}}"

thank you so much!

Cannot get it to actually work. The “to:” line is giving me trouble. I’m not a dev, what’s missing there? (“null” is added by HA). Thanks.

alias: Frontmost app changes light color
description: ''
trigger:
  - platform: state
    entity_id: sensor.hackintosh_frontmost_app
    to: null
condition: []
action:
  - variables:
      app_colors: >-
        {% set d = { 'Microsoft Edge': '[54,219,237]', 'Finder': '[28,147,246]',
        'Final Cut Pro': '[192,26,2]', } %} {{ d.get( trigger.to_state.state )
        }}
  - service: light.turn_on
    target:
      entity_id: light.e15_rgb
    data:
      rgb_color: '{{app_colors}}'

The null thing is normal, by setting to: null it limits triggering to just changes of the state of the sensor not changes to its attributes.

Have you checked the automation debug tool?

One thing to try is other color setting methods, not all lights work with rgb_color.

It fails here:

UndefinedError: 'dict object' has no attribute 'to_state'

Am I supposed to replace some entity name in the template? The sensor.hackintosh_frontmost_app entity is correct and returns “Microsoft Edge” right now as expected.

It should work as is…

Are you trying to test this by using the “Run Actions” button? That would cause the error because no trigger object exists when you do that…

You should be able to test it using “Run Actions” by using the state of the sensor in the template instead of a trigger variable:

{% set d = { 'Microsoft Edge': '[54,219,237]', 'Finder': '[28,147,246]',
'Final Cut Pro': '[192,26,2]', } %} {{ d.get( states('sensor.hackintosh_frontmost_app') ) }}

For some reason it doesn’t (nor when testing with “Run Actions” with the code you gave me, nor as the automation itself). I see it as triggered in the log, the frontmost state entity DOES change correctly, I AM able to set color on the bulb with rgb_color but the automation doesn’t seem to do anything. Thanks for your help.

I set up an analog to your sensor and automation and it is working for me. I did a version with rgb values and one with color temp and the values were passed properly in both. I did have to specify a brightness though, so maybe try adding that to you parameters.

alias: Test Vizio app changes light color
description: ''
trigger:
  - platform: state
    entity_id: sensor.vizio_app
condition: []
action:
  - variables:
      app_colors: >-
        {% set d =  {
        '_UNKNOWN_APP': 370, 'Hulu': 300, 
        'Netflix': 250, 'Youtube': 200 } %}
        {{d.get(trigger.to_state.state)}}
  - service: light.turn_on
    target:
      entity_id: light.ecosmart_zb_br30_01
    data:
      brightness: 254
      color_temp: '{{app_colors}}'

On mine, it just doesn’t do anything. I see the new “TRIGGERED” banner in the automation editor as I switch apps, so that’s working correctly.

The trace finishes but the light is still off… Well, that’s probably the end of the line for this for me :slight_smile: Thank you!