POC zigbee temp sensors

This commit is contained in:
2026-08-22 09:06:54 +02:00
parent b4405bac25
commit 86b6b4f87b
6 changed files with 708 additions and 0 deletions
+129
View File
@@ -324,3 +324,132 @@ change, rather than continuously. The SNZB-02P publishes `temperature` in °C
and `humidity` as a percentage.
Reference: [Zigbee2MQTT MQTT topics](https://www.zigbee2mqtt.io/guide/usage/mqtt_topics_and_messages.html)
## 7, reading with curl
```bash
curl --silent --max-time 2 "mqtt://192.168.178.247/$topic" | dd bs=1 skip=$((2 + ${#topic})) status=none
{"battery":100,"humidity":56.3,"humidity_calibration":0,"temperature":27.1,"temperature_calibration":0,"update":{"installed_version":8704,"latest_release_notes":null,"latest_source":"https://raw.githubusercontent.com/Koenkk/zigbee-OTA/master/images/Sonoff/snzb-02p_v2.2.0.ota","latest_version":8704,"state":"idle"}}
```
## 8. Store six sensors in RRD files and create graphs
Zigbee2MQTT must retain each sensor's state so the collector can obtain the
latest reading without waiting for a sleeping sensor:
```yaml
device_options:
retain: true
```
Install RRDtool and create a Python virtual environment in this project:
```bash
sudo apt install -y rrdtool python3-venv
cd "$HOME/service_zigbee"
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
```
Edit the supplied `config.yaml`. MQTT settings, collection timing, storage
paths, logging, all six sensors, RRD archives, and graph presentation are
configured in this one file. Replace all five `CHANGE_ME` topics with the
friendly names shown by Zigbee2MQTT:
```bash
nano config.yaml
```
By default, the databases are written to `/var/lib/rrd` and the generated
graphs to `/var/www/html/sensors`. Create those directories and grant the user
running the collector ownership:
```bash
sudo install -d -o "$USER" -g "$USER" /var/lib/rrd
sudo install -d -o "$USER" -g "$USER" /var/www/html/sensors
```
Both paths can be changed under `storage` in `config.yaml`:
```yaml
storage:
data_directory: /var/lib/rrd
graph_directory: /var/www/html/sensors
```
Test one collection cycle:
```bash
.venv/bin/python read_temperature.py --once
```
The script creates one RRD file per sensor in the configured data directory and
three PNG files per sensor in the configured graph directory:
- `*_day.png` covers the last 24 hours.
- `*_month.png` covers the last 31 days.
- `*_two_years.png` covers the last two years.
Each graph contains a filled temperature area, a solid humidity line, and a
dotted battery line. Initially, most of each graph is blank because no historic
samples existed before the RRD was created.
The collector also generates `/var/www/html/sensors/index.html`. It displays
six graphs at a time and has controls for switching between the daily, monthly,
and two-year periods. It remembers the selected period and refreshes the PNGs
at the interval configured under `html.refresh_seconds`.
With a web server serving `/var/www/html`, open:
```text
http://RASPBERRY_PI_IP/sensors/
```
The page settings are configurable in `config.yaml`:
```yaml
html:
filename: index.html
title: Zigbee sensor history
default_period: day
refresh_seconds: 600
```
RRD structure settings are used only when each `.rrd` file is first created.
Changing `rrd.step_seconds`, data sources, or archives does not rewrite an
existing database; preserve or remove the old RRD deliberately before creating
a replacement.
Run the ten-minute collection loop in the foreground with:
```bash
.venv/bin/python read_temperature.py
```
To run it automatically as a user service, copy the supplied service file. It
assumes this repository is located at `$HOME/service_zigbee`:
```bash
mkdir -p "$HOME/.config/systemd/user"
cp zigbee-rrd.service.example "$HOME/.config/systemd/user/zigbee-rrd.service"
systemctl --user daemon-reload
systemctl --user enable --now zigbee-rrd.service
sudo loginctl enable-linger "$USER"
```
Inspect its status and live logs with:
```bash
systemctl --user status zigbee-rrd.service
journalctl --user -u zigbee-rrd.service -f
```
Graphs can be regenerated without collecting a new MQTT reading:
```bash
.venv/bin/python read_temperature.py --graph-only
```
References:
- [RRDtool database creation](https://oss.oetiker.ch/rrdtool/doc/rrdcreate.en.html)
- [RRDtool graph elements](https://oss.oetiker.ch/rrdtool/doc/rrdgraph_graph.en.html)