[2024.8.0] MQTT payload_template depreciated

Hi,

Since installing 2024.8.0 I have been getting 2 repairs re: mqtt.publish using topic_template and payload_template.

The error tells me to use topic and payload instead.

However, when I do this, Z2M complains of a badly formed message and ignores it. Mean while both topic_template and payload_template work fine (albeit with the 2 repair notices).

The docs still say to use topic_template and payload_template and I am not sure what to do here.

He is the yaml for my script that sends the message:

  set_z2m_scene:
    alias: 'Lights: Set Z2M scene'
    sequence:
      - service: input_select.select_option
        target:
          entity_id: input_select.light_scene
        data:
          option: "{{ name }}"
      - service: mqtt.publish
        data:
          topic_template: "zigbee2mqtt/{{group}}/set"
          payload_template: "{{ payload }}"
    mode: queued
    icon: mdi:lamps

Here is the part of the automation that forms the message:

- service: script.turn_on
  target:
    entity_id: script.set_z2m_scene
  data:
    variables:
      name: "{{ states('sensor.light_scene_should') }}"
      group: lux_scenes
      payload: >
        {% set should = states('sensor.light_scene_should') %}
        {% set desc = {
          'all_off':  '{"scene_recall": 0 }',
          'hallway':  '{"scene_recall": 1 }',
          'some_on':  '{"scene_recall": 2 }',
          'all_on':   '{"scene_recall": 3 }',
          'accents':  '{"scene_recall": 4 }'
          } %}
        {{ desc[should] }}

Can anyone advise?

Hi there.

According to the documentation just change to topic or payload, respectively.

Regards,
Marcelo

That doesn’t work, hence my question.

See if this helps

        {{ desc[should] | to_json }}

FWIW, topic and topic_template are equivalent; both support templates. Long ago, there was a difference (the first didn’t support templates, the second did).

Use MQTT Explorer to check the value of payload published to topic.

1 Like

Thanks! I’ll try that.

Using ‘payload_template’ works whilst ‘template’ does not so there must be a difference between the two,

payload and payload_template options support templates. There’s no option named template.

Sorry, I meant payload and payload_template.

payload_template works whilst payload does not,

1 Like

Maybe something is amiss in the latest version because I have always used templates with the payload option.

As an experiment, change the name of the Jinja2 variable to something other than “payload” (so it’s not the same as the payload option for mqtt.publish). For example, use msg.

1 Like

That did it! - both payload and payload_template work now.

Thanks so much! I’m kicking myself because I should have spotted that myself, but this highlights what makes this community so great - the friendly extra pairs of eyes (and brain!).

Cheers!

2 Likes

Also got stuck and sorry to say the message I get from the warning is simply not explaining at all about where to locate the issue.

I get 10 warnings coming from
homeassistant/electric/power/… or homeassistant/electric/energy/…
all pointing to some payload_template(xx,y) where xx and y are sort of randomn numbers which give me no idea.

Not to mention that I’, 100% sure that none of my photovoltaic items, let it be sensors, scripts, automations got any linkage to mqtt at all.

Ok it says it’ll work till before 2025.2.0 which is some time till something will fail, but I don’t understand why it’s that compilcated to provide a warning message with a more precise hint about where in hell the problem might be located.

Perhaps someone with deeper knowledge could explain about to read the warning and give some guidance about which direction one should dig into the configurations to get it solved in time.

Changing it from payload_template to just payload doesn’t work for me as it won’t get parsed anymore.

That’s my current template and the text is a json string.

payload_template: “{{ states(‘input_text.klima_ir_code_on’) }}”

I think this may be to do with an integration.

Can you share your code?

Sure, here we go.

type: vertical-stack
cards:
  - type: horizontal-stack
    cards:
      - show_name: true
        show_icon: true
        type: button
        tap_action:
          action: perform-action
          perform_action: mqtt.publish
          data:
            payload_template: '{{ states(''input_text.klima_ir_code_on'') }}'
            topic: zigbee2mqtt/IR Sender Schlafzimmer/set
          target: {}
        name: Klima Schlafzimmer AN
        icon: mdi:home-thermometer
        show_state: false
      - show_name: true
        show_icon: true
        type: button
        tap_action:
          action: call-service
          target: {}
          data:
            qos: 0
            retain: false
            topic: zigbee2mqtt/IR Sender Schlafzimmer/set
            payload_template: '{{ states(''input_text.klima_ir_code_off'') }}'
          perform_action: mqtt.publish
        name: Klima Schlafzimmer AUS
        show_state: false
        icon: mdi:home-thermometer-outline
  - type: horizontal-stack
    cards:
      - type: conditional
        conditions:
          - condition: state
            entity: sensor.klimaanlage_schlafzimmer_power
            state_not: '0'
        card:
          type: entities
          entities:
            - entity: sensor.klimaanlage_schlafzimmer_power


Try this (I made a few changes). If it works, I’ll explain what I did.

type: vertical-stack
cards:
  - type: horizontal-stack
    cards:
      - show_name: true
        show_icon: true
        type: button
        tap_action:
          action: perform-action
          perform_action: mqtt.publish
          data:
            topic: zigbee2mqtt/IR Sender Schlafzimmer/set
            payload: "{{ states('input_text.klima_ir_code_on') }}"
        name: Klima Schlafzimmer AN
        icon: mdi:home-thermometer
        show_state: false
      - show_name: true
        show_icon: true
        type: button
        tap_action:
          action: perform-action
          perform_action: mqtt.publish
          data:
            topic: zigbee2mqtt/IR Sender Schlafzimmer/set
            payload: "{{ states('input_text.klima_ir_code_off') }}"
        name: Klima Schlafzimmer AUS
        show_state: false
        icon: mdi:home-thermometer-outline
  - type: horizontal-stack
    cards:
      - type: conditional
        conditions:
          - condition: state
            entity: sensor.klimaanlage_schlafzimmer_power
            state_not: '0'
        card:
          type: entities
          entities:
            - entity: sensor.klimaanlage_schlafzimmer_power

Doesn’t work. In MQTT the text will not be template processed

set = {{ states(‘input_text.klima_ir_code_on’) }}

I have this same issue in version 2024.9.1

type: custom:button-card
show_name: true
show_icon: true
entity: select.oswietlenie_hol
tap_action:
  action: call-service
  service: mqtt.publish
  data:
    topic: Dom/OswietlenieHol_settimem
    payload: '{{ states("input_number.czas_w_minutach") | float | int }}'
  target: {}

I also tried with evaluate_payload, but without success.

Hi !
I’m using a lot of data_template: and topic: payload: