Automation - MQTT.Publish Payload with IF, Elif from state

Hi All, i need little help with send MQTT Payload per Automation. I have outside temperature sensor ‘sensor.netatmo_venku_temperature’. If i write in template:

{{states(‘sensor.netatmo_venku_temperature’)}}

show me result for examples: 15
now i need send some Temperature Number to zigbee Valve via MQTT Payload. I tried:

- id: '1568543932325'
  alias: Ventil_test
  trigger:
  - at: '12:00'
    platform: time
  - at: '15:00'
    platform: time
  condition: []
  action:
  - data:
      payload: >-
        {% if {{states('sensor.netatmo_venku_temperature')}} <= '15' %}
           '{"current_heating_setpoint":"19"}'
        {% elif {{states('sensor.netatmo_venku_temperature')}} >= '15' %} 
           '{"current_heating_setpoint":"15"}' 
          {% endif %}
      topic: zigbee2mqtt/0x00158d000192cfd7/set
    service: mqtt.publish

There i have something bad. Can somebody help me? Thank you all

Replace

- data:

with

- data_template:
condition: []
  action:
  -  data_template:
      payload: >-
        {% if {{states('sensor.netatmo_venku_temperature')}} <= '15' %}
           '{"current_heating_setpoint":"19"}'
        {% elif {{states('sensor.netatmo_venku_temperature')}} >= '15' %} 
           '{"current_heating_setpoint":"15"}' 
          {% endif %}
      topic: zigbee2mqtt/0x00158d000192cfd7/set
     service: mqtt.publish

Configuration not valid :frowning:

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ‘:’, got ‘}’) for dictionary value @ data[‘action’][0][‘data_template’][‘payload’]. Got None. (See /config/configuration.yaml, line 98). Please check the docs at Automation - Home Assistant

Try this:

condition: []
action:
  - service: mqtt.publish
    topic: "zigbee2mqtt/0x00158d000192cfd7/set"
    payload_template: >-
      {% if states('sensor.netatmo_venku_temperature')|int <= 15  %}
        "current_heating_setpoint":"19"
      {% else %} 
        "current_heating_setpoint":"15"
      {% endif %}

Invalid config for [automation]: [payload_template] is an invalid option for [automation]. Check: automation->action->0->payload_template. (See /config/configuration.yaml, line 98). Please check the docs at Automation - Home Assistant

so i tried now with added data:

  condition: []
  action:
  - service: mqtt.publish
    data:
     payload_template: >-
        {% if states('sensor.netatmo_venku_temperature')|int >= 15  %}
          "current_heating_setpoint":"21"
        {% else %} 
           "current_heating_setpoint":"24"
        {% endif %}
     topic: "zigbee2mqtt/0x00158d000192cfd7/set"

Configuration is valid, but in log Zigbee2MQTT is:

zigbee2mqtt:error 9/17/2019, 11:35:5
2 AM No converter available for ‘state’ (“current_heating_setpoint”:“21”)

And no success :frowning: I hope, we are closer… :slight_smile:

Unfortunately I don’t know anything about zigbee2mqtt. But it looks like the message you wanted to send is being sent correctly ( "current_heating_setpoint":"21" ). Are you sure the topic and message format is correct?

I have one more automation (only for testing) and this working well.

- id: '1568580869169'
  alias: Ventil_ve4hod_na22°C
  trigger:
  - at: '4:00'
    platform: time
  - at: '23:00'
    platform: time
  condition: []
  action:
  - data: 
      payload: '{"current_heating_setpoint":"22"}'
      topic: zigbee2mqtt/0x00158d000192cfd7/set
    service: mqtt.publish

I really like Home Assistant, but some things are for me against logic :smiley:

Ah I see. Try this:

     payload_template: >-
        {% if states('sensor.netatmo_venku_temperature')|int >= 15  %}
          {"current_heating_setpoint":"21"}
        {% else %} 
          {"current_heating_setpoint":"24"}
        {% endif %}

You are really GOOOD! Thank you! :-):ok_hand: :innocent: There is also full code with automation (Outside Netatmo sensor) and Thermostat Valve Eurotronic Zigbee Spirit. Hopefully it will help someone:

- id: '1568543932325'
  alias: Ventil1_°C_Netatmo_venku
  trigger:
  - at: '6:00'
    platform: time
  - at: '12:00'
    platform: time
  - at: '18:00'
    platform: time
  condition: []
  action:
  - service: mqtt.publish
    data:
     payload_template: >-
        {% if states('sensor.netatmo_venku_temperature')|int >= 18  %}
          {"current_heating_setpoint":"18"}
        {% else %} 
           {"current_heating_setpoint":"21"}
        {% endif %}
     topic: zigbee2mqtt/0x00158d000192cfd7/set
1 Like

Because using data instead of data_template wasn’t the only error in your template. You also did this which is invalid:

{% if {{states('sensor.netatmo_venku_temperature')}} <= '15' %}

You cannot nest templates (i.e. put one template inside of another one).

The reason why this payload failed:

"current_heating_setpoint":"24"

is because zigbee2mqtt was expecting to receive a dictionary which takes this form:

{"key": "value"}

but you supplied it with this (notice the missing braces):

"key": "value"

Neither of the two errors in your automation are directly related to Home Assistant.

  • It’s the Jinja2 template engine that disallows a template within a template.
  • It’s zigbee2mqtt that expects the payload to be in the form of a dictionary.
1 Like

Thank you Taras for “kicking” :slight_smile: . I am playing with HA only 2 Weeks and I know, I still have a lot to learn. Thank you very much for your support :pray:

1 Like

To be fair, that was my fault.