Default MQTT publishing behaviour

I notice that a number of entities seem to publish messages to various topics under ha/# - I don’t recall setting this up. Is this determined by each device or integration, or is the publishing of sensor data from entities to MQTT something which is core to HA?

If it’s core to HA, where and how would I configure the topics for a given sensor?

Let me give a concrete example. I have the Emporia energy monitor integration, which pulls data from Emporia cloud. Gladly I’ve noticed it seems to publish data to various topics like ha/sensor/1st_floor_db_12_1min/last_changed. Where do I go in HA to see the MQTT config side of this, i.e. to choose what topic it publishes to?

And I have another device (a zigbee presence sensor, connected to a Sonoff running Tasmota which forwards raw data to Zigbee2MQTT which is installed into HA and creates its own entities in HA - wow, that was long-winded). Anyway this presence sensor does not automatically publish to MQTT. Practically speaking maybe it’s better to use Zigbee2MQTT to publish sensor data to an MQTT topic, but let’s say I wanted to do that from Home Assistant, is there a way to somehow enable any entity in regards to publishing data to MQTT?

I’m aware I could create an automation to do this, my question is largely academic, as I’d like to understand how other entities might be published, at a later date.

Where did you see this # you’re referring to?

Your post is lacking details about your installation and config. You’re also mixing up a few different things, which make it hard to answer. Best to focus on one issue per topic.

Perhaps first a note on MQTT: you have a broker (probably mosquitto). I’m guessing you’re using an add-on for this. Your devices and HA will all connect to it. HA will subscribe to topics (as configured, either by directly configuring MQTT sensors or using other integrations that will make use of the available topics on your broker). Devices determine the topics they publish to. If you get this, some pieces will already fall in place in your mind.

I’m not sure where you’re looking, but I think you’re referring to (without knowing it) the root path of your broker, so the answer is then a trivial yes: you will see everything that publishes any information from that node downwards, including the broker’s own stats.

Have you checked the integration’s settings? I don’t know this device or integration, but it seems a little odd that it would first pull data via the cloud via some other protocol for use in HA and in addition publish data to MQTT. It could be that the cloud data is already made available by them through MQTT and what you’re seeing is just that.

You can call the MQTT publish service from an automation.

Read all of this: MQTT - Home Assistant.

Hi Paul. I’m relatively well experienced with MQTT and have been using it for 15 years (coding in C, mainly using Node-RED, etc.) so my understanding of MQTT is low-level (for the avoidance of confusion, I don’t mean low understanding, I mean I understand the nuts-and-bolts of how it works very well), which is why I struggle with HA and other applications that wrap things up with very little clear explanation as to how it relates to the low level. I didn’t provide detail about how my MQTT broker was installed as my questions were targeted at the presentation layer of the OSI model. However for the avoidance of further confusion, I’m running MQTT broker on a different server, and the Home Assistant MQTT service is configured to point to that server. Not sure how it makes a difference. However I concede it would probably have been sensible to mention my version of Home Assistant: I’m running Hass.io on a Debian virtual machine; Home Assistant 2023.4.2, Supervisor 2023.08.1.

Let me go back to my questions and distill:

  1. Messages are being published from Home Assistant to my MQTT broker. It doesn’t matter to what topic they are being published. My question: within Home Assistant, exactly what is doing the publishing? Home Assistant core, or the Home Assistant integration in question?

I believe you have provided an answer to this question, but it’s still not clear to me. You said

and

Okay so I have 108 devices in Home Assistant, but they do not automatically / by default publish their data to MQTT. If I look at the MQTT integration, its panel says “11 devices”. Were these devices created by the MQTT integration itself, or by third party integrations which make them available to the MQTT entity? This is the area that I’m least clear on. I appreciate this is a new question, not in my original post, but I’m now responding to your response.

I appreciate you cannot know details about the integrations I have installed. Yes I have checked the integration’s configuration. There is no option to specify whether it publishes sensor data to MQTT. Even if I reinstall the integration from scratch, there was no option to specify “publish sensor data to MQTT”. However it magically appears on a sub-topic of ha/# *(Please note, you thought I was getting confused when I wrote ha/#. Please note that hash (“#”) is a wildcard to include any subtopic, I used this notation in my post. Also please note there’s no such thing as “the root path of your broker”, brokers don’t have root paths. It’s possible that a device or application might have a default path prefix (usually we refer to default portions of a topic as prefix or suffix in MQTT land), but there is no such concept as a root path). Anyway, even though you cannot know the details of an integration I have installed, can you comment on how it knows to publish to the same topic prefix as other integrations? Is this some standard that software authors follow, or is this ha topic prefix a setting in Home Assistant somewhere? I certainly can’t find it.

  1. Is there a way except using automations (you told me it’s possible to use an automation to call the MQTT publish service, yes I know that - I even said I know that in my initial post :slight_smile: ) to enable a given entity or device to automatically publish to MQTT? Based on your previous post I am assuming the answer is “no”. But it still doesn’t explain why I’m seeing various different messages from Home Assistant being published to sub-topics of ha/#.

Let me provide more examples. I’m seeing messages on the following topics:

ha/device_tracker/#
I installed the Home Assistant app on my phone, and enabled the option to track my device.

ha/climate/#
I have a Nest and a spa integration, both of which publish climate sensor information to this topic. Again, I can’t find where this is happening in Home Assistant.

ha/sensor/#
I’m getting power monitoring sensor data (as mentioned before, from Emporia integration)

It’s a good point, but I would know if that’s the case because (a) the integration pulls its data by Python from a public web service, so it’s definitely not over MQTT, (b) messages are coming into the ha topic prefix.

Sorry for my overly wordy reply, and I totally accept that there’s a large hole in my understanding of how Home Assistant works, which is why I’m asking these questions. So perhaps if I have framed any of my questions relating to Home Assistant incorrectly then please do point that out. I am coming from using Node Red (standalone Node Red - not within HA), where conceptually there is absolutely nothing that automatically publishes anything to MQTT unless you tell it to do so. Also NR does not have any internal knowledge of devices or entities, unless you program it to do so.

Hope that all makes sense. Clearly I speak a different language to many people who use Home Assistant, so I really appreciate if any respondees can be overly descriptive of Home Assistant functionality, but assume I am technologically minded in most other aspects!

Things that could publish would be e.g. service calls, like turning on/off an MQTT light. The service calls depend on the integration that defines the service calls.

For example, you can define your own MQTT light. It also needs the transport layer, which is provided by the MQTT integration.

That said, there might be integrations that will simplify this for you, e.g. some device that uses a specific schema for their devices (e.g. what the status values should be for a light to be on or off). In this case, you won’t configure the MQTT topics by hand, but provide just same basic details about the device to the integration. Take this integration (a media player) I’ve built for HA. It uses MQTT as the transport, but you don’t need to define all the topics it needs (for service calls, sensors, etc.), because the integration is built to work specifically with Shairport Sync, that has an MQTT interface with it’s schema defined: it knows what to look for and what to send, given some basic configuration.

I find the phrasing problematic, while trying to understand what you’re getting at. Data, e.g. temperature values, will only flow into HA. “Data” that flows out would be more like commands (from service calls). The MQTT integration itself doesn’t do anything more than giving HA a way to connect with an MQTT broker. Other integrations can then use the MQTT integration, or devices connecting to your broker wilil publish a number of sensors using the same device ID, and thus HA will group them. That’s the 11 you’re seeing.

This is relevant, because anything that writes data to an MQTT broker would come from an integration you’re using. Anything that provides data to HA doesn’t need an integration. The point of MQTT is to have this middleman using a standard, independent transport layer (just like HTTP). A device will simply publish data to the broker… To make sense of what’s published from that side, you need an integration that can understand the specific topics, or you will configure it by hand in YAML.HA will combine entities using the same device ID to make a device.