[Mosquitto Broker] MQTT Sensor Not working

Hello,

I have just started with Home Assistant and I am learning quite a bit.
My project is to send data from ESP32 to Home Assistant. I am using MQTT to send the data. I set up Mosquitto Broker and my ESP32 is sending data on the topic “esp32/BME280/temperature”, which I can listen to under the “Listen to a topic”. However, I cannot seem to add a sensor using these Values.

sensor:
    - platform: mqtt
      name: "Temperature 1"
      state_topic: "esp32/BME280/temperature"
      value_template: "{{ value_json['AM2301'].Temperature }}"
      unit_of_measurement: "°C"

Entry in configuration YAML does not produce an entity when I go to dashboard to add the entry to a sensor chart.

mqtt:
    - sensor:
      - name: "Temperature 2"
        state_topic: "esp32/BME280/temperature"
        value_template: "{{ value_json['AM2301'].Temperature }}"
        unit_of_measurement: "°C"

does produce an entity but the chart does not work.

Thank you in advance.

What seems to be the problem here?

If you are using mqtt rather than api on esphome the device should show up on the integration screen under mqtt. You shouldn’t have to add lines to configuration.yaml. I’m assuming you’re using esphome. If not then show the whole json out put as it may just be a parsing problem.

1 Like

I am not using ESPhome. I am just trying to post values received as this leads to another project of mine. Like I said, I can receive the data on esp32/BME280/temperature while I listen to it on the configuration page of MQTT but I am not sure how to take those values and post them in a chart.

and thanks for the reply.

Sorry, I had edited my post just before you replied.

mqtt:
      sensor:

In your bottom example of YAML I think the ‘-’ before sensor isn’t needed. The s of sensor should be under the first t of mqtt. Doing this from phone so proving difficult to edit.
Your top one uses platform: mqtt which has been deprecated.

1 Like

I tried using

mqtt:
 sensor:
  name: "Temperature 2"
  state_topic: "esp32/BME280/temperature"
  value_template: "{{ value_json['AM2301'].Temperature }}"
  unit_of_measurement: "°C"

Still didnt work. Posting my full YAML.


# 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: "Temperature 2"
  state_topic: "esp32/BME280/temperature"
  value_template: "{{ value_json['AM2301'].Temperature }}"
  unit_of_measurement: "°C"

mqtt:
  sensor:
    -  name: "Temp"

mqtt:
     - sensor: 
           name: "Temp"

It looks like either of these formats works. Top one works for me in latest version of HA.

It may well be your templetating of the json. Show the json output of esp32/BME280/temperature mqtt message. The parsing of these can be tricky.
Is it a AM2301 or a bmp280 or both you’re using? These formatted texts i type are not showing correctly on my phone. Indentations all wrong.

1 Like

Okay. Thank you for the info. i think I might have just cracked it. But to me safe I just reformated the HA OS and reinstalled it. Running on Raspberry Pi with the latest version.

At first, I was sending the values from Arduino as a String “char tempString[8]” and publishing it on a topic. So i guess the reason my sensor wasn’t working is because of the value template?

But after reading your message i retraced my steps and realised I should be sending in the message as a json. Which I did. and Extracting from MQTT Explorer my data is being shown as

{
“tempF”: 76.27999878,
“humi”: 62.23632813,
“pressure”: 29.93196678
}

Now I think I just need to correct the value template to extract each sensor value and pass it to the sensor.

And I don’t know how to do that because I have never worked with templating before.

  value_template: "{{  value_json.tempF }}"

Try this. Just 1 space before value_json. Phone key problem ob my end.

1 Like

Wow, It worked. Thank you so much!!!

Glad it worked. :+1:

1 Like