Do you have a separate program to help you identify your MQTT tree structure, and where you’d like to view/publish your values? I use MQTT Explorer if I need an easy way to view or push test values. When I know where I want to push/read values and have tested that it works properly in MQTT Explorer, I’ll know my topic and payload that I can call in hass via mqtt.publish, using yaml or a python_script.
If you know your topic and payload, and need yaml or python_script examples, let me know (or someone else can chime in). I can fetch these when I’m at my residence (will be a few hours).
Oh yes I understand my MQTT tree and how to use it, but I don’t understand how to publish a sensor value onto the MQTT broker from within HA. I’ve been using the RESTful sensor to get values and I want to publish them.
That talks about an MQTT Pagethat I think must have gone in the UI revamp. Ah found it under developer tools. However, that is just a one shot publish I think.
I was hoping of an in-built method but I’m getting the feeling there isn’t one so a Node-Red solution beckons.
It might be something to consider to allow the sensor value to be published as part of the sensor configuration itself.
Could be as simple as a mqtt_pub_topic option in the configuration.
Node-RED does this very easily, with the Home Assistant and MQTT palettes installed you can poll HA sensor or event states and publish to MQTT broker automatically, based on time, or on state changes, or combinations. You can also make HA sensors by importing MQTT topics. I have the Node-RED server running on a RPi 3B+ on the network, and I am running more and more Automations with it as it has some nice Boolean logic flows and switches that are a very flexible. Takes a little to understand it, but once you understand the message structure it is awsome. My 2 cents
I’m presuming the OP is wanting ot publish every, or at least, most sensors. Is there a way of grouping a bunch of sensors and if they’re a member of that group they’re all published?
Not really, I create a lot of my sensors by the MQTT platform. Some data sources are slightly easier to get using the RESTful sensor (especially if in XML format), but I was looking to publish (some of) that data.
Yes I do that extensively already.
I was aware I could publish by polling HA with NR, I just wondered if there was an easier way.
On a related issue, can you get a notification when a service is run? I’m using the speedtestdotnet component and I want to publish the result. I don’t want to poll the sensor, I want to know when it has run then publish the latest value.
Not really Sort of. If you have a group of sensors called group.all_sensors and do this:
trigger:
platform: state
entity_id: group.all_sensors
all it will accomplish is to trigger when the group’s state changes; you won’t know which member of the group changed its state. You would have to blindly publish the states of all group members regardless if the member had changed its state or not. That’s not a recommended design pattern.
EDIT
There’s no guarantee that bad practice will even work properly. Here’s a simple failure scenario:
You have a group of binary_sensors, they are all off, so the group’s state is also off.
One of the binary_sensors turns on so now the group’s state is on.
The group’s state-change causes the ‘bad practice’ Template Sensor to publish the state of the group’s members (so far, so good).
Another binary_sensor in the group turns on.
The group’s state is already on so there’s no state-change.
The Template Sensor doesn’t publish anything and there’s the flaw.
However all of this amounts to creating an automation that’s triggered by an event, which is effectively equivalent to an automation that is triggered by a sensor’s state-change (namely the speedtestdotnet sensor you already have).
Effectively equivalent, meaning in both cases you need to use an automation.
Not sure why you would need to publish a value that hasn’t changed since the last time it was published but, yes, triggering based on event detection of the speedtest service will allow you to do that.
If its for speedtestdotnet, you may wish to explore at MQTT Eventstream.
FWIW, if there’s only one sensor, or just one service, that needs to be published then it seems to me that firing up an entire new integration (either MQTT Statestream or Eventstream) is overkill. A single automation will get the job done equally well.
In another browser tab (or window) open the Lovelace UI and turn on a light.
Return to the browser pane containing Developer tools > Events and you’ll see the call_service event details for the service that turned on the light (i.e. light.turn_on).
For example, I have a patio light and here’s the event reported when I turned on the light: