Just for fun

Home About PubKey

From InfluxDB to Twitter - daily temperature tweet

This is my first attempt to build a tweetbot. I'm using node-red both as development and runtime environment. It comes with dedicated plugins for both InfluxDB and Twitter.

The whole flow of the tweetbot is this:

It starts with an inject node "Daily at noon" (*), with triggers the whole flow once a day at high noon.

The four influx query nodes read data from the InfluxDB using the following queries, each one encapsulated in one influx query node:

select mean(pv) from measured where deviceid = 'Hedge' and kind = 'Temperature' and time > now() - 15m
select mean(pv) from measured where deviceid = 'Hedge' and kind = 'Temperature' and time > now() - 24h
select min(pv) from measured where deviceid = 'Hedge' and kind = 'Temperature' and time > now() - 24h
select max(pv) from measured where deviceid = 'Hedge' and kind = 'Temperature' and time > now() - 24h
See one of this node here.

The data in the InfluxDB considered by these queries looks like this:

> select * from measured where kind = 'Temperature' and deviceid = 'Hedge' limit 10
name: measured
time                           category deviceid kind        pv                 qv sv tv
----                           -------- -------- ----        --                 -- -- --
2018-12-24T12:57:42.749065469Z Outdoor  Hedge    Temperature 5.029083728790283     -1 
2018-12-24T12:58:54.77913537Z  Outdoor  Hedge    Temperature 5.020352363586426     -1 
2018-12-24T13:00:06.754460483Z Outdoor  Hedge    Temperature 5.032242298126221     -1 
2018-12-24T13:01:18.76525238Z  Outdoor  Hedge    Temperature 5.024284362792969     -1 
2018-12-24T13:02:30.767286588Z Outdoor  Hedge    Temperature 5.016140937805176     -1 
2018-12-24T13:03:42.765550192Z Outdoor  Hedge    Temperature 5.0108466148376465    -1 
2018-12-24T13:04:54.774511705Z Outdoor  Hedge    Temperature 5.019856929779053     -1 
2018-12-24T13:06:05.772434549Z Outdoor  Hedge    Temperature 5.020042419433594     -1 
2018-12-24T13:07:16.759739681Z Outdoor  Hedge    Temperature 5.036855697631836     -1 
2018-12-24T13:08:27.757626686Z Outdoor  Hedge    Temperature 4.993878364562988     -1 
pv has the temperature, sv has the battery voltage. In this case it is -1 since this particular thermometer is powered by a power supply from mains.

In the following four change nodes (*) a dedicated topic for the messages from the influx query nodes will be set to merge them into a single key/value object in the following join node (*). This node will collect four messages and put them into a single object with the topic as key and the message as value.

This object in turn is input for the function node "TextPreparator" (*) where the tweet is prepared using a small snippet of JavaScript code:

let output = ""
output += `Die aktuelle Temperatur zuhause ist ${msg.payload.avg15min[0].mean.toFixed(1)}°C.\n`
output += `Die Durchschnittstemperatur der letzten 24 Stunden war ${msg.payload.avg24h[0].mean.toFixed(1)}°C, `
output += `die Tiefsttemperatur war ${msg.payload.min[0].min.toFixed(1)}°C um ${msg.payload.min[0].time.getHours()}:${msg.payload.min[0].time.getMinutes()} Uhr und `
output += `die Höchsttemperatur war ${msg.payload.max[0].max.toFixed(1)}°C um ${msg.payload.max[0].time.getHours()}:${msg.payload.max[0].time.getMinutes()} Uhr.\n`
output += "Powered by @NodeRED and @InfluxDB."
msg.payload = output
return msg;

Finally this text will be sent to Twitter via the node "Tweet" (*). Done.