influxDB record an action

Hello all,

I would like to learn how to show sensor events in Grafana, where initial the value is either text (in this case "vibration", or true/false). I was thinking a bar chart with time across the bottom.

In this example. three window vibration sensors are feeding into influxDB:

Example object from MQTT node upon "vibration detected":
image
Note the "action" payload is only sent every so often when it detects vibration.

Contents of one of the function nodes:
image

When I go to Grafana, it shows "no data":

Try enclosing all your Key fields in quotes!
"action": msg.payload.action...........................

Are you not getting an error from InfluxDB when you insert?

Add a debug node showing what you are actually sending to the db.

That isn't necessary when passing field and tag values to the influx out node, giving it an array with two entries.

1 Like

However, you can't plot a graph of the action field, as it is a string. You can only graph numbers.

1 Like

image

This is what I am trying now, to convert the "vibration" to either 1 or 0.
Is this a good way of doing it?

//Convert the vibration payload to 1 or 0 value

if (msg.payload.action == "vibration") {
//shock detected
msg.payload.action = 1;
} 
else {
//nothing detected
msg.payload.action = 0;
}


//Now configure the payload as normal for influxD

msg.payload = [
    {
        action: msg.payload.action,
        strength:  msg.payload.strength,
        batteryPercent: msg.payload.battery,
        linkQuality: msg.payload.linkquality,
        batteryVolts: msg.payload.voltage
    },
    {
        topic: "Xiaomi_1_kitchen",
        make: "aqara",
        location: "kitchen",
        connection: "zigbee",
        power: "battery",
        friendlyname: "Kitchen window"
    }
];

which then sends this:
image

That is ok, you shouuld use === here though, did not the editor suggest that? Also you can make it a bit more concise using
msg.payload = msg.payload.action === "vibration" ? 1 : 0

Note that you will have to drop the existing measurement in influx in order to change the field type from string to number

That's great thanks, I have seen that before now I see it in front of me :slight_smile:

I need to look again how to drop values with certain tags, not sure I could do it before....

You can't drop part of a measurement. You have to drop the whole measurement (in order to change a field type). At least I believe that is the case.

1 Like

Ah that's what I remember too. For now I just changed the key value to "vibration" and will ignore the "action" one.

Next, I need to fix it in Grafana so it shows more of a square wave (if that makes sense), rather than filling in the gaps between 1's and 0's like this:
image

There is a Staircase option available for the whole graph, or you can use the Overrides feature to apply it to just one line.

Thanks for the help, much appreciated!

In case anyone searches in the future, it was fixed by using staircase option as suggested by Colin, and setting fill to "none", where it was previously "linear" - doh!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.