Help with automation - template mqtt

Hi all!

I would ask you for some help with automation. I’m quite new to HA and not a programmer of any kind. I managed to get automation working based on input select for operating mode for my heating.
I would like to configure automation and input select in my native language, so instead of this:

input_select:
  central_heating_operation_mode_4:
    name: Operating Mode HC OK4
    options:
      - "auto"
      - "off"
      - "on"
      - "low"

I would like to use this:

input_select:
  central_heating_operation_mode_4:
    name: Operating Mode HC OK4
    options:
      - "Auto"
      - "Izključeno"
      - "Vključeno"
      - "Znižano"

For achieveing this I have to change (and use a template) this automation:

- id: ebusd2
  alias: Nastavi ogrevanje OK4
  initial_state: true
  trigger:
    - platform: state
      entity_id: input_select.central_heating_operation_mode_4
  action:
    service: mqtt.publish
    data:
      topic: ebusd/mc.4/OperatingMode/set
      #      retain: true
      payload: "{{ states.input_select.central_heating_operation_mode_4.state }}"

I’m struggling with this automation, but it’s not working:

- id: ebusd2
  alias: Nastavi ogrevanje OK4
  initial_state: true
  trigger:
    - platform: state
      entity_id: input_select.central_heating_operation_mode_4
  action:
    - service: automation.turn_off
      entity_id: automation.get_central_heating_operation_mode_4
    - service: mqtt.publish
      data:
        topic: ebusd/mc.4/OperatingMode/set
        payload:
          "{% set opmode = {'Auto': 'auto', 'Vključeno': 'on', 'Izključeno':
          'off', 'low': 'Znižano', } %} {{ opmode.get('trigger.payload','auto') }}"
    - delay: 00:00:05
    - service: automation.turn_on
      entity_id: automation.get_central_heating_operation_mode_4

It’s not working…anyone have any clues what is wrong or how to properly use templates. I have been studying also the example in HA docs, but couldn’t get it working.

Thank you for your help in advance!

        payload: >
          {% set opmode = {'Auto': 'auto', 'Vključeno': 'on', 'Izključeno': 'off', 'low': 'Znižano', } %}
          {{ opmode.get(trigger.to_state.state, 'auto') }}
1 Like

Thank you @petro for your super fast answer and solution. It’s working!!! Thank you!!

Now I would like to change the similar automation for getting status of operating mode.

- id: ebusd111
  alias: Ogrevanje OK2
  initial_state: true
  trigger:
    - platform: mqtt
      topic: ebusd/mc/OperatingMode
  action:
    - service: input_select.select_option
      target:
        entity_id: input_select.central_heating_operation_mode_2
      data:
        option: "{% set opmode = {'auto': 'Auto', 'on': 'Vključeno', 'off':
          'Izključeno', 'low': 'Znižano', } %} {{ opmode.get(trigger.to_state.state, 'auto') }}"

Still trying to figure this out…:slight_smile:

you’d use the trigger.payload on that one.

Think about what your trigger is. Your trigger is a mqtt trigger, so it’ll have topic, payload, etc. Your other trigger was a state trigger, so that trigger had, to_state, from_state, etc.

2 Likes

Thanks @petro for your help!
Managed to get it only partly working…It’s ‘templating’ only part of the options. But I’m not sure if this partial works because of the translation.
The result from sensor is:
auto (from mqtt) is templated → auto,
on (from mqtt) into → ‘Vklopljen’ (not ‘Vključeno’, which is probably HA translation consequence)
off (from mqtt) into → ‘Izključen’ (not ‘Izključeno’)
low (from mqtt) into → ‘low’

My question is:
Is it better (possible) to template the sensor instead?