# Adding temp and humidity to influxdb

**URL:** <https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677>\
**Category:** General\
**Created:** [6 January 2021 19:23 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677 "2021-01-06T19:23:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfabernathy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jfabernathy/32/34883_2.png) [@jfabernathy](https://discourse.nodered.org/u/jfabernathy)\
**Post date:** [6 January 2021 19:23 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/1 "2021-01-06T19:23:21Z")

</div>

I've searched a lot and have not hit on what I need yet. I have a Raspberry Pi reading a sensor and sending temperature and humidity readings using MQTT to another RPI which is the Mosqitto Broker and node-red server.  
The data is coming in the format of a JSON object. I've created a MQTT input on nodered that get the data and sends it on to the debug msg object. So every 10 seconds I get a message in the debug window like this:  
1/6/2021, 2:12:12 PM[node: ab69ee34.8aa8d](http://127.0.0.1:1880/#)  
testTopic : msg.payload : Object

object

Temperature: 58.2

Humidity: 40.8

I've tried to create a influxdb out object to add the 2 items to the database but at this point I really don't know what I'm doing. When I connect the MQTT input to the influxdb out, I get stuff in the database but it's wrong. There is only one measurement, Temperature and when I select \* from Temperature I get a lot of data like:  
1609960616822963797 40.8 58.4  
1609960626985944420 40.8 58.4  
1609960637143475475 40.8 58.4  
Where the 40.8 is the humidity and the 58.4 is the temerature.

I feel like I'm just stumbling around. So any advice on how I should be doing this would be appreciated

---

<div class="post-metadata">

**Author:** ![jfabernathy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jfabernathy/32/34883_2.png) [@jfabernathy](https://discourse.nodered.org/u/jfabernathy)\
**Post date:** [6 January 2021 19:52 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/2 "2021-01-06T19:52:30Z")

</div>

I did try to change this up, maybe for the better. I put in 2 influxdb outputs and made one Temperature and one Humidity. I now seem to have 2 Measurements in the influxdb and I can select either measurement and see a list of temperatures and humidity correctly labeled. The flow is:

```auto
[{"id":"ab69ee34.8aa8d","type":"debug","z":"3b73e5ef.a7ba8a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":490,"y":200,"wires":[]}]

```

---

<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:** [6 January 2021 20:44 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/3 "2021-01-06T20:44:42Z")

</div>

I think you had it right the first time. A Measurement in Influx is similar to a table in a conventional database. If you are acquiring two values at the same instant (temp and hum in this case) then the two should go together into one Measurement. I suspect this is what you did the first time, so you had a Measurement called Temperature, which you probably defined in the influx node, and in there you had a number of rows each with a timestamp, temp and hum in it. If you had done  
`select * from Temperature limit 2`  
you would likely have seen the headings at the top showing the column names. So probably the only mistake was to call the Measurement Temperature instead of something more meaningful.

---

<div class="post-metadata">

**Author:** ![jfabernathy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jfabernathy/32/34883_2.png) [@jfabernathy](https://discourse.nodered.org/u/jfabernathy)\
**Post date:** [6 January 2021 21:48 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/4 "2021-01-06T21:48:36Z")

</div>

Thanks for answering. I wonder if both are right. The data before I adding the Humidity variable to the influxdb, displays the same as when only temperature was a variable. The title at the top of the columns is the same. I would have thought that the method of having 2 influxdb outputs would not have had the same time code, but it's the same. I'm going to play with the first method some and use better measurement names . Seems to be a better DB way of doing things.

---

<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:** [6 January 2021 22:03 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/5 "2021-01-06T22:03:11Z")

</div>

Sorry, I can't understand most of that statement.  
There is nothing positively wrong with sending the two values to separate Measurements (in which case the default field name is `value` as you discovered), but if you are acquiring them at the same time the database operation is more efficient if you put them both in the same measurement.

As a slight aside, if you are going to have temperature and humidity values from other locations (other rooms for example) then you should put them all in one Measurement, all with field names temperature and humidity, with a tag (possibly called `room` to identify the location. Then the select statements would read something like  
`select * from environment where room = "bedroom"`

---

<div class="post-metadata">

**Author:** ![jfabernathy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jfabernathy/32/34883_2.png) [@jfabernathy](https://discourse.nodered.org/u/jfabernathy)\
**Post date:** [6 January 2021 22:24 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/6 "2021-01-06T22:24:58Z")

</div>

thanks, good advice

---

<div class="post-metadata">

**Author:** ![jfabernathy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jfabernathy/32/34883_2.png) [@jfabernathy](https://discourse.nodered.org/u/jfabernathy)\
**Post date:** [7 January 2021 02:06 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/7 "2021-01-07T02:06:06Z")

</div>

I do have an additional question. I made the changes and now I see:

```auto
> select * from environmential limit 3
name: environmential
time Humidity Location Temperature
---- -------- -------- -----------
1609974245581971052 45.4 RV 59
1609974305728266776 45.6 RV 59
1609974365868077181 45.6 RV 59
> 

```

But I tried a select by location like you suggested and I can't get it to work.  
This select yields nothing:

```auto
select * from environmential where Location = "RV"
> 

```

---

<div class="post-metadata">

**Author:** ![jfabernathy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jfabernathy/32/34883_2.png) [@jfabernathy](https://discourse.nodered.org/u/jfabernathy)\
**Post date:** [7 January 2021 02:12 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/8 "2021-01-07T02:12:29Z")

</div>

It was the double quotes

```auto
select * from environmential where Location = 'RV' limit 3
name: environmential
time Humidity Location Temperature
---- -------- -------- -----------
1609974245581971052 45.4 RV 59
1609974305728266776 45.6 RV 59
1609974365868077181 45.6 RV 59

```

---

<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:** [7 January 2021 09:53 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/9 "2021-01-07T09:53:25Z")

</div>

Can I just check that you have specified that Location is a tag, not a field. It is important as Influx handles them differently. Tags are indexed so that they can be used in a where clause efficiently. In the help text for the influx out node it tells you how to format the data for fields and tags by passing an array of two objects in msg.payload. The first is the set of fields and the second the tags. So for your situation msg.payload should contain something like

```auto
[
  {Temperature: 25, Humidity: 50},
  {Location: 'RV'}
]

```

---

<div class="post-metadata">

**Author:** ![jfabernathy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jfabernathy/32/34883_2.png) [@jfabernathy](https://discourse.nodered.org/u/jfabernathy)\
**Post date:** [7 January 2021 10:14 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/10 "2021-01-07T10:14:29Z")

</div>

I see what you mean. Here is the raw data:

```auto
 msg.payload : Object

{ Location: "RV", Temperature: 52.3, Humidity: 53.2 }

```

Let me work with that. This is just at test run and once I get it all working I'll fix the formatting as you suggest.

Thanks.

---

<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:** [7 January 2021 10:19 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/11 "2021-01-07T10:19:02Z")

</div>

With the data like that Location is a field which is not indexed, so when you use it in a WHERE clause it will have to read every record in the db and test that value in code. With an indexed field it just has to look up 'RV' in the index and it will know that it needs records 1,2,3,5,35, etc.  
You won't notice the difference with a small set of data but once you get up to a few thousands or even millions of records then you certainly will.

---

<div class="post-metadata">

**Author:** ![jfabernathy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jfabernathy/32/34883_2.png) [@jfabernathy](https://discourse.nodered.org/u/jfabernathy)\
**Post date:** [7 January 2021 11:18 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/12 "2021-01-07T11:18:03Z")

</div>

I'm going to need some help with the formatting. I now have the sender publish this mesage:

```auto
[{"Temperature": 52.1, "Humidity": 53.0},{Location: 'RV'}]

```

The MQTT in can't deal with that format gives a parsing error. If I change it from a parses JSON to string, then influxdb out can't seem to handle it.

1610017802383052824 [{"Temperature": 52.1, "Humidity": 52.9},{Location: 'RV'}]  
1610017862534485962 [{"Temperature": 52.1, "Humidity": 53.0},{Location: 'RV'}]  
1610017922630854092 [{"Temperature": 52.1, "Humidity": 52.9},{Location: 'RV'}]

---

<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:** [7 January 2021 11:59 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/13 "2021-01-07T11:59:58Z")

</div>

> [@jfabernathy](#):
>
> The MQTT in can't deal with that format

What has it got to do with MQTT? that is what you need to send to the influx node.

---

<div class="post-metadata">

**Author:** ![jfabernathy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jfabernathy/32/34883_2.png) [@jfabernathy](https://discourse.nodered.org/u/jfabernathy)\
**Post date:** [7 January 2021 12:09 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/14 "2021-01-07T12:09:36Z")

</div>

MQTT is how I'm getting the data from the remote raspberry pi 3 over wifi to the mosquitto broker and node-red servers, and the influxdb

If I have MQTT just receive it as a string unmodified it gets passed on to the influxdb but it doesn't seem to get added to the database correctly. When I just send over the string to influxdb the select statement produces this output:

```auto
1610018624333263482 {"Temperature": 52.1, "Humidity": 52.9},{Location: 'RV'}
1610018744636223334 {"Temperature": 52.0, "Humidity": 53.0},{Location: 'RV'}

```

---

<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:** [7 January 2021 12:14 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/15 "2021-01-07T12:14:42Z")

</div>

It would be more usual to do the message adjustment for influx in the pi and send it over mqtt as you were before. However if you want to do that show us in a debug node what you are sending to mqtt and what you get out at the other end.

---

<div class="post-metadata">

**Author:** ![jfabernathy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jfabernathy/32/34883_2.png) [@jfabernathy](https://discourse.nodered.org/u/jfabernathy)\
**Post date:** [7 January 2021 12:23 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/16 "2021-01-07T12:23:08Z")

</div>

I'd tried an experiment. I create a node-red inject object to sent a string when clicked. This is the string:

```auto
[{"Temperature": 99.1, "Humidity": 89.0},{Location: 'RV'}]

```

If I look at the database from console with with \> select \* from environmental I see this:

```auto
1610021787158126446 [{"Temperature": 99.1, "Humidity": 89.0},{Location: 'RV'}]
1610021810351416884 [{"Temperature": 99.1, "Humidity": 89.0},{Location: 'RV'}]
> 

```

---

<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:** [7 January 2021 12:26 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/17 "2021-01-07T12:26:06Z")

</div>

That is because you are passing a string to the influx node not an array. So it just adds the string as the field called `value` to the database.

---

<div class="post-metadata">

**Author:** ![jfabernathy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jfabernathy/32/34883_2.png) [@jfabernathy](https://discourse.nodered.org/u/jfabernathy)\
**Post date:** [7 January 2021 12:27 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/18 "2021-01-07T12:27:47Z")

</div>

As to your other question. I have the MQTT in node set with topic environmentialTopic with output "a parsed JSON Object. That is connected to both the influxdb out node and the debug node. The current debug from where the data gets to the DB shows this.

```auto
1/7/2021, 7:24:17 AMnode: ab69ee34.8aa8d
environmentialTopic : msg.payload : Object
object
Location: "RV"
Temperature: 51.7
Humidity: 53
1/7/2021, 7:25:18 AMnode: ab69ee34.8aa8d
environmentialTopic : msg.payload : Object
{ Location: "RV", Temperature: 51.7, Humidity: 53 

```

---

<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:** [7 January 2021 12:28 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/19 "2021-01-07T12:28:48Z")

</div>

If you want to pass the array across via mqtt do it like this

```auto
[{"id":"6271c81c.bb635","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":3980,"wires":[["b4f090e3.744f88"]]},{"id":"b4f090e3.744f88","type":"function","z":"bdd7be38.d3b55","name":"","func":"msg.payload = [{\"Temperature\": 52.1, \"Humidity\": 53.0},{Location: 'RV'}]\nmsg.topic = \"test\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":270,"y":3980,"wires":[["424fea8b.e2252c"]]},{"id":"424fea8b.e2252c","type":"mqtt out","z":"bdd7be38.d3b55","name":"","topic":"","qos":"0","retain":"false","broker":"10e78a89.5b4fd5","x":430,"y":3980,"wires":[]},{"id":"396bccbc.0f042c","type":"mqtt in","z":"bdd7be38.d3b55","name":"","topic":"test","qos":"0","datatype":"json","broker":"10e78a89.5b4fd5","x":80,"y":4060,"wires":[["22c41098.a1b7e"]]},{"id":"22c41098.a1b7e","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":270,"y":4060,"wires":[]},{"id":"10e78a89.5b4fd5","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 January 2021 12:50 UTC](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677/20 "2021-01-07T12:50:37Z")

</div>

Sorry, come to this rather late. However, I provided a detailed view on how to record home environment data in InfluxDB in this previous thread:

[Need more detailed information on influxdb - General - Node-RED Forum](https://discourse.nodered.org/t/need-more-detailed-information-on-influxdb/36885/5)

Including an explanation of the terminology and examples of measurements, tags and fields. Also examples of continuous queries and retention policies in order to keep the data sizes under control.

[Next page](https://discourse.nodered.org/t/adding-temp-and-humidity-to-influxdb/38677.md?page=2)
