Set Input Select Automation with JSON parsing

I’m trying to define an action that will read the status of the Victron Inverter (through MQTT) and update an Input Select entity accordingly.

The automation is correctly saved and runs whenever there is a mode change. However, the input select value on the UI is not changing …

Any ideas what I’m doing wrong? Here’s my automation action yaml:

service: input_select.select_option
target:
  entity_id: input_select.victron_inverter_mode
data:
  option: |
    {% set obj = "trigger.payload" | from_json %}
    {% if is_state(obj.value, 1) %}
      'Charger Only'
    {%-elif is_state(obj.value, 2) %}
      'Inverter Only'
    {%-elif is_state(obj.value, 3) %}
      'On'
    {%-else %}
      'Off'
    {% endif %}

Victron is formatting the payload as JSON. Example payload:

{"value": 1}

So I’m first converting the JSON to an object and then try to update input select based on the inverter mode. Any ideas on what I’m doing wrong?

After lots of experimentation, I found the solution. is_state can only be used on entities. That was the issue. Hopefully it will one day be useful for some else :slight_smile:

service: input_select.select_option
target:
  entity_id: input_select.victron_inverter_mode
data:
  option: |
    {% set obj = trigger.payload_json %}
    {% if obj.value == 1 %}
      Charger Only
    {%-elif obj.value == 2 %}
      Inverter Only
    {%-elif obj.value == 3 %}
      On
    {%-else %}
      Off
    {% endif %}

For future reference, the documentation for state oriented functions makes it clear that they are all designed to be used with entities.

If you’re interested, here’s another way to do the same thing:

service: input_select.select_option
target:
  entity_id: input_select.victron_inverter_mode
data:
  option: |
    {% set inv_mode = {1: 'Charger Only', 2: 'Inverter Only', 3: 'On'} %}
    {{ inv_mode[trigger.payload_json.value] if trigger.payload_json.value in inv_mode.keys() else 'Off' }}
1 Like