# influxDB record an action

**URL:** https://discourse.nodered.org/t/influxdb-record-an-action/39713
**Category:** General
**Created:** [25 January 2021 14:47 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713 "2021-01-25T14:47:02Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)
#### Post date: [25 January 2021 14:47 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/1 "2021-01-25T14:47:03Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/220b2621bd9282d800f8fbbaf8997af0c329326a.png)

Example object from MQTT node upon "vibration detected":  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3a08a4800e6a5ec918b8ec26f7b0273976e4b791.png)  
Note the "action" payload is only sent every so often when it detects vibration.

Contents of one of the function nodes:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/4/a4e0c9159af70f31bfd373f35e7cd5789b96c07e.png)

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a617f0f4b838b4263ade4c71950f5ec0e7609347.png)

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [25 January 2021 14:56 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/2 "2021-01-25T14:56:08Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [25 January 2021 15:00 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/3 "2021-01-25T15:00:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [25 January 2021 15:02 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/4 "2021-01-25T15:02:40Z")

</div>

> [@ghayne](#):
>
> Try enclosing all your Key fields in quotes!  
> "action": msg.payload.action.

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

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [25 January 2021 15:04 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/5 "2021-01-25T15:04:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)
#### Post date: [25 January 2021 15:07 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/6 "2021-01-25T15:07:30Z")

</div>

> [@Colin](#):
>
> Add a debug node showing what you are actually sending to the db.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/9932e7b094b9edf82e25992bd5fda5c602d12191.png)

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

```auto
//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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/d/5d79fe0918736b3f92ac9f4908075ff34600733c.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [25 January 2021 15:30 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/7 "2021-01-25T15:30:29Z")

</div>

> [@877](#):
>
> Is this a good way of doing it?

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

---

<div class="post-metadata">

### Author: ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)
#### Post date: [25 January 2021 15:34 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/8 "2021-01-25T15:34:10Z")

</div>

> [@Colin](#):
>
> 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 🙂

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

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [25 January 2021 15:36 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/9 "2021-01-25T15:36:00Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)
#### Post date: [25 January 2021 16:05 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/10 "2021-01-25T16:05:11Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b1fa57abd874bae8d86d0a27d80cf6ac00039f4d.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [25 January 2021 16:19 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/11 "2021-01-25T16:19:08Z")

</div>

> [@877](#):
>
> I need to fix it in Grafana so it shows more of a square wave

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

---

<div class="post-metadata">

### Author: ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)
#### Post date: [25 January 2021 17:14 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/12 "2021-01-25T17:14:00Z")

</div>

Thanks for the help, much appreciated!

---

<div class="post-metadata">

### Author: ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)
#### Post date: [25 January 2021 19:20 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/13 "2021-01-25T19:20:05Z")

</div>

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!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/451d50b4a6cd6feb14de325e11c337d4b9ef2176.png)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [8 February 2021 19:20 UTC](https://discourse.nodered.org/t/influxdb-record-an-action/39713/14 "2021-02-08T19:20:29Z")

</div>

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