Some Bits: Nelson's log

5 posts about grafana

Shelly heat/temperature + Grafana

I bought a little Wi-Fi thermometer, a Shelly H+T. Neat product; eInk display, wifi, runs on 4 AA batteries for a year. I don’t know much about Shelly but it seems fairly hacker friendly. In particular you can run the thing in local network mode and not use their cloud service at all.

Setting up Shelly

First challenge was getting the device up and running. The docs are good and there’s several routes. I went the WiFi only route (no Bluetooth); you plug the device in, get it into AP config mode by holding the reset button in the battery compartment. It creates a WiFi network for itself and runs a web server on 10.33.1.1 which you can then use to configure it. You have to work fast; even plugged in to a USB power source it seems to only want to run for 30 seconds before going in to sleep mode. The main configuration required is to tell it your wifi network details to connect to your LAN.

First step after that was to flash the firmware. Glad I did, they completely changed everything. Next step was to configure it to do something useful. Because it’s low power you can’t really poll the device, most of the time the WiFi is off. So instead it wants to push data.

Shelly is remarkably sophisticated and has a lot of thoroughly documented connection options. I chose MQTT with their default settings because it seemed the simplest path.

Setting up MQTT

I’m not a fan of MQTT, it’s too complicated. But it is a standard and there are tools. I found this guide helpful in getting started. It has you installing mosquitto for a MQTT server (“broker”), mosquitto-clients for testing, then setting up telegraf to be an MQTT listener for data logging to influx and thus Grafana.

mosquitto takes some config; by default it will only listen on localhost. You have to configure authentication. I used a simple username/password, probably “no auth” would be fine on my LAN too. Note this configuration has to occur before a line like listener 1883, defining which mqtt instance you’re setting up. (See what I mean about complicated?)

Then I pointed Shelly at the MQTT broker (no SSL, port 1883, username and password) and.. nothing. There’s a log file in /var/log/mosquitto but it didn’t have much useful in it. So I added connection_messages true to the configuration and then I could at least see that Shelly was connecting. No data logged to a file though.

To get the data you have to use mosquitto_sub to subscribe and listen to the data. There’s a concept of “topics”, which kind of events you listen to; # is a wildcard. You can also use mosquitto_pub to send test messages. Honestly to figure all this out I asked ChatGPT how to use it and it told me. Way easier than reading docs and it happened to work this time. (ChatGPT failed later producing telegraf configs.)

With all that set up I got Shelly logging overnight. It seems to log to two topics: shellyplusht-b8d61a8b17cc/events/rpc and shellyplusht-b8d61a8b17cc/online. The useful data is in the RPC.

The important RPC payload is a JSON blob method named NotifyFullStatus and a wakeup_reason of status_update. The data itself is in params and the key thing is data blocks for temperature and humidity.

Not clear on update frequency. When it was plugged in I was getting updates every 305 seconds or so. There was some status indication that suggests it wants to update every 600 seconds; I guess 300 is when it starts trying and the extra 5 is getting wifi online? On battery it updates less often, I think only when a measurement changes significantly. There’s a setting in the firmware where you can make it wake up for every change of 0.5°C or 1% humidity.

Anyway, Shelly is working and publishing to MQTT. Now to get the data somewhere useful.

Logging to Influx

Telegraph can take input from MQTT. It took some levering to get this working but I finally got telegraf to just store all the fields that are in the JSON data blog from Shelly. Here’s the config:

[[inputs.mqtt_consumer]]
  servers = ["tcp://127.0.0.1:1883"]
  topics = ["shellyplusht-b8d61a8b17cc/events/rpc"]
  username = "shelly"
  password = "hunter2"
  data_format = "json"
  # json_time_key = "params.ts"
  # json_time_format = "unix"
  tag_keys = ["method"]
  json_query = "params"

This logs all RPC messages from shelly; the useful ones with data but also other RPCs. It also logs all the measurements, many of which are not so useful, but it’s just easier this way and it’s only logging every 5 minutes at most.

Note the commented out date parsing. I’m sure I got that JSON key name right but telegraf couldn’t find it, at least in some of its inputs. I gave up. Instead telegraf will log with its own timestamp rather than the one Shelly reported. That’s OK most of the time but if Shelly buffers any data and uploads it in bulk it will be a problem.

Also the “tag_keys” seems to be useless. I was hoping to use influx tags to filter just on the NotifyFullStatus responses and not the other junk. No idea why but it’s not ending up in the database.

I probably should have tried json_v2 as the telegraf parser.

(An aside on testing telegraf with systemd. systemctl reload telegraf does work but sometimes delays, I think telegraf waits until the agent finished its last run. journalctl -f -u telegraf is a useful way to watch the telegraf log file. Shelly only logs every 5 minutes so it’s a slow process of testing.)

Calibration

A man with two clocks never knows what time it is. None of these devices are precisely calibrated so I don’t know for sure what’s right. Shelly has an option in the firmware to add a static offset for calibration so I can at least get it to line up with the Laseregg.

When I first was using it, my LaserEgg said 70.5°F, Shelly said 72.7°F. A Taylor thermoprobe (for cooking) says 71.5°F (both air and a glass of water). In general the Shelly is reading 2-3°F more than the LaserEgg and 2% less humidity.

But then I put Shelly in the fridge for 10 minutes, and it dutifully read colder. Then I took it out. Now 12 hours later Shelly and the LaserEgg are basically reading the same without me putting in a calibration offset at all.

Graph

Here’s 24 hours of my far-too-complex set of graphs of Shelly data. Top two are calibrating temperatures between Shelly and the Laseregg, bottom two are status just from Shelly. The weird dip in temperature is when I locked Shelly in the fridge

Small multiples in Grafana

I learned how to make lots of little graphs in Grafana 8 today. It’s a bit strange but I figured it out thanks to this video on variables and these docs. Here’s a visualization of the power production of all 28 solar panels on my house. Some of those panels aren’t doing a lot of work in the morning; hope it’s better in the summer!

The first step is to declare a variable. You do this in the dashboard settings. In this case I wanted to create a variable named “inverter” which has 28 possible values, one per serial number. I’ve already stored the serial numbers as tags in my Influx data so this Influx Query will get at them

show tag values from pvs6_inverter with key=SERIAL

When you do this Grafana will magically add a little selection UI to your dashboard. You can configure whether there’s an “All” option and whether multiple sections are allowed when you define the variable.

Then you need to make a Grafana panel for the graph that is parameterized by that variable. This is as easy as adding a where SERIAL =~ /$inverter$/ to the query for the graph data; you can do this via the graphical query builder. Once you’ve done this you can choose which graph you want to see, but there’s still only one panel with one graph.

The last step is to enable repetition. This is under “Panel Options / Repeat Options”. You choose which variable to repeat by, then what layout you want. “Horizontal” actually means vertical too; I have it set to 6 max per row and it creates multiple rows automatically. Then go back to the dashboard view and select “All”. You should see all your panels. It’s a little wonky about reloading on changes, you may have to do something to force the UI to refresh.

The last bit of polish I did was to remove all the decorations from the graph I could to get it to display more densely. No title, hide the legend, no Y axis. I switched to the Graph (old) widget because I couldn’t figure out how to hide the X time axis in the new Time Series graph. The only thing I couldn’t do was change the padding of the graphs themselves; there’s more whitespace than I’d like. But it’s not bad and I appreciate how much UI work Grafana does, so nicely.

Grafana and InfluxDB: rollups

Now that I have a few months of data some of my Grafana graphs were getting slow. I realized I’d made no provision for data rollups. I’m recording data every few seconds. Great for a detailed graph of the last 24 hours, but far too fine grained for a report for a full year. Clearly I need some data aggregation / rollups, just like rrdtool did 22 years ago.

Joke’s on me: there’s no easy way to do aggregates or rollups in Grafana + InfluxDB. It’s a frequently requested feature, this discussion from 2016 is the most complete I’ve found. No clear answer but the fact it’s still not present (even in InfluxDB 2.0) speaks volumes. To be fair, doing a general purpose rollup is kind of hard, lots of weird corner cases.

InfluxDB does have reasonable support for creating a rollup table yhourself; see the Downsampling and data retention docs. You set up a continuous query to populate records and then a retention policy to delete old data. (Hysterically, the docs suggest you create the retention policy first; those only run every 30 minutes so you’ll probably get lucky!) But that amounts to creating a second table for the downsampled data. Whatever queries that data is then responsible for selecting the right table (or worse, merging data). There’s a variety of hacks for working with this in Grafana; the usual suggestion is to make the Grafana query select from the value of a variable rather than a static data source, then populate the variable appropriately. I imagine this can be made to work but what a PITA!

Looking at my own problem again I realized that I only had a million points at worst; that’s not awesome but why is the system choking on that? InfluxDB is super fast! Turns out my problem was the weird dashboard I’d imported didn’t have any sort of group_by clause defined. The ordinary Grafana boilerplate includes a group_by($_interval) as part of the query. InfluxDB is still having to read all the records but at least it’s only shipping a rolled up version to Grafana. Once I added that back my existing dashboards got speedy again. Problem avoided. That won’t work for a system with a lot of data, but for a small data system it’s fine.

My Grafana / Starlink setup

I did a bunch of work setting up Grafana to track my Starlink system and promptly forgot about it! Documenting it now.

I’m graphing Starlink stats using InfluxDB and Grafana. Telegraf is not involved.

The data collector is from starlink-gprc-tools. It runs as a background process and writes data in to InfluxDB. I haven’t changed the code from sparky8512’s repo, although I do have my own fork on Github.

I’ve installed a frozen copy in /usr/local/src on my Grafana server. Here’s the “collect” script that fires off the background process. This runs forever, no need for cron:

#!/bin/bash
stats="status usage obstruction_detail alert_detail ping_drop ping_run_length ping_latency"
interval=60

cd /usr/local/src/starlink-grpc-tools
source venv/bin/activate

python ./dish_grpc_influx.py -U starlinkstats -P PASSWORD -t $interval $stats

I haven’t really configured Grafana yet. I found some stock dashboard (in the repo?) that looks like the screenshot below. But it’s storing a lot more stats and I should drill into it at some point.

Telegraf / InfluxDB / Grafana

For years and years and years I’ve been using Munin to monitor servers. Sure it’s old and limited but it still worked reliably and simply without too much software to install. But I’ve finally modernized, mostly so I could collect more data off my Starlink satellite.

I went with the combination of Telegraf, InfluxDB, and Grafana. The so called “TIG stack”. The end result is a system that works remarkably like Munin. Only instead of crappy Perl scripts there’s a clean fast Telegraf agent collecting data. The simple MTRR databases are replaced with InfluxDB, which seems a bit complicated for the purpose but works well. And rrdtool’s old busted graphs are replaced by Grafana, a very fancy enterprise-level dashboard construction system.

I followed a couple of guides for installing stuff: this one and this one. The install breaks down to “install some packages from third party repos” followed by “configure Telegraf and Grafana”.

The configuration is.. not simple. First you have to set up the database with a user, etc. Use the default names! They are baked in to things you will be using either. Then you have to set up Telegraf to log what you need. You can do this incrementally, you don’t have to get it all right at once, but there’s a whole lot of options and things to learn about. I think it took me almost two hours to tweak to what I wanted.

Grafana’s the real monster though. One nice thing about Munin is if you started logging a new number with a plug, the rest of the system would automatically figure out it should graph that number and show you. Not so with Grafana; instead you have to go through a very powerful but complicated web configuration to develop your systems. Fortunately Grafana is pretty good at auto-detecting variables and making some suggestions. The Explore query system is also good at figuring out what you might have in the database.

But the real win with Grafana is you can load other people’s dashboard designs from a community sharing site. For instance, I’m graphing most of my Telegraf system data using this dashboard. Notice how they helpfully supply a Telegraf config too; you can just copy and paste that stuff in and it will probably work.

Anyway, now I finally have some Starlink monitoring using sparky8512’s tools. It doesn’t use Telegraf but has its own little agent to log to InfluxDB directly, plus a sample Grafana dashboard. The dashboard is pretty limited, I’ll probably spend some time configuring my own eventually. But what’s there works for now.

Grafana Starlink screenshot

I was inspired by this Starlink enthusiast and his speedtest charts. Ironically the Starlink setup doesn’t give any speed tests, but I’ve added that using this telegraf plugin. Note if you use that you must install SpeedTest.net’s own client, not the one in the default Ubuntu repos.

All in all I’m pretty happy with the TIG stack. Despite (or maybe because of) all the complexity the new system consumes a lot less CPU than Munin did. And it’s way more flexible and modern. Sometimes it’s good to update to new things. It’s a shame it’s so complicated to configure though; it’s hard to imagine how Ubuntu could ship this as an easy turnkey install.