# InfluxDB Out node - Define Measurement programmatically

**URL:** <https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525>\
**Category:** General\
**Created:** [12 January 2022 13:24 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525 "2022-01-12T13:24:53Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [12 January 2022 13:24 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/1 "2022-01-12T13:24:53Z")

</div>

Hi There and Happy New Year.

I have a situation where I receive MQTT messages containing the ID of the device forwarding the data and some measured value from the sensors.  
I am attempting to store the measured values in a "Measurement" named like the device.  
So, If I receive a message like "DEV1/something123" I will store "Something123" in Measurement "DEV1".

I see there is a filed in that node to define the Measurement but I don't have idea on how to feed that field without manually writing into it.  
Is there a way to programmatically define the Measurement to use in InfluxDb-Out node?

I am trying avoiding having to use Python to achieve this.

Thanks for helping.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [12 January 2022 14:12 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/2 "2022-01-12T14:12:48Z")

</div>

What does the measurement represent...volts?...temperature?...mpg?...volume?

That should be the name of the 'measurement' and DEV1 should probably be a 'tag'.

You might want to read the 'Measurement', 'Tags & Fields' and 'Timestamp' sections of [this explanation](https://devconnected.com/the-definitive-guide-to-influxdb-in-2019/#b_InfluxDB_Key_Concepts_Explained) before continuing.

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [12 January 2022 14:26 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/3 "2022-01-12T14:26:18Z")

</div>

I understand your point. It is clear. And thanks for showing me an alternative approach.  
The issue I am facing, and this is why I am trying to make one table (Measurement) for each device, is to simplify the next activity which is to select the tags in Grafana.  
If I have one Measurement per device, once I have selected the measurement in Grafana, I'll have only a dozen of measures(tags) to manage inside that table (Measurement).  
If I use the device name as tag and I have 1000 devices, it becomes very messy. Same when I device is replaced or deleted.  
This is why I am trying to make a Measurement per device. I could use Python, but as I said, my first attempt is to verify if I can manage that into nodered.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [12 January 2022 17:18 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/4 "2022-01-12T17:18:56Z")

</div>

> [@FeliceM](#):
>
> If I use the device name as tag and I have 1000 devices, it becomes very messy.

Why would that be messy? This is what a database is designed for. If you need the data from one device you specify it in the where clause. You need all data on a measurement for all devices, use the measurement name in the where clause.

It seems like you don’t have much experience with databases (if I’m wrong, please accept my apology). I (and others) can offer help but you need to explain what you need to store and how you need to access it.

1. why did you choose influx as the database?
2. how much data are you dealing with, thousands of rows, millions?
3. what is the frequency of inserts?
4. how will you be using the data?

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [13 January 2022 02:32 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/5 "2022-01-13T02:32:52Z")

</div>

You are right, I have no much experience with databases.  
To answer your questions:

- Each device sends MQTT with device ID and a dozen of measures (voltage, current, temperature, alarms, etc).
- The frequency is every 5/10 seconds.
- Not all devices send the exact the same measures.
- Retention policy 3 months
- A new device can be added/removed at any time.
- Let's assume I have 100 devices in the field.
- The reason to use Influx, is because it is a time-series DB and easy to use. No need to define tables and types.
- Data are shown in Grafana. One user per device or multiple devices.

I do understand your approach. My idea was to create a table/measurement for each device using the device ID just to keep things easier to manage visually on the Grafana side.

---

<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:** [13 January 2022 08:09 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/6 "2022-01-13T08:09:54Z")

</div>

> [@FeliceM](#):
>
> Not all devices send the exact the same measures.

Is that because you have a number of different device types? If so do all devices have a core set of data and different types have different extra data?

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [13 January 2022 08:26 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/7 "2022-01-13T08:26:03Z")

</div>

Correct. Some device has 12 strings of data: ID/Voltage/Current/temperature/A/B/C/8/9/19/11/12  
Other devices have: ID/alarm1/alarm2/3/A/etc  
The ID is always present and it is autogenerated on each device extracting the MAC address and adding some bits to it so that is recognisable and traceable.

---

<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:** [13 January 2022 08:38 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/8 "2022-01-13T08:38:02Z")

</div>

> [@FeliceM](#):
>
> Some device has 12 strings of data: ID/Voltage/Current/temperature/A/B/C/8/9/19/11/12  
> Other devices have: ID/alarm1/alarm2/3/A/etc

I would have one Measurement for each device type (where all devices of that type have the same data) with the ID as a tag. That will be the most efficient way of storing and accessing the data.

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [13 January 2022 09:13 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/9 "2022-01-13T09:13:16Z")

</div>

That is more or less what I had in mind, a measurement per device or per type using the ID or part of it. But I would love to make it programmatically as to avoid to have to create manually a new Measurement when a new type is made.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 January 2022 09:24 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/10 "2022-01-13T09:24:47Z")

</div>

> [@FeliceM](#):
>
> But I would love to make it programmatically as to avoid to have to create manually a new Measurement when a new type is made.

So use a `switch` node to examine the topic from the `mqtt-in` node and route the message to a flow leg for each group of devices.

---

<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:** [13 January 2022 09:35 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/11 "2022-01-13T09:35:53Z")

</div>

> [@FeliceM](#):
>
> a measurement per device or per type

There is a big difference between an Influx Measurement for each type, with the ID as a tag, and a Measurement for each device, where the measurement name includes the ID. With the former there will be as many Measurements as there are device types, with the latter there will be as many Measurements as there are individual devices.

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [13 January 2022 13:10 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/12 "2022-01-13T13:10:45Z")

</div>

Thanks for elaborating on my question. Your comments gave me some good ideas.  
However, I am still eager to understand if there a way to pass the "Measurement" name to this node without having to manually type it in the tab.

 ![Schermata 2022-01-13 alle 15.04.37](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/41ebad61a11282d6b2d01aa9388b239c996b7242.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:** [13 January 2022 13:27 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/13 "2022-01-13T13:27:52Z")

</div>

> [@FeliceM](#):
>
> I am still eager to understand if there a way to pass the "Measurement" name to this node

Of course, as is described in the help text for the node:  
"If the _measurement_ field is not set in the node configuration, the user can send in data with a specified measurement name in **msg.measurement** to overwrite the _measurement_ field in the configuration of the node."

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [13 January 2022 13:32 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/14 "2022-01-13T13:32:54Z")

</div>

Thank you Colin. My eyes were stuck on the form. I didn't see that.  
Thanks a million.

---

<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:** [27 January 2022 13:33 UTC](https://discourse.nodered.org/t/influxdb-out-node-define-measurement-programmatically/56525/15 "2022-01-27T13:33:26Z")

</div>

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