Adding a var in msg.payload

Hi,

I want to write some data to InfluxDB. Everything works fine but now i want to write some tags instead of a fixed input.

I've got the following but node red see it as text and not a var.

It's propably something simple but i'm kinda new in this and just learning.

Thanks at advance.
Kind regards,
Ward

PS: The import code for the file is this:

[{"id":"7358ccf.3170634","type":"tab","label":"Test","disabled":false,"info":""},{"id":"2b7d1279.baf0de","type":"function","z":"7358ccf.3170634","name":"Counter","func":"var counter=flow.get('count');\ncounter = msg.payload;\nflow.set('count',counter)\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":60,"wires":[[]]},{"id":"940f5e20.55e36","type":"function","z":"7358ccf.3170634","name":"Text","func":"var text=flow.get('text');\ncounter = msg.payload;\nflow.set('text',text)\nreturn msg;\n","outputs":1,"noerr":0,"x":310,"y":100,"wires":[[]]},{"id":"b7102360.aa3698","type":"inject","z":"7358ccf.3170634","name":"","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":70,"y":60,"wires":[["2b7d1279.baf0de"]]},{"id":"46998a2f.92718c","type":"inject","z":"7358ccf.3170634","name":"","topic":"","payload":"Value","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":70,"y":100,"wires":[["940f5e20.55e36"]]},{"id":"3f529c0c.bf62e4","type":"inject","z":"7358ccf.3170634","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":80,"y":240,"wires":[["e8a9bc1c.8605d"]]},{"id":"e8a9bc1c.8605d","type":"function","z":"7358ccf.3170634","name":"multiple measurement points","func":"var counter=flow.get('count');\nvar text=flow.get('text');\n\nmsg.payload = [\n    {\n        measurement: \"Test\",\n        fields: {\n            'text': 'count'\n        },\n        timestamp: new Date()\n    }\n];\nreturn msg;","outputs":1,"noerr":0,"x":380,"y":240,"wires":[["afad0324.18b288"]]},{"id":"afad0324.18b288","type":"influxdb batch","z":"7358ccf.3170634","influxdb":"8d207046.8b93e","precision":"","retentionPolicy":"","name":"","x":750,"y":240,"wires":[]},{"id":"8640915f.ca16a8","type":"inject","z":"7358ccf.3170634","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":80,"y":280,"wires":[["2059349d.55668c"]]},{"id":"2059349d.55668c","type":"influxdb in","z":"7358ccf.3170634","influxdb":"8d207046.8b93e","name":"time query","query":"SELECT * FROM Test GROUP BY * ORDER BY DESC LIMIT 1;","rawOutput":false,"precision":"s","retentionPolicy":"","x":330,"y":280,"wires":[["29547787.74f57"]]},{"id":"29547787.74f57","type":"debug","z":"7358ccf.3170634","name":"","active":true,"console":"false","complete":"false","x":730,"y":280,"wires":[]},{"id":"8d207046.8b93e","type":"influxdb","z":"","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"NodeRed","name":"InfluxDB_NodeRed","usetls":false,"tls":""}]

Please export your flow again, read this post and edit your post.

In Influxdb the word 'tag' has a specific meaning. Fields and Tags are very different. Do you mean you want to write the number in count to the field called text? That doesn't sound particularly likely. Can you explain exactly what you are trying to do?

@bakman2 the code is inserted properly now.

@Colin
Indeed is just want to enter a field set no tag set. The text is just the field key and the number is the field value.
I just want to insert a number (in the example 100) as field value in a flow var (count) and the name of the field key stored in the flow var (text). And the var's what i said as tags in the earlier post i want to insert in the code for writing to the InfluxDB.

I hope it's better explained because englisch is not my native language and a little harder for me to explain myself. But as mentioned at the beginning the code is now correctly implemented so it's maybe easier to understand if you see the program itself.

Thanks at advance
Kind regards,
Ward

OK, in that case you need something like this

var counter=flow.get('count');
var text=flow.get('text');

msg.payload = [
    {
        measurement: "Test",
        fields: {
        },
        timestamp: new Date()
    }
];
msg.payload[0].fields[text] = counter
return msg;

However, it seems odd to go through context for something like this, are you sure you want to do it that way?

1 Like

Thanks for the reply Colin.

It's just for learning purposes but eventually i want collect data from a PLC and transmit those values at 5s intervals in InfluxDB and i want to alter the schema design like i want it. So no single valuepoint each writing.
How do you suggest i do that best?

Can you explain what you mean by that please and why you want to do it.

For example if we want to log the kwh of different machines. I want to make a measurement of a tagset and a fieldset where the tagset contains the name of the machine and the fieldset the value of the kwh every hour.
So if we want to log a extra machine then we can write a different name in the tagset. Because the tagset is indexed and we can make search quick queries later on.

And further down the road there are also multiple fieldset for a tagset. For example. There are different stages in a CIP program (rinsing with water, rinsing with Acid, ect...) so that will be the tagset. But we want to log the temp, conductivity, flow, ect,... for each stage of the CIP and will be the multiple fieldsets.

I hope this explains it a bit better.

OK, I understand what you want to do, but not what what problem you are having.
If you always want to add data to the database with the current timestamp it is easier to use the Influxdb Out node rather than the batch node.
Then all you have to do is to get the data in, put it all in the payload and pass it on the influx node.

That's absolutely true i just went further with a previous tutorial that ended with the batch node and now you mentioned it, it is better to use a Out node.
Thanks alot for the info, now i can play a little more with the info given to me.

Kind regards,
Ward

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