HA to publish a non MQTT sensor to a MQTT broker

I would like an idiots guide to getting HA to publish a sensor, switch or anything else it hold information on, to a MQTT server, where it can then be picked up on a raspberry pi and have some kind of process done to it.
I have got the MQTT broker up and running, I can get it to send a test word to the pi which then turns on a LED. What I cannot do for example, is get HomeAssistant to send via MQTT the values of a thermostat. I understand you put some code into the Config,Yaml, but then what do you do to get the value of the thermostat into the MQTT message ? I feel like an idiot, as I have spent the past two days trying to work it out, and there does not seem to be an idiots guide to doing this, not what the steps are to get HA to send out data via MQTT. (Am I right in assuming that the device I am trying HA to share the information on, does not have to be MQTT itself ?)

I’ll need to know which thermostat values you want to publish.

Until then, the following example demonstrates how to use mqtt.publish to publish the value of a climate entity’s temperature attribute (it can be any other attribute or the entity’s state). Whenever its value changes, it’s published to the specified topic.

- alias: example 
  - platform: state
    entity_id: climate.your_thermostat
    attribute: temperature
  - service: mqtt.publish
      topic: 'your/topic/goes/here'
      payload: '{{ trigger.to_state.attributes.temperature }}'
  mode: single

You can also publish the payload in JSON format thereby allowing it to include the values of multiple attributes (if that’s something you need).

The example would be a sensor.sonoff_1001392a8a_temperature.

What I am having trouble understanding is where do I put this code. When I read all the documentation, it shows a lot of coding, but does not explain what I am meant to do with the code. For example, when reading the mqtt.publish, it talks about testing the set up, but where do I paste the code 'mosquitto_pub -h -t homeassistant/switch/1/on -m “Switch is ON” ’ in the Home Assistant ?

As I said, I need an idiots guide to doing this!

  • Using the Automation Editor, create a new automation.
  • Switch the Automation Editor from Visual to YAML mode.
  • Copy the following YAML and paste it over whatever is currently displayed in the editing window (i.e. overwrite the existing YAML).
alias: example 
  - platform: state
    entity_id: sensor.sonoff_1001392a8a_temperature
      - 'unavailable'
      - 'unknown' 
  - service: mqtt.publish
      topic: 'your/topic/goes/here'
      payload: '{{ trigger.to_state.state }}'
mode: single
  • In the topic option, replace your/topic/goes/here with the topic you intend to use.
  • In the alias option, replace the word “example” with whatever you want to name this automation.
  • Click the Save button.
  • Optionally switch back to Visual mode (to view the automation in that mode) or exit the Automation Editor. The automation is complete and functional.


Automation Basics

You don’t.

The documentation is providing an example of how to publish a payload to a topic using a command-line application called mosquitto_pub. It’s a completely separate application from Home Assistant.

I believe you would be better served by a visual application like MQTT Explorer. It’s an MQTT client that allows you to publish/subscribe to topics. It’s useful for debugging purposes such as confirming that the broker has received a payload published by an MQTT client like Home Assistant (or any other MQTT client). One of its features is the ability to display all of the topics that the broker knows about in a tree-view (makes it easier to understand).

A BIG thank you, although the code you gave me didn’t quite work, it was enough for me to fiddle with and take a small step in the direction of getting Home Assistant to send out some information via MQTT. I now know that my aim to turn an old car dashboard into a home assistant Dashboard, is possible. Although I have a long long way to go to get the amount of power I am consuming to be represented by a car speedometer!

What was wrong with it?

I am very grateful with the help you gave me, when I say it didn’t quite work, it was probably down to how i integrated it into my existing code. I am flying now!

1 Like

Glad to hear it works.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.