81 lines
4.2 KiB
Plaintext
81 lines
4.2 KiB
Plaintext
|
<!-- { "title": "From InfluxDB to Twitter - daily temperature tweet" } -->
|
||
|
|
||
|
<h1>#title#</h1>
|
||
|
<p>
|
||
|
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.
|
||
|
</p>
|
||
|
|
||
|
<p>
|
||
|
The whole flow of the tweetbot is this:
|
||
|
|
||
|
<img src="files/Screenshot_2019-01-04_08-28-39.png" width="1200"/>
|
||
|
</p>
|
||
|
|
||
|
<p>
|
||
|
It starts with an inject node "Daily at noon" (<a href="files/Screenshot_2019-01-04_08-29-24.png" target="_blank">*</a>), with triggers the whole flow once a day at high noon.
|
||
|
</p>
|
||
|
|
||
|
<p>
|
||
|
The four influx query nodes read data from the InfluxDB using the following queries, each one encapsulated in one influx query node:
|
||
|
|
||
|
<pre><code class="SQL">
|
||
|
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
|
||
|
</code></pre>
|
||
|
|
||
|
See one of this node <a href="files/Screenshot_2019-01-04_08-29-47.png" target="_blank">here</a>.
|
||
|
</p>
|
||
|
|
||
|
<p>
|
||
|
The data in the InfluxDB considered by these queries looks like this:
|
||
|
|
||
|
<pre>
|
||
|
> 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
|
||
|
>
|
||
|
</pre>
|
||
|
|
||
|
<code>pv</code> has the temperature, <code>sv</code> has the battery voltage. In this case it is -1 since this particular thermometer is powered by a power supply from mains.
|
||
|
</p>
|
||
|
|
||
|
|
||
|
<p>
|
||
|
In the following four change nodes (<a href="files/Screenshot_2019-01-04_08-30-06.png" target="_blank">*</a>) 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 (<a href="files/Screenshot_2019-01-04_08-30-27.png" target="_blank">*</a>). This node will collect four messages and put them into a single object with the topic as key and the message as value.
|
||
|
</p>
|
||
|
|
||
|
<p>
|
||
|
This object in turn is input for the function node "TextPreparator" (<a href="files/Screenshot_2019-01-04_08-30-48.png" target="_blank">*</a>) where the tweet is prepared using a small snippet of JavaScript code:
|
||
|
|
||
|
<pre><code class="JavaScript">
|
||
|
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;
|
||
|
</code></pre>
|
||
|
</p>
|
||
|
|
||
|
<p>
|
||
|
Finally this text will be sent to Twitter via the node "Tweet" (<a href="files/Screenshot_2019-01-04_08-31-06.png" target="_blank">*</a>). Done.
|
||
|
</p>
|
||
|
|
||
|
<p>
|
||
|
<img src="files/Screenshot_2019-01-04_09-08-07.png"/>
|
||
|
</p>
|