Home Assistant, MQTT & Pico

Some help please!

I’ve modified the code in my weather station to publish json formatted data to HA.

I can see the data coming through to HA when I “Listen to a topic” in MQTT.

What I can’t seem to get working is HA seeing the sensors.

I tried yaml but that didn’t work:

sensor:
  - platform: mqtt
    name: "Temperature"
    state_topic: "home/mqtt_ws"
    unit_of_measurement: "°C"
    value_template: "{{ value_json.temp }}" # Note the spaces.
  - platform: mqtt
    name: "Humidity"
    state_topic: "home/mqtt_ws"
    unit_of_measurement: "%RH"
    value_template: "{{ value_json.rh }}"  # Note the spaces.

How do I get HA to discover the sensors?

Cheers,

Mark

1 Like

Hey @Mark285907,

You could try getting Home Assistant to automatically detect devices by publishing a special config message to the discovery topic. I think it’d look something like:

homeassistant/sensor/ws_temp/config

The payload would be along the lines of:

{
  "name": "Temperature",
  "state_topic": "home/mqtt_ws",
  "unit_of_measurement": "°C",
  "value_template": "{{ value_json.temp }}",
  "unique_id": "ws_temp_01"
}

You’d just have to go into the settings of Home assistant, look for Devices & Services, then MQTT and then configure. The messages need to be retained and each device must have a unique ID.

Hopefully that helps!

1 Like

Thanks Ryan.

I was expecting (maybe incorrectly?) that HA would have tools to integrate an MQTT sensor without coding. Maybe I’ve misunderstood but I thought once I had the device set up in the MQTT broker, I’d then set up each sensor (entity?) in a similar fashion.

Fixed.

I needed to add the unique_id for each sensor in the configuration.yaml file. Restarted HA and I can see the values.


# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

mqtt:
  sensor:
    - name: "Pico OA Temperature"
      unique_id: "pico_oa_temperature"    
      state_topic: "home/mqtt_ws"
      unit_of_measurement: "°C"
      value_template: "{{ value_json.temp }}" # Note the spaces.
    - name: "Pico OA Humidity"
      unique_id: "pico_oa_humidity" 
      state_topic: "home/mqtt_ws"
      unit_of_measurement: "%RH"
      value_template: "{{ value_json.rh }}"  # Note the spaces.
1 Like

Hey @Mark285907,

Glad to see you’re up and running! Best of luck with your project.