Publish environmental data from Enviro+ via MQTT

I purchased an Enviro+ from Pimoroni recently and wrote a Python service to publish all the recorded data via MQTT, so that it can be ingested by Home Assistant.

The repository is here: enviroplus-mqtt

The sensor itself is pretty cool - you can measure lux, proximity, temperature, pressure, humidity, oxidising gas, reducing gas, NH3, PM1.0, PM2.5 and PM10.0. I’ve set one up in my lounge for monitoring indoor climate and will probably purchase another for measuring outdoor climate soon.

2 Likes

greetings:

I came across the (enviroplus-MQTT) listing you posted and i am interested in this concept, since i purchased and enviroplus and would like to graph the data. unfortunately I don’t understand where the MQTT data goes and how you view it or what other software I need to display the graphing data. Can you help me to understand what else I need beside the Enviroplus-MQTT software to view enviroplus data
thanks
Tom

Hi Tom

MQTT itself is just a messaging platform for devices - in this case, the enviroplus-mqtt service is just sending messages with sensor values to a MQTT server of your choosing, and any devices that care to listen can receive those readings. By default, no devices will be listening and no data will be logged or graphed.

I’m assuming you are also using Home Assistant, in which case you should be able to set up an associated MQTT server, and then configure Home Assistant to listen for sensor readings on the appropriate MQTT topics.

Some instructions for setting up an MQTT server can be found in the Home Assistant MQTT Docs, or in the Hass.io MQTT Docs

Once you have an MQTT server running and have configured enviroplus-mqtt to publish sensor readings to that server, you can configure Home Assistant to treat those readings as sensors. I have the following in my config:

sensor:
  - platform: mqtt
    name: 'Lounge Temperature'
    state_topic: 'lounge/enviroplus/temperature'
    unit_of_measurement: '°C'
    value_template: '{{ value | round(1) }}'
    expire_after: 600


  - platform: mqtt
    name: 'Lounge Humidity'
    state_topic: 'lounge/enviroplus/humidity'
    unit_of_measurement: '%'
    value_template: '{{ value | round(1) }}'
    expire_after: 600


  - platform: mqtt
    name: 'Lounge Pressure'
    state_topic: 'lounge/enviroplus/pressure'
    unit_of_measurement: 'hPa'
    value_template: '{{ value | round(2) }}'
    expire_after: 600


  - platform: mqtt
    name: 'Lounge PM1.0'
    state_topic: 'lounge/enviroplus/particulate/1.0'
    unit_of_measurement: 'μg/m³'
    value_template: '{{ value | round(1) }}'
    expire_after: 600


  - platform: mqtt
    name: 'Lounge PM2.5'
    state_topic: 'lounge/enviroplus/particulate/2.5'
    unit_of_measurement: 'μg/m³'
    value_template: '{{ value | round(1) }}'
    expire_after: 600


  - platform: mqtt
    name: 'Lounge PM10.0'
    state_topic: 'lounge/enviroplus/particulate/10.0'
    unit_of_measurement: 'μg/m³'
    value_template: '{{ value | round(1) }}'
    expire_after: 600


  - platform: mqtt
    name: 'Lounge NH3 (Resistance)'
    state_topic: 'lounge/enviroplus/gas/nh3'
    unit_of_measurement: 'Ω'
    value_template: '{{ value | round(1) }}'
    expire_after: 600


  - platform: mqtt
    name: 'Lounge Reducing (Resistance)'
    state_topic: 'lounge/enviroplus/gas/reducing'
    unit_of_measurement: 'Ω'
    value_template: '{{ value | round(1) }}'
    expire_after: 600


  - platform: mqtt
    name: 'Lounge Oxidising (Resistance)'
    state_topic: 'lounge/enviroplus/gas/oxidising'
    unit_of_measurement: 'Ω'
    value_template: '{{ value | round(1) }}'
    expire_after: 600

Those values will probably be recorded to your Home Assistant database (depends on your config), and if so will be available as graphs in the History page.

1 Like

Great work @SamB
I did a pull request for the fix the error with the LTR559.

How are you guys using the screen? I might run the all-in-one.py together.

And as usual, the temperature on the BME280 is to high. How are you guys compensating for it?

I’m experiencing quite unusual values reported via mqtt for the PMS sensor. I have even placed other sensors side by side and the values are way higher.

They don’t even match locally, with another script running.

Hey,
I figure out why the values are wrong. The values currently used are a calculation that does not represent the real number of particles read by the sensor. I did a pull request with a fix, hopefully, @SamB can take a look.
However Pimoroni lack of documentation made this an easy mistake.

As right now I can’t recommend this board to anyone, the temperature values are ridiculously high if you place the board as it was intended to be used, the microphone was never supported, the gas readings are hard to be interpreted, and there is a lack of documentation. Get an ESP32 with these sensors and run ESPeasy or ESPHome.


After a day and a half of continuous running, the readings sent from my enviro+ are 4 hours late.
It looks it longer it runs bigger it gets. I have 2 (esphome) PMS reading side by side, and as you can see on the image, the readings are hours late.
An easy test is to cover the lux meter and see how long it takes to respond.

I thought that it might be related to the delay in publishing the first message, but that doesn’t seem to be the case after running a few minutes with a 600-second initial delay.

Hi @ferazambuja

I have been experiencing similar issues and finally found some time to investigate properly. It looks like the PMS5003 buffers any unread samples, so if we read from the sensor more slowly than it generates readings then we will see a progressively larger delay between actual and indicated PM levels.

I have pushed a change to the repo that reads continuously in a background thread as fast as the PM sensor allows and in my testing the indicated readings now change immediately when I put a candle nearby for example.

Hope that helps

I have put my Enviro+ and PMS5003 inside a case with a fan that circulates fresh air down over the sensors and then back up the other side to cool the RPi.


2 Likes

Hi Sam
Thank you for your post.
I am really a newby, so I got stuck
Can you help with the MQTT syntax? I am using Mosquitto broker on my raspberry and want to “manipulate” data with node-red.
I edited envlogger.services …main.py as -h localhost -p 1883 --prefix ‘enviro’ but I get nothing coming in. Can you write an example … main.py …
I am always in trouble with <> “” -p PORT --p PORT (is it -p 1883 or --p ‘1883’
Thanking you in advance
Pedro

That looks so cool. How are the BME280 readings?
I’m using mine with a Right Angle Female Header, the readings are still higher but it got a bit better.

I will give it a try on the new code :smiley:

@Pedotec if you are having trouble first stop the service:

sudo systemctl stop envlogger.service

For testing run like this

python3 /usr/src/enviroplus-mqtt/src/main.py -h 192.168.1.XXX -p 1883 -U mqtt_broker_username -P password --prefix lounge/enviroplus --interval 5 --use-pms5003

Check with any mqtt software if you are getting the messages or on Hassio on the Developer tools -> MQTT, and listen for lounge/enviroplus/lux.
If you are getting messages is working :+1:

Now follow instructions from the github and add the arguments to envlogger.service

Don’t forget to add the sensors to your config.yaml, there is an example a few posts ago.

Good luck

Thank you @ferazambuja
I followed strictly your instruction and got the message I was getting before Error when connecting to MQTT as response to python3 /usr…main.py
I am going to try Home Assistant install, but see no reason why it shouldn’t work with my Mosquitto broker (I am not using a broker username or password)
Thanks again
Pedro

Is your MQTT working for other devices?
Give it a try with a user name and password. I’m not sure what the code defaults when missing this.
And to be safe type the IP address instead of localhost.

You don’t need to install home assistant for it.

Good luck

I tried the mosquitto broker on my mac; so basically just changed -h 192… for my mac.
Get error before even trying connection:

pi@raspberrypi:~ $ python3 /usr/src/enviroplus-mqtt/src/main.py -h 192.168.1.xxx -p 1883 -U xxx -P xxxx --prefix lounge/enviroplus --interval 5 --use-pms5003
File “/usr/src/enviroplus-mqtt/src/main.py”, line 49
sys.exit(f"Connecting to the MQTT server failed: {logger.connection_error}")
^
SyntaxError: invalid syntax

I had stoped the service…

Does the single line python3… do the job?does it depend on the rest of the service?

The pimoroni instalation is OK , I get nice readings from all the sensors.

So it’s very frustrating!

Thanks again for your help

I believe that the problem is on your mosquitto configuration and not on the enviro+ MQTT.
I’m also on a mac, try MQTT Explorer (it’s available on the app store) or mqtt.fx (less user-friendly).
Test if you are able to connect to your servers with your credentials.

Hello
I’m quite new to python, I managed to get all the demo files running and would also like to send the MQTT results to another RaspPi which has node red, Influx and grafana running.
I’ve installed as per SamB’s instructions and run your test, but I get this error.

Traceback (most recent call last):
File “/usr/src/enviroplus-mqtt/src/main.py”, line 3, in
from logger import EnvLogger
File “/usr/src/enviroplus-mqtt/src/logger.py”, line 8, in
ltr559 = LTR559()
File “/usr/local/lib/python3.7/dist-packages/ltr559/init.py”, line 196, in init
BitField(‘ALS’, 0x0F)
File “/usr/local/lib/python3.7/dist-packages/i2cdevice/init.py”, line 166, in init
self._i2c = smbus.SMBus(1)
FileNotFoundError: [Errno 2] No such file or directory

Could someone give me some advice please?

Thanks in advance
Ian

Hi Ian

Based on the error you are seeing I think you probably don’t have I2C enabled on your Raspberry Pi.

How did you install the enviroplus library from Pimoroni? You may be able to just enable I2C using the raspi-config utility, but if I2C isn’t enabled there may also be other issues with your installation.

In any case, using raspi-config to enable I2C and then rebooting is probably the easiest fix to try initially.

Hey Sam

Thanks for the reply.

I2C is running, I followed this “Tutorial” from a DietPi forum: https://dietpi.com/phpbb/viewtopic.php?f=15&t=7154 (where I also asked for help)

I do also have DietPi running on my PiZero

@ferazambuja @SamB, Thanks for all the input and the work with Enviro+. I am trying to get my Enviro+ to report to HA. I have all Piromani examples running in Pi in Python3 without an issue. I’ve followed all the steps in Repo. When I try to run

sudo systemctl enable envlogger.service

I get:
Failed to enable unit: File envlogger.service: Invalid argument

If I run

python3 /usr/src/enviroplus-mqtt/src/main.py -h 192.168.1.XXX -p 1883 -U xxxusername -P xxpassword --prefix LivingEnviro

I get:

Traceback (most recent call last):
     File "/usr/src/enviroplus-mqtt/src/main.py", Line 3, in <module>
         from logger import EnvLogger
    File "/usr/src/enviroplus-mqtt/src/logger.py", Line 3, in <module>
        import paho.mqtt.client as mqtt
ModuleNotFoundError: No module names 'paho'

Please help,
Thanks in advance

I tried installing paho according to the instructions here:
https://www.eclipse.org/paho/clients/python/

Restarted Pi, but I am still getting the same errors above. Please help!

Hi @manjulapra

Installing paho-mqtt is the correct solution to the second part of your problem above. However, if you ran pip install paho-mqtt as specified in that link it will have been installed for python2 rather than python3. To install for python3, run pip3 install paho-mqtt

Can you paste the output of the command cat /etc/systemd/system/envlogger.service here? You can remove passwords etc