MQTT.publish retain flag support

I have an automation where I want to send a status to my MQTT to be picked up by another system/service. I have it working but the retain: true doesn’t work :frowning:

action:
  service: mqtt.publish
  data_template:
    topic: "topic/path"
    payload: > ...script that works...
    retain: true

Doe the publish service not support retain? doesn’t say anything in docs, so i’m guessing not, which really sucks.

It most certainly does work. I’m using it.

Which broker are you using?

How do you know the message is not retained?

@tom_l did i do the code right?

I’m using mosquito on an ubuntu 18 machine. Retain works from other systems. I know it doesn’t have the retain flag as I monitor with MQTT.fx and it says its not set. Also, when I subscribe from another system, I don’t get the value sent by my automation, therefore, retain not set.

@tom_l strange… it started working :blush: i must have had a typo in “retain” or something… would have thought it would error out and not work at all.

1 Like

From the Services page:

retain is definitely supported.

1 Like

I’m experiencing the same odd thing. I set retain: true, but for some reason mqtt.publish is not not setting the parameter.

  • HA: 0.116.4
  • Mosquitto broker 5.1 running as add-in

I use the below automation to listen if I get any lights sending state commands, and use that to trigger an auto discovery on topic ha_discovery

#------------
# Light
#------------
  - id: mqtt_config_entity_creator_light
    alias: mqtt_config_entity_creator_light
    trigger:
      - platform: mqtt
        topic: '+/light/+/state'
    condition:
      condition: template
      value_template: >
        {% set base_topic = trigger.topic.split('/')[0] %}
        {% set entity = trigger.topic.split('/')[2] %} 
        {% set entity_id = 'light.' + base_topic + '_' + entity %}
        {{ states(entity_id) == 'unknown'}}
    action:
      - service: mqtt.publish
        data_template:
          topic: "ha_discovery/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/config"
          payload: >
            {% set base_topic = trigger.topic.split('/')[0] %}
            {% set domain = trigger.topic.split('/')[1] %}
            {% set entity = trigger.topic.split('/')[2] %}
            {"name": "{{ (base_topic + ' ' + entity) | replace('_', ' ') | title }}",
            "payload_off": "off",
            "payload_on": "on",
            "command_topic": "{{ base_topic + '/' + domain + '/' + entity + '/set' }}",
            "state_topic": "{{ base_topic + '/' + domain + '/' + entity + '/state' }}",
            "brightness_state_topic": "{{ base_topic + '/' + domain + '/' + entity + '/brightness' }}",
            "brightness_command_topic": "{{ base_topic + '/' + domain + '/' + entity + '/brightness/set' }}",
            "unique_id": "{{ base_topic + '_' + entity }}" }
          retain: true

When I listen to the topic, I see that the message has not been retained :frowning:. Anyone knowing what goes on here?

I’m having the same issue - did you ever resolve this?

Did you read the topic?