Post sensor values to MQTT

Is there a simple way to push sensor values to MQTT for other systems to consume?

The only way I can see is by using an automation or by using Node-Red to monitor the state of the sensor or poll it and push the value to MQTT.

Have you attempted using the mqtt.publish service? In my scenario, I can push anything from relay commands to timer commands via a payload_template and topic.

My mqtt commands topic is structured like so: cmnd/[DEVICE]/[COMMAND]
The payload_template would contain the json parameters.

Now, if you are just trying to push a value somewhere, I assume you would be able to push the value anywhere in your MQTT tree via the publish command as long as you have access, and then subscribe to it elsewhere, but I have not tried doing this myself (yet).

This can also be called/scripted by a python_script in hass as well.

How do you trigger it and relate it to a specific sensor?

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.

There are some yaml mqtt publishing examples here that might help you with the syntax in the meantime (the example uses switches to trigger):

If you want to call from python_script, something like this should work:

service_data = { "payload_template": [YOURPAYLOAD], "topic", [YOURTOPIC] }"mqtt","publish", service_data, False)
1 Like

That talks about an MQTT Page that 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


If you want to publish your RESTful sensor’s state to an MQTT topic, use an automation.

- alias: 'Publish sensor value'
    platform: state
    entity_id: sensor.my_sensor
    service: mqtt.publish
      payload: "{{trigger.to_state.state}}"
      topic: whatever/topic/you/want

If you want to publish the states of several sensors yet with only one automation, you could use something like this:

- alias: 'Publish multiple sensor values'
    platform: state
    entity_id: sensor.my_sensor1, sensor.my_sensor2, sensor.my_sensor3
    service: mqtt.publish
      payload: "{{trigger.to_state.state}}"
      topic: "sensor/{{trigger.to_state.object_id}}"

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.

Use the right tool for the job!

Cheers all.

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:

    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.

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.

You can monitor events and detect when a specific service call is executed (see: Event Call_Service).

The speedtestdotnet component exposes one service:


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).

Assuming the value returned is different or is a ‘state change’ any update (even if the value doesn’t change)?

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.

What about

@hsepm, ah, that might do the job. Thanks I’ll experiment tonight.

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.

This is a bit OT, but how do I trigger an automation when a service is run? I must be missing something…