Sending (publishing) analog values via MQTT

Is it possible to to take an analog value from a state, and publish it with MQTT? More specifically, I would like to take the temperature from weather underground (’{{ states.sensor.weather_underground_pws.attributes.temp_f }}’) and put the value into a message published my MQTT, to be used by a separate micro-controller.

Thanks in advance

You can use the MQTT publish service to publish a message.
See about using service calls here

From doing a little searching / googling about creating service calls. It’s looks like I need to write a python script to implement this. It appears to do this it needs to reside in the config directory (/custom_components/ However I don’t have a config directory currently.
The .homeassistant directory only contains two directories, “dep”, and “tts”. Do I create the file and somehow tell home assistant where its at through the configuration.yalm file?


I’m not sure why you think you need to write a python script. You can do it in yaml. This is one I have for sending a command to my trv.

      - service: mqtt.publish
          topic: '/energenie/eTRV/Command/{{command}}/{{id}}'
          payload: 'ON'

In all of the examples I have seen, the payload has been a Boolean value, on or off. Can the payload value be a floating point value? Can it be referenced as an attribute, such as ‘{{ states.sensor.weather_underground_pws.attributes.temp_f }}’?


The payload of an MQTT message is a number of bytes, which can be a string, as in my example above, or a string representing a number.

If you supply a number to the payload parameter (or payload_template as you are sending the state of a sensor), it is converted to an ascii equivalent (and so has to be translated back to a number by the receiver).

I don’t have an example of that in yaml, but here is a snippet of an appdaemon script I have, which sends a number

        if abs(current_temp - target_temp) > 1.0:
            adjusted_target = target_temp + self.target_offset
            self.call_service("mqtt/publish", topic = self.target_temp_topic,
                payload = adjusted_target, qos = "1", retain = "true")

ok thank you! I will just need to test pulling the data from the ‘{{ states.sensor.weather_underground_pws.attributes.temp_f }}’. I believe the self.call_service will be very handy

Just to make sure you are aware, this is an appdaemon call. It is only available if you are using appdaemon - which I highly recommend, but it is a whole new subject.

You could use something like this:

  - alias: 'Publish Wunderground Temperature to MQTT'
      - platform: state
        entity_id: sensor.pws_temp_f
      - service: mqtt.publish
          topic: 'wunderground/temperature'
          payload: '{{ states("sensor.pws_temp_f") }}'

Looks like a solid place to start. Thank you!!

Thanks for all the input. The following was the solution that you guys guided me to. This code is inside my automation.yaml file

  • id: a_mqtt_publish_outside_temperature
    alias: “MQTT Publish Outside Temperature”
    • platform: state
      entity_id: sensor.pws_temp_f
    • service: mqtt.publish
      topic: “4917/home_assistant/outside/temperature”
      payload: ‘{“outside_temperature”:{{ states(“sensor.pws_temp_f”) }}}’