Set input select automation

Hi,

I try to set an input_select when a modbus sensor changes.
Reverse order works (first automation), I can change the modbus registry when the input_select changes.
Also, when i put the value hardcoded to ‘’ it works.
So the actual problem is the template.
If anyone can review my code and tell me whats wrong, that would be helpfull.

This is my code:

- alias: Set Victron mode from input_select
  trigger:
    platform: state
    entity_id: input_select.victron_mode
  action:
    service: modbus.write_register
    data_template:
      hub: victron_roadship
      unit: 246
      address: 33
      value: >
        {% if is_state("input_select.victron_mode", "Charge from AC only") %}
          1
        {%-elif is_state("input_select.victron_mode", "Invert only") %}
          2
        {%-elif is_state("input_select.victron_mode", "Charge from AC and invert") %}
          3
        {%-elif is_state("input_select.victron_mode", "Off") %}
          4
        {% endif %}

- alias: Set victron mode input_select from modbus sensor
  trigger:
    platform: state
    entity_id: sensor.mode_switch_position_raw
  action:
    service: input_select.select_option
    data:
      entity_id: input_select.victron_mode
      option: >
        {% if is_state("sensor.mode_switch_position_raw", "1" %}
          'Charge from AC only'
        {%-elif is_state("sensor.mode_switch_position_raw", "2" %}
          'Invert only'
        {%-elif is_state("sensor.mode_switch_position_raw", "3" %}
          'Charge from AC and invert'
        {%-elif is_state("sensor.mode_switch_position_raw", "4" %}
          'Off'
        {% endif %}

Cheers,
Tom

you’d need a data_template

is this what would be a correct data_template:

- alias: Set victron mode input_select from modbus sensor
  trigger:
    platform: state
    entity_id: sensor.mode_switch_position_raw
  action:
    service: input_select.select_option
    data:
      entity_id: input_select.victron_mode
      option: 
        data_template: >
          {% if is_state("sensor.mode_switch_position_raw", 1 %}
            Charge from AC only
          {%-elif is_state("sensor.mode_switch_position_raw", 2 %}
            Invert only
          {%-elif is_state("sensor.mode_switch_position_raw", 3 %}
            Charge from AC and invert
          {%-elif is_state("sensor.mode_switch_position_raw", 4 %}
            Off
          {% endif %}

this still does not react.
Tried putting both the value of the is_state as the option value in quotes, both nothing.

1 Like

No, I think this would be more adequate:

action:
  service: input_select.select_option
  data_template:
    entity_id: input_select.victron_mode
    option: >
      {% if is_state("sensor.mode_switch_position_raw", "1" %}
        'Charge from AC only'
      {%-elif is_state("sensor.mode_switch_position_raw", "2" %}
        'Invert only'
      {%-elif is_state("sensor.mode_switch_position_raw", "3" %}
        'Charge from AC and invert'
      {%-elif is_state("sensor.mode_switch_position_raw", "4" %}
        'Off'
      {% endif %}

having said that, you dont have an ‘else’ clause, which really should be there, in case the state is not listed. A safeguard.

or use a condition to only act upon the state being in [‘1’,‘2’,‘3’,‘4’]

now i got

Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected '}', expected ')') for dictionary value @ data['action'][0]['data_template']['option']. Got None. (See /config/configuration.yaml, line 13). Please check the docs at https://home-assistant.io/components/automation/

So it doesnt see anything for option?
I added

{% else %}
  script.noop
{% endif %}

same issue

I should have checked your code more carefully, sorry… your missing all the closing ‘)’ . just as it says in the error :wink:

action:
  service: input_select.select_option
  data_template:
    entity_id: input_select.victron_mode
    option: >
      {% if is_state("sensor.mode_switch_position_raw", "1" )%}
        'Charge from AC only'
      {%-elif is_state("sensor.mode_switch_position_raw", "2") %}
        'Invert only'
      {%-elif is_state("sensor.mode_switch_position_raw", "3") %}
        'Charge from AC and invert'
      {%-elif is_state("sensor.mode_switch_position_raw", "4")%}
        'Off'
      {% endif %}

of course, I didnt check your options, you should take care of that yourself, and make them exactly the same as defined in the options of the input_select.

this is the regular syntax:

  sequence:
    service: input_select.select_option
    data:
      entity_id: input_select.name
      option: Option

if you use a template, use data_template:

  sequence:
    service: input_select.select_option
    data_template:
      entity_id: input_select.name
      option: >
        {{template here}}

the else clause needs to be in the same format as the if’s, and use the same entity_id. You cant simply add a script, in an input_select as option, which is what you are trying to do here.

Thats why I suggested to make sure you only enter the action part if the condition has been met that the only possible states are in the list of options.
Adding

  condition:
    condition: template
    value_template: >
      {{ states('sensor.mode_switch_position_raw') in ['1','2','3','4'] }}

should take care of it.

and then to make it error-proof, change the last elif in else:

action:
  service: input_select.select_option
  data_template:
    entity_id: input_select.victron_mode
    option: >
      {% if is_state("sensor.mode_switch_position_raw", "1" )%}
        'Charge from AC only'
      {%-elif is_state("sensor.mode_switch_position_raw", "2") %}
        'Invert only'
      {%-elif is_state("sensor.mode_switch_position_raw", "3") %}
        'Charge from AC and invert'
      {%-else %} # <-- was option 4, but since is the only left possibility, you can simply use else
        'Off'
      {% endif %}
2 Likes

you are the best.
got it running. thank you!

1 Like

cool, thanks for letting me know, glad to have been of help.

just out of curiosity. belgian/dutch? I see vdb in your handle…
i’m from belgium

Dutch, with 25+ years of career/fuel experience in Belgium/Vlaanderen :wink: