Adding sensor network data to tmux by querying InfluxDB_

November 24, 2021 @11:00

When I get seriously involved in writing things on the computer I tend to go to a full-screen terminal window and bring out tmux. I was a very heavy user of GNU screen for many years but I found the pane splitting in tmux to be more flexible so at some point I switched to it. I ported much of my screen configuration over to maintain the muscle memory of the keybindings. While I was at it I added several widgets to the status bar at the bottom of the screen. These served various purposes over the years, but are mostly just scripts accreting atop one another.

So I heard you liked a shell, so we put a shell in your shell

Today though, I want to focus on the newest fragment, a bit of Python aptly named get-outside-temp that retrieves the current outside temperature from InfluxDB.

#!/usr/bin/env python3
''' get-outside-temp (c) 2021 Matthew J. Ernisse <matt@going-flying.com>
All Rights Reserved

Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the following conditions are met:

    * Redistributions of source code must retain the
      above copyright notice, this list of conditions
      and the following disclaimer.
    * Redistributions in binary form must reproduce
      the above copyright notice, this list of conditions
      and the following disclaimer in the documentation
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
from influxdb import InfluxDBClient

temp_query = '''SELECT LAST("value") * 1.8 + 32 FROM "sensorValue" WHERE
        ("sensorName" = 'Ambient' AND
        "sensorType" = 'TEMP' AND
    "nodeId" = '11')'''

if __name__ == '__main__':
    client = InfluxDBClient(
        'cntrs.internal.ub3rgeek.net',
        8086,
        database='sensenet'
    )
    result = client.query(temp_query)
    for value in result.get_points('sensorValue'):
        print(f"{value['last']:0.2f}°")

The script itself is almost nauseatingly straightforward and the query is based on the query in the Grafana panel that I use to visualize the rest of the sensors on the network.

Sensors in Grafana

The only real gotcha is that get_points yields a generator and so even though I am using the LAST selector which should return only a single measurement the for loop is necessary. I plunked this in along side the other scripts in the tmux status bar and now have up-to-the minute weather data to ignore!

Subscribe via RSS. Send me a comment.