Changing input_select based on MQTT message with different values

i have the following input_select:
neon_mode:
name: Neon Mode
options:
- “Off”
- “Constant”
- “Slow Flicker”
- “Fast Flicker”
- “Alternating Flicker”

but my device publishes these payloads:
“OFF”
“CONSTANT”
“FLICKERSLOW”
“FLICKERFAST”
“ALTERNATE”

i would like to keep the values displayed by the UI, but to change it based on the last values.
the automation:

  • id: neon_selector_changed
    alias: Neon Mode Set
    initial_state: ‘on’
    trigger:
    platform: state
    entity_id: input_select.neon_mode
    action:
    service: “mqtt.publish”
    data_template:
    topic: “home/neon/setmode”
    retain: false
    payload: ‘{{ states(“input_select.gameneon_mode”) }}’

won’t work - is it possible to map the values received from the publsh to the UI selector values?

This will take your selection, remove the space and make it uppercase to match your payloads.

payload: '{{ states("input_select.gameneon_mode").replace(' ','').upper() }}'

thx - is this javascript? what can be used inside the brackets?
is it possible to use a switch case here to do an exact mapping?

just read about it… jijna2 wow i’m impressed - thx! using with if/else now:
payload: >
{% if is_state(‘input_select.gameneon_mode’,‘Off’) %}
OFF
{% elif is_state(‘input_select.gameneon_mode’,‘Constant’)%}
CONSTANT
{% elif is_state(‘input_select.gameneon_mode’,‘Slow Flicker’)%}
FLICKERSLOW
{% elif is_state(‘input_select.gameneon_mode’,‘Fast Flicker’)%}
FLICKERFAST
{% elif is_state(‘input_select.gameneon_mode’,‘Changing Flicker’)%}
ALTERNATE
{% endif %}

how can i do the same for trigger? (trigger.payload received as MQTT message)

No, it’s Jinja, but it’s closest counterpart is python.

Yes, a dictionary:

{% set mydict = { 'a': 1, 'b': 2 } %}

You can use a template trigger

trying to make use of such syntax and can’t make it to be valid - can you see what i’m doing wrong here:

{%set mydict = { ‘Off’:‘OFF’,‘Constant’:‘CONSTANT’, ‘Slow Flicker’:‘FLICKERSLOW’, ‘Fast Flicker’:‘FLICKERFAST’, ‘Changing Flicker’:‘ALTERNATE’}%}
{%mydict[input_select.gameneon_mode]%}

This is probably the safest, using the get method while providing a default to fall back on. If the input select isn’t in the dictionary, ‘OFF’ will be sent.

{% set mydict = { 
    'Off':'OFF',
    'Constant':'CONSTANT',
    'Slow Flicker':'FLICKERSLOW',
    'Fast Flicker':'FLICKERFAST',
    'Changing Flicker':'ALTERNATE' } %}
{{ mydict.get(states('input_select.gameneon_mode'), 'OFF') }}

Thanks :slight_smile: when trying to set the same for the trigger i get error on config validation. was trying to use the example from https://www.home-assistant.io/docs/automation/trigger/#template-trigger :
alias: Set Game Neon Mode Selector
initial_state: ‘on’
trigger:
platform: mqtt
topic: “home/gameneon/mode”
value_template: >
{% set mydict = {
‘OFF’:‘Off’,
‘CONSTANT’:‘Constant’,
‘FLICKERSLOW’:‘Slow Flicker’,
‘FLICKERFAST’:‘Fast Flicker’,
‘ALTERNATE’:‘Changing Flicker’} %}
{{ mydict.get(states(‘trigger.payload’), ‘Off’) }}
action:
service: input_select.select_option
data_template:
entity_id: input_select.gameneon_mode
option: ‘{{ trigger.payload }}’

Configuration invalidCHECK CONFIG

Invalid config for [automation]: [value_template] is an invalid option for [automation]. Check: automation->trigger->0->value_template. (See /config/configuration.yaml, line 46). Please check the docs at https://home-assistant.io/components/automation/

can you see what is wrong here?

Capture

if i remove the value_template section configuration validates ok

if its in the trigger you need to remove the states(’ ') around trigger.payload.

well configuration is fine now but using the following:

Capture

and then using mqtt publish to “home/gameneon/mode” with “FLICKERSLOW”. getting the following errors in home-assistant.log:

2019-04-06 02:18:17 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: ‘trigger’ is undefined
2019-04-06 02:18:54 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: ‘trigger’ is undefined
2019-04-06 02:18:56 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: ‘trigger’ is undefined
2019-04-06 02:18:56 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: ‘trigger’ is undefined
2019-04-06 02:19:24 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: ‘trigger’ is undefined
2019-04-06 02:19:30 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: ‘trigger’ is undefined
2019-04-06 02:19:54 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: ‘trigger’ is undefined
2019-04-06 02:19:58 WARNING (MainThread) [homeassistant.components.input_select] Invalid option: FLICKERSLOW (possible options: Off, Constant, Slow Flicker, Fast Flicker, Changing Flicker)
2019-04-06 02:19:58 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: ‘trigger’ is undefined

what am i missing?

BTW - how do i set code text in code block? can’t see it explained anywhere…

I just looked at the full automation, not just the template. What are you expecting it to do? As it stands, it doesn’t look like it will do much. If you are trying to get it to set the input select, this is what you need:

  - id: game_neon_selector_change
    alias: Set Game Neon Mode Selector
    initial_state: 'on'
    trigger:
      platform: mqtt
      topic: "home/gameneon/mode"
    action:
      service: input_select.select_option
      data_template:
        entity_id: input_select.gameneon_mode
        option: >
          {% set mydict = { 
            'OFF': 'Off'
            'CONSTANT': 'Constant',
            'FLICKERSLOW': 'Slow Flicker',
            'FLICKERFAST': 'Fast Flicker',
            'ALTERNATE': 'Changing Flicker' } %}
          {{ mydict.get(trigger.payload, 'OFF') }}

works perfectly - thx :slight_smile: