Help with publishing to MQTT

I have an entity sensor.barns_pv_power (unit of measurement: w ) that I would like to send to my MQTT broker (on a separate RPi), which I will then send to my Node-RED app which is on yet another separate RPi. I understand how those two RPi’s work together.

I have installed the MQTT integration in HA, but I don’t know how to configure it.

Presumably I create a topic name (say “barns_pv”), but what would I put in the Payload field?

Many thanks!


Create an automation with a State Trigger.

alias: example
- platform: state
  entity_id: sensor.barns_pv_power
- service: mqtt.publish
    topic: barns_pv
    payload_template: '{{ trigger.to_state.state }}'
    retain: true

Thank you so much @123 - that works! A quick follow up question though; using an app called MQTT Explorer, I see that the topic comes through as barns_pv, but I would like it to be a subset of emon. What would be the correct syntax for this? Changing the topic to topic: emon.barns_pv doesn’t work, nor does `topic: emon/barns_pv’ .


Try emon/barns_pv

That should work, unless whatever else that is publishing to the emon root topic is doing something unusual with all sub-topics.

We know it should work because the automation succeeded in publishing to barns_pv. Choosing another topic name should not matter.