Monitor CO2 levels with Prometheus, MQTT, and/or HomeKit
Project description
CO2 monitoring with Prometheus
This reads from the CO2 Meter and makes it available as a Prometheus service. The core logic comes from this hackaday article.
Setup
Note this assumes you are running on a Raspberry Pi running Raspberry Pi OS (Bullseye/Trixie)
- Install Python 3
- Install the monitor with
python3 -m pip install co2mini[homekit](remove[homekit]if you don't use HomeKit) - Set up CO2 udev rules by copying
90-co2mini.rulesto/etc/udev/rules.d/90-co2mini.rules - Set up the service by copying
co2mini.serviceto/etc/systemd/system/co2mini.service - (Optional) Put a configuration file (see Configuration section below) in
/etc/co2mini.env - Run
systemctl enable co2mini.service
Configuration
The /etc/co2mini.env file contains the environment variables used to configure co2mini beyond the defaults.
This is mostly necessary when enabling MQTT.
Example:
MQTT_ENABLED=true
MQTT_BROKER=localhost
MQTT/Home Assistant
The MQTT feature is meant to work with Home Assistant, although nothing bad will happen if you just want to use the MQTT messages directly.
When co2mini starts up, it will send out the discovery message that Home Assistant expects, as well as responding to homeassistant's status when coming online. Be sure those are enabled in the Home Assistant MQTT integration (usually is enabled by default) if you have any issues.
To configure co2mini, the following environment variables are available:
| Variable | Default | Description |
|---|---|---|
NAME |
co2mini |
This is used for the default display name of the device in Home Assistant |
MQTT_ENABLED |
False |
Used to enable/disable sending information over MQTT |
MQTT_BROKER |
localhost |
MQTT Broker hostname |
MQTT_PORT |
1883 |
MQTT broker port number (1883 is the standard MQTT broker port) |
MQTT_USERNAME |
Username for authenticating to MQTT broker (leave blank if no authentication is needed) | |
MQTT_PASSWORD |
Password for authenticating to MQTT broker (leave blank if no authentication is needed) | |
MQTT_DISCOVERY_PREFIX |
homeassistant |
Prefix for sending MQTT discovery and state messages. |
MQTT_RETAIN_DISCOVERY |
False |
Flag to enable setting retain=True on the discovery messages. You probably don't need this. |
MQTT_OBJECT_ID |
co2mini_{HOSTNAME} |
Override for setting the object_id in Home Assistant. Default builds using the hostname of the device. |
Homekit
If you have the homekit dependencies installed, on the first startup you will need to check the logs to get the setup code to integrate with Homekit.
You can find the code using journalctl -u co2mini.service or possibly by checking the status with systemctl status co2mini.service.
Note also that it's sometimes possible that co2mini will have some errors logged and won't be reporting in Homekit anymore.
If this happens, it seems like the easiest thing to do is to remove the device from your homekit, remove the accessory.state file in your home (rm accessory.state) and restart co2mini (sudo systemctl restart co2mini.service) to get a new code to pair.
Special notes for Dietpi users
- Be sure to install
Python3 pipas well (ID130) - Make sure the dietpi user is in
plugdevgroup (sudo usermod -aG plugdev dietpi)
Development
This project assumes you will use uv to configure the virtual environment and manage dependencies. Formatting and linting is handled with ruff. Pre-commit is using prek, though pre-commit would also work (be sure to install it separately).
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file co2mini-0.5.0.tar.gz.
File metadata
- Download URL: co2mini-0.5.0.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd671566b423b2ed8873d3259bbd7269f0392fce84e9ca922919eaa6b485df08
|
|
| MD5 |
1a58456d91b4d1b8294a3c6eb85c575b
|
|
| BLAKE2b-256 |
c951f0064026e3613bc4c63ab49d5775586940fa700f735beedef8976bad66d8
|
Provenance
The following attestation bundles were made for co2mini-0.5.0.tar.gz:
Publisher:
release.yaml on jerr0328/co2mini
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
co2mini-0.5.0.tar.gz -
Subject digest:
dd671566b423b2ed8873d3259bbd7269f0392fce84e9ca922919eaa6b485df08 - Sigstore transparency entry: 1417651055
- Sigstore integration time:
-
Permalink:
jerr0328/co2mini@15e65473e5ce36c4e25228573b1730d5b3432f66 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/jerr0328
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@15e65473e5ce36c4e25228573b1730d5b3432f66 -
Trigger Event:
release
-
Statement type:
File details
Details for the file co2mini-0.5.0-py3-none-any.whl.
File metadata
- Download URL: co2mini-0.5.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ee14b92c15ddfb76195652e641f225f6c0a6e50c5037941f7166d21a4aceb46
|
|
| MD5 |
ca06909cec159793d809ff790cd0cf64
|
|
| BLAKE2b-256 |
74c774268f67e436386d3d09e423ddafffe18d511621cc7ff1d9028e965d226b
|
Provenance
The following attestation bundles were made for co2mini-0.5.0-py3-none-any.whl:
Publisher:
release.yaml on jerr0328/co2mini
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
co2mini-0.5.0-py3-none-any.whl -
Subject digest:
8ee14b92c15ddfb76195652e641f225f6c0a6e50c5037941f7166d21a4aceb46 - Sigstore transparency entry: 1417651058
- Sigstore integration time:
-
Permalink:
jerr0328/co2mini@15e65473e5ce36c4e25228573b1730d5b3432f66 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/jerr0328
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@15e65473e5ce36c4e25228573b1730d5b3432f66 -
Trigger Event:
release
-
Statement type: