Skip to content

Just a few examples I wrote a while ago #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/rain_to_influx.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Sending rain data (and other sensor data) to influxdb

This script subscribes to a certain topic and sends all incoming values to influxdb database "iotdata". The values are tagged with a sensor id extracted from the topic.

The script:

```javascript
var request = require('request');

subscribe('sensor/+/rainDelta', function (topic,val,obj,prev,msg) {
var sensorId=topic.split("/")[1];
data='rain_amount,sensor='+sensorId+' value='+val;
request.post({
url: 'https://influxdb.example.com/write?db=iotdata',
body: data
});
});
```
44 changes: 44 additions & 0 deletions examples/raingauge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Calculating interval deltas for a rain gauge (and other counters)

This script calculates deltas on each value from my rain gauge. The gauge behaves in the following way:
- if it rained a certain amount (0.45mm), a counter is increased and the total value is sent via mqtt
- the total value is also published every 30 seconds
- if the device is restarted, the total value starts at zero
- the topic where the total value is published is /sensor/SENSORID/rainTotal

This script calculates the delta between two total value changes and handles some special situations:
- a zero delta is ignored (so no unnecessary updates if it's not raining)
- a delta is ignored when its negative (that happens when the device is restarted)
- a delta is ignored when the previous value is more than 10 minutes old (happens when the script did not run for a while, so the rain cannot be assigned to the correct timestamp)
- a delta is ignored when it's >10mm (that should not happen because a delta should be sent every 0.45mm or at least every 30 seconds - it's just a sanity check)

The result is published via /sensor/SENSORID/rainDelta and can directly be stored to a time series database and then aggregated.

This script can easily be used/modified to do the same calculations for any kind of counter that runs over or is reset from time to time.

The script:

```javascript
subscribe('sensor/+/rainTotal', function (topic,val,obj,prev,msg) {
var sensorId=topic.split("/")[1]
var lastValueTopic='sensor/'+sensorId+'/rainLastValue';
var oldValue=getValue(lastValueTopic)
if (oldValue!==undefined) {
var delta=Math.round((val-oldValue.v)*1000)/1000;

if (now()-oldValue.t>1800000) {
log.info("sensor ",sensorId,", delta ",delta,"- delta older than 30 minutes ignored");
} else if (delta<0) {
log.info("sensor ",sensorId,", delta ",delta,"- negative delta ignored");
} else if (delta>10) {
log.info("sensor ",sensorId,", delta ",delta,"- very large delta ignored");
} else if (delta==0) {
log.debug("sensor ",sensorId,", delta ",delta,"- zero delta ignored");
} else {
log.debug("sensor ",sensorId,", delta ",delta);
publish('sensor/'+sensorId+'/rainDelta',delta);
}
}
publish(lastValueTopic,{v:val,t:now()},{retain:true});
});
```