If/else select in automation

Hi, I want to pre-set a selector and on a certain condition send that value to the real selector.

I can set my EV charger to 4 different modes (off, solar, normal and smart).
I made an automation so that on “car connected” the selector switches to one of these four modes. Made it plug and play, so to say. I can’t set the selector if the car is not connected because it defaults to off in that case, and is does not retain any mode after unplugging.
That all works fine, but I’m trying something different now.

I made a helper with identical four modes (off, normal, solar and smart again). That helper stays in any mode I set it in. On condition “car connected” I want to set the REAL selector mode same as the pre-set HELPER selector. I came up with the following but it throws an error at me. Guess I need some help.

key elemenst are:

topic: SmartEVSE/Mode
payload: OFF | SOLAR | NORMAL | SMART
selector helper: input_select.mode_on_carconnect
smartevse real selector: select.smartevse_mode

- id: '1717743814726'
  alias: Set Mode On CarConnect
  description: ''
  trigger:
  - platform: mqtt
    topic: SmartEVSE/EVPlugState
    payload: Connected
  condition: []
  action:
  - service: mqtt.publish
    data:
     topic: SmartEVSE/Set/Mode
     payload_template: >-
        {% if states('input_select.mode_on_carconnect') == "OFF" %}
          {"select.smartevse_mode":"OFF"}
        {% else %} 
           {% if states('input_select.mode_on_carconnect') == "NORMAL" %}
          {"select.smartevse_mode":"NORMAL"}
        {% else %} 
           {% if states('input_select.mode_on_carconnect') == "SOLAR" %}
          {"select.smartevse_mode":"SOLAR"}
        {% else %} 
           {% if states('input_select.mode_on_carconnect') == "SMART" %}
          {"select.smartevse_mode":"SMART"}
        {% endif %}

HA tells me that

TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: ‘endif’. The innermost block that needs to be closed is ‘if’.

You can also (I think) change this to {% elif states... and then it becomes a single if statement (that requires a single {% endif %}.

[edited for typos and clarity]

…must be doing something wrong with the if/else and closing. I don’t see it.

- id: '1717743814726'
  alias: Set Mode On CarConnect
  description: ''
  trigger:
  - platform: mqtt
    topic: SmartEVSE/EVPlugState
    payload: Connected
  condition: []
  action:
  - service: mqtt.publish
    data:
     topic: SmartEVSE/Set/Mode
     payload_template: >-
        {% elif states('input_select.mode_on_carconnect'), "OFF" endif %}
          {"select.smartevse_mode":"OFF"}
        {% elif states('input_select.mode_on_carconnect') == "NORMAL" endif %}
          {"select.smartevse_mode":"NORMAL"}
        {% elif states('input_select.mode_on_carconnect') == "SOLAR" endif %}
          {"select.smartevse_mode":"SOLAR"}
        {% elif states('input_select.mode_on_carconnect') == "SMART" endif %}
          {"select.smartevse_mode":"SMART"}

TemplateSyntaxError: Encountered unknown tag ‘elif’.

the first one still need to be an if - sorry I wasn’t clear on that.

payload_template: >-
        {% if sta
           ^

You certainly are. Read the Jinja documentation rather than guessing.

     payload_template: >-
        {% if states('input_select.mode_on_carconnect') == "OFF"  %}
          {"select.smartevse_mode":"OFF"}
        {% elif states('input_select.mode_on_carconnect') == "NORMAL" %}
          {"select.smartevse_mode":"NORMAL"}
        {% elif states('input_select.mode_on_carconnect') == "SOLAR" %}
          {"select.smartevse_mode":"SOLAR"}
        {% elif states('input_select.mode_on_carconnect') == "SMART" %}
          {"select.smartevse_mode":"SMART"}
        {% endif %}

Or alternatively:

     payload_template: >-
          {{ {"select.smartevse_mode": states('input_select.mode_on_carconnect')} }}
2 Likes

Thx, should have explained that I have a hard time reading (code). My sight is not perfect.
Your solution works like a charm. I was thinking about passing a value from one ID to another. Now I know how to do that a next time. And yes, I’ll study the docs. Thx again.

1 Like

I was a bit to fast…

Using the developer in HA

- id: '1717743814726'
  alias: Mode On CarConnect
  description: ''
  trigger:
  - platform: mqtt
    topic: SmartEVSE/EVPlugState
    payload: Connected
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: SmartEVSE/Set/Mode
      payload_template: {{ {'select.smartevse_mode': states('input_select.mode_on_carconnect')} }}

…gives…

- id: '1717743814726'
  alias: Mode On CarConnect
  description: ''
  trigger:
  - platform: mqtt
    topic: SmartEVSE/EVPlugState
    payload: Connected
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: SmartEVSE/Set/Mode
      payload_template: {'select.smartevse_mode': 'SOLAR'}

…where as the payload should just be ‘SOLAR’ (without the {‘select.smartevse_mode’: …}

Then just:

payload_template: "{{ states('input_select.mode_on_carconnect') }}"

This did the trick. Thx

For some reason the payload_template needs to start with >-
Working code below. Thx to all.

- id: '1717778691507'
  alias: PresetModeCarConnect
  description: ''
  trigger:
  - platform: mqtt
    topic: SmartEVSE/EVPlugState
    payload: Connected
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: SmartEVSE/Set/Mode
      payload_template: >-
        {{ states('input_select.presetmodecarconnect') }}
  mode: single

Not if you surround the template in quotes, like I did.