Need help setting up MQTT in my Hass.io/ESPHome configured BME280 Sensor and Remote LED

Tags: #<Tag:0x00007f7802d63bf8>

Hi,
I’m new to Hass.io and ESPHome and am currently trying to transfer my Domoticz/ESPEasy weather station into Hass.io. …but really struggling with the MQTT configuration.

My setup is a Raspberyy Pi 3 b+ with Hass.io v2.12 installed. I’m using ESPHome on 2 x NodeMCU boards to support a BME280 (with Temp, pressure and Humidity) and to send the sensor data to the other unit where a 4 x 20 LED should display the results.

So far, I’ve successfully connected a DHT11 to the unit with the LED and can display the Temp and Humidity sensor values. The other unit (with the BME280) can send THP sensor values to my MQTT.fx to view the sensor data. However, the problems I’m having are that I really don’t understand how to configure the following:

  1. …to configure the BME280 NodeMCU unit to publish the sensor data on an MQTT topic of my choice.

  2. …to configure the NodeMCU unit (with the LED) to subscribe to the other units sensor data so that I can display the remote BME280 sensor data in the LED.

I’ve spent days playing around with the settings but just not making any significant progress.

I’ve provided the following yaml file to show progress so far but would appreciate any help i getting this sorted. If any further details required then more than happy to provide them.

Thanks in anticipation.

The ESPHome unit with the BME280 unit has yaml file…

esphome:
  name: temp_press_humidity
  platform: ESP8266
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
#api:
#  password: 'xxxxxxxxxxxx'

ota:
  password: 'xxxxxxxxxxxxx'
  
wifi:
  ssid: "RobsBillion-2.4g"
  password: "xxxxxxxx"
  manual_ip:
# Set this to the IP of the ESP
  static_ip: 192.168.1.132
# Set this to the IP address of the router. Often ends with .1
  gateway: 192.168.1.254
# The subnet of the network. 255.255.255.0 works for most home networks.
  subnet: 255.255.255.0

i2c:
  sda: D2
  scl: D1
  
sensor:
  - platform: bme280
temperature:
  name: "BME280 Temperature"
  oversampling: 16x
  id: bme28_temperature
pressure:
  name: "BME280 Pressure"
  id: bme28_pressure
humidity:
  name: "BME280 Humidity"
  id: bme28_humidity
address: 0x76
update_interval: 10s

mqtt:
broker: 192.168.1.120
username: robshamqtt
password: xxxxxxxxx
topic_prefix: /robs
log_topic: /house/frontroom/thpsensor

The ESPHome unit with the LED unit has yaml file…

esphome:
  name: rgb_lights
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "RobsBillion-2.4g"
  password: "xxxxxxxxxxxxxxxx"
  manual_ip:
# Set this to the IP of the ESP
  static_ip: 192.168.1.131
# Set this to the IP address of the router. Often ends with .1
  gateway: 192.168.1.254
# The subnet of the network. 255.255.255.0 works for most home networks.
  subnet: 255.255.255.0

# Enable logging
logger:

# Enable Home Assistant API
#api:
#  password: 'xxxxxxxxxxxxxxx'

ota:
  password: 'xxxxxxxxxxxxxx'
  
light:
  - platform: fastled_clockless
chipset: WS2812
pin: D5
num_leds: 12
rgb_order: GRB
name: "FastLED WS2812 Light"
effects:
  - addressable_rainbow:
  - addressable_color_wipe:
  - flicker:
  - strobe:
  - addressable_scan:
  - addressable_twinkle:
      name: Twinkle Effect With Custom Values
      twinkle_probability: 5%
      progress_interval: 4ms
  - addressable_random_twinkle:
  - addressable_fireworks:

i2c:
  sda: D1
  scl: D2

time:
- platform: sntp
  id: my_time

display:
  - platform: lcd_pcf8574
dimensions: 20x4
address: 0x27
lambda: |-
  //it.print("Hello World! Rob...");
  // Let's write a sensor value (let's assume it's 42.1)
  // Print the current time
  it.strftime("%H:%M  %d.%m.%Y", id(my_time).now());
#      it.printf(0, 1, "Temperature: %.1fC  Humidity   : %.1f%%", id(robstemperature).state, id(robshumidity).state);
#      it.printf(0, 1, "Temperature: %.1fC  Humidity   : %.1f%%", id(bme280_temperature).state, id(bme280_humidity).state);
  
sensor:
  - platform: dht
pin: D6
temperature:
  name: "DHT11 Temperature"
  id: robstemperature
humidity:
  name: "DHT11 Humidity"
  id: robshumidity
update_interval: 10s
model: dht11


mqtt:
broker: 192.168.1.120
port: 1883
username: robshamqtt
password: xxxxxxxxxxxxxx
on_message:
    - topic: house/frontroom/thpsensor
      then:
      - logger.log: MQTT is connected!

Update as needed to make some progress.

I uninstalled the MQTT package I was using and cleaned out the configuration.yaml of any mqtt references. I then installed the package “MQTT Server & Web client” v1.1.0 (i.e. the one with Hivemq’s web client) and managed to get the web-client working with test topics. The problem is that I still can’t find the right mqtt confurations settings to use in one of my ESPHome commands as when I upload it to the device i get the mqtt error…

========================= [SUCCESS] Took 27.62 seconds =========================
INFO Successfully compiled program.
INFO Connecting to 192.168.1.132
INFO Uploading /data/temp_press_humidity/.pioenvs/temp_press_humidity/firmware.bin (354368 bytes)
Uploading: [============================================================] 100% Done…

INFO Waiting for result…
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from temp_press_humidity/debug
ERROR Cannot connect to MQTT broker: [Errno -2] Name or service not known

The configuration.yaml mqtt config is…

mqtt:
  broker: 192.168.1.120:1884
  username: robshamqtt
  password: xxxxxxxxxxx
  client_id: hassio-mqtt-client

The MQTT Server & Web Client config looks like…

{
  "ssl": false,
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem",
  "broker": true,
  "allow_anonymous": false,
  "mqttusers": [
    {
      "username": "robshamqtt",
      "password": "xxxxxxxxxxx",
      "readonly": false,
      "topics": [
        "#"
      ]
    }
  ]
}

…and the ESPHome units MQTT config, attempting to publish sensor values, looks like…

mqtt:
  broker: 192.168.1.120:1884
  username: "robshamqtt"
  password: "xxxxxxxxxxxx"
  client_id: hassio-mqtt-client
  log_topic: house/room/

So, I’ve absolutely no idea why I’m getting the above error and if anyone out there knows then please get back to me.
Thanks…

Your static_ip, subnet & gateway need to be indented under wifi.

I’m not having any problems relating to static IPs or wifi connectivity - the copy from the yaml to this forum page lost some of the formatting, but appreciate what you’re saying.

The only problem is related to MQTT config.
Thanks…

Your log topics are different.

You shouldn’t start a topic with / but one of them has it and one doesn’t.

Also, you shouldn’t have multiple mqtt clients with the same ID. It appears your esphome and your hassio share the same ID

your mqtt config is not indented properly.

Thanks for you responses. I’ve now simplified my setup as much as I can. It now only has the ESPHome unit acting as the publisher (i.e. with the BME280 sensor) and the RPi/Hass.io acting as broker. I’ve added log info from the ESPHome unit (now upgraded to v1.13.2) and my MQTT.fx acting as my external client (which is unable to connect).

Hope this helps…

ESPHome unit (MQTT publisher) code…

esphome:
  name: temp_press_humidity
  platform: ESP8266
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: 'xxxxxxxxxxx'

ota:
  password: 'xxxxxxxxxx'
  
wifi:
  ssid: "RobsBillion-2.4g"
  password: "xxxxxxxxx"
  manual_ip:
    # Set this to the IP of the ESP
    static_ip: 192.168.1.132
    # Set this to the IP address of the router. Often ends with .1
    gateway: 192.168.1.254
    # The subnet of the network. 255.255.255.0 works for most home networks.
    subnet: 255.255.255.0

i2c:
  sda: D2
  scl: D1
  
sensor:
  - platform: bme280
    temperature:
      name: "BME280 Temperature"
      oversampling: 16x
      id: bme28_temperature
    pressure:
      name: "BME280 Pressure"
      id: bme28_pressure
    humidity:
      name: "BME280 Humidity"
      id: bme28_humidity
    address: 0x76
    update_interval: 10s

mqtt:
  broker: 192.168.1.120
  port: 1884
  username: "robshamqtt"
  password: "xxxxxxxxx"
  client_id: hassio-mqtt-client
  log_topic: house/room/

ESPHome log details…

========================= [SUCCESS] Took 25.24 seconds =========================
INFO Successfully compiled program.
INFO Connecting to 192.168.1.132
INFO Uploading /data/temp_press_humidity/.pioenvs/temp_press_humidity/firmware.bin (365072 bytes)
Uploading: [============================================================] 100% Done…

INFO Waiting for result…
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from 192.168.1.132 using esphome API
INFO Connecting to 192.168.1.132:6053 (192.168.1.132)
WARNING Initial connection failed. The ESP might not be connected to WiFi yet (Error connecting to 192.168.1.132: timed out). Re-Trying in 1 seconds
INFO Connecting to 192.168.1.132:6053 (192.168.1.132)
INFO Successfully connected to 192.168.1.132
[07:26:21][D][bme280.sensor:197]: Got temperature=23.9°C pressure=999.4hPa humidity=46.6%
[07:26:21][D][sensor:092]: ‘BME280 Temperature’: Sending state 23.88000 °C with 1 decimals of accuracy
[07:26:21][D][sensor:092]: ‘BME280 Pressure’: Sending state 999.43921 hPa with 1 decimals of accuracy
[07:26:21][D][sensor:092]: ‘BME280 Humidity’: Sending state 46.58398 % with 1 decimals of accuracy
[07:26:22][D][api:572]: Client ‘Home Assistant 0.93.2 (192.168.1.120)’ connected successfully!
[07:26:24][I][mqtt:162]: Connecting to MQTT…
[07:26:24][W][mqtt:247]: MQTT Disconnected: TCP disconnected.
[07:26:29][I][mqtt:162]: Connecting to MQTT…
[07:26:29][W][mqtt:247]: MQTT Disconnected: TCP disconnected.
[07:26:31][D][bme280.sensor:197]: Got temperature=23.9°C pressure=999.4hPa humidity=46.8%
[07:26:31][D][sensor:092]: ‘BME280 Temperature’: Sending state 23.88000 °C with 1 decimals of accuracy
[07:26:31][D][sensor:092]: ‘BME280 Pressure’: Sending state 999.43518 hPa with 1 decimals of accuracy
[07:26:31][D][sensor:092]: ‘BME280 Humidity’: Sending state 46.80078 % with 1 decimals of accuracy
[07:26:34][I][mqtt:162]: Connecting to MQTT…
[07:26:34][W][mqtt:247]: MQTT Disconnected: Unknown.
[07:26:34][W][mqtt:247]: MQTT Disconnected: TCP disconnected.
[07:26:39][I][mqtt:162]: Connecting to MQTT…
[07:26:39][W][mqtt:247]: MQTT Disconnected: TCP disconnected.
[07:26:41][D][bme280.sensor:197]: Got temperature=23.9°C pressure=999.5hPa humidity=46.8%
[07:26:41][D][sensor:092]: ‘BME280 Temperature’: Sending state 23.86000 °C with 1 decimals of accuracy
[07:26:41][D][sensor:092]: ‘BME280 Pressure’: Sending state 999.45306 hPa with 1 decimals of accuracy
[07:26:41][D][sensor:092]: ‘BME280 Humidity’: Sending state 46.79883 % with 1 decimals of accuracy
[07:26:44][I][mqtt:162]: Connecting to MQTT…
[07:26:44][W][mqtt:247]: MQTT Disconnected: TCP disconnected.
[07:26:49][I][mqtt:162]: Connecting to MQTT…
[07:26:49][W][mqtt:247]: MQTT Disconnected: TCP disconnected.
[07:26:51][D][bme280.sensor:197]: Got temperature=23.9°C pressure=999.4hPa humidity=46.7%
[07:26:51][D][sensor:092]: ‘BME280 Temperature’: Sending state 23.86000 °C with 1 decimals of accuracy
[07:26:51][D][sensor:092]: ‘BME280 Pressure’: Sending state 999.44421 hPa with 1 decimals of accuracy
[07:26:51][D][sensor:092]: ‘BME280 Humidity’: Sending state 46.74121 % with 1 decimals of accuracy

Configuration.yaml is unchanged as…

mqtt:
  broker: 192.168.1.120
  port: 1884
  username: robshamqtt
  password: xxxxxxxxx
  client_id: hassio-mqtt-client

And finally, my MQTT.fx tool log which is still unable to connect to my broker…

2019-06-02 07:21:37,928  INFO --- Start App                      : Style: LIGHT /styles/mqttfx_theme_light.css
2019-06-02 07:21:38,193  INFO --- ScriptingManager               : Found action with name: Switch Fountain Test
2019-06-02 07:30:31,572  INFO --- BrokerConnectorController      : onConnect
2019-06-02 07:30:31,608  INFO --- MqttFX ClientModel             : MqttClient with ID c0a39a316f2f4a4cbca2ebec77a3dbaa assigned.
2019-06-02 07:30:32,013 ERROR --- MqttFX ClientModel             : Error when connecting
org.eclipse.paho.client.mqttv3.MqttException: Connection lost
	at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:181) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_181]
	at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_181]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source) ~[?:1.8.0_181]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) ~[?:1.8.0_181]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_181]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_181]
	at java.lang.Thread.run(Unknown Source) [?:1.8.0_181]
Caused by: java.io.EOFException
	at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readFully(MqttInputStream.java:148) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?]
	at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:112) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?]
	at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:133) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?]
	... 7 more
2019-06-02 07:30:32,018 ERROR --- MqttFX ClientModel             : Please verify your Settings (e.g. Broker Address, Broker Port & Client ID) and the user credentials!
org.eclipse.paho.client.mqttv3.MqttException: Connection lost
	at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:181) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_181]
	at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_181]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source) ~[?:1.8.0_181]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) ~[?:1.8.0_181]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_181]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_181]
	at java.lang.Thread.run(Unknown Source) [?:1.8.0_181]
Caused by: java.io.EOFException
	at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readFully(MqttInputStream.java:148) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?]
	at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:112) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?]
	at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:133) ~[org.eclipse.paho.client.mqttv3-1.2.0.jar:?]
	... 7 more
2019-06-02 07:30:32,022  INFO --- ScriptsController              : Clear console.
2019-06-02 07:30:32,047 ERROR --- BrokerConnectService           : Connection lost

Re-reading your first post, I think you are going about this in slightly the wrong way. As I understand it you are trying to do this:

esphome sensor<->Mqtt broker<->esphome display

Rather I think this is the correct way:

esphome sensor<->home assistant (via mqtt or api)<–>esp display

In other words the display should be reading a home assistant entity, not trying to interpret some random mqtt message.

Hi nickrout, I think you’ve pretty well summed-up my confusion. I really don’t know what the hass.io building blocks of setting up hass.io need to be to allow me to transfer the BME280 sensor values from one ESPHome unit (the publisher) to another (acting as the subscriber and presenting the sensor values on an LED). I assumed that it’d require an MQTT add-on but now not sure if this is the right way to go.

If you can point me in the right direction then i’d really appreciate it. …or even to an example somewhere, though I’ve done searches over the last few days.
Thanks…

There are two ways of integrating esphome with home assistant, mqtt and the esphome api. It really matters little which one you choose.

Once you create a sensor in esphome, and you have either mqtt or api set up, then the sensor appears as an entity in home assistant.

Displaying an entity such as a temperature is documented here:

I hope you can work through that, any questions, I wouldn’t call myself an expert, but I’ll do what I can :slight_smile:

I think your problem is mqtt client ID being the same as mentioned by @flamingm0e above.
You are using the same name in both configurations.
client_id: hassio-mqtt-client

Yes. This is because you have both devices using the same client ID. Every device that connects to a broker HAS to have a unique ID

Thanks nickrout. The eagle has landed. I hadn’t relaised the ESPHome to Homeassistant to ESPHome was a possible option. Anyway, I now have my temperature, humidity and pressure values being displayed on my ESPHome LED unit. This is a much simpler solution than messing around with MQTT.

Thanks for your help…
Rob

Hi, can you mark as solved your OP? It would be useful for other people.

Hi,

how can I achieve automation/trigger for d1 mini to publish mqtt topic when there is a change in sensor value?

D1 Mini with Ultrasonic sensor - ESP Home firmware
Hassio with MQTT broker.

Dont want to setup D1 mini with sensor update_internval.

Need instant update when change in sensor value.

Thanks
SK

They certainly update immediately using the api, I suspect mqtt will do the same.

For binary sensors I see instant response but for sensor its done through update_interval, I dont want my node connects to HA every 5 seconds with same value, should contact HA only when value changes.

I would experiment with using a very frequent update_interval (say 1 sec) and then applying a filter like https://esphome.io/components/sensor/index.html#sliding-window-moving-average-exponential-moving-average

Actually correction, I think it would be the delta filter which only publishes when a change is greater than the delta value. https://esphome.io/components/sensor/index.html#throttle-heartbeat-debounce-delta

Thanks!!!
Filter - delta worked.

sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Ultrasonic Sensor"
    id: ultra_sen
    update_interval: 1s
    filters:
      - delta: 0.05
1 Like