# Sending data to Graphite using Node-RED

**URL:** <https://discourse.nodered.org/t/sending-data-to-graphite-using-node-red/76998>\
**Category:** General\
**Tags:** docker\
**Created:** [27 March 2023 21:07 UTC](https://discourse.nodered.org/t/sending-data-to-graphite-using-node-red/76998 "2023-03-27T21:07:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [27 March 2023 21:07 UTC](https://discourse.nodered.org/t/sending-data-to-graphite-using-node-red/76998/1 "2023-03-27T21:07:08Z")

</div>

Greetings. The subject of how to send data into a [Graphite database](https://graphiteapp.org/) has come up in this forum, but to my knowledge, the nut has not been cracked. I'd like to do that now, but need a little help.

[This post](https://discourse.nodered.org/t/data-to-graphite-in-grafana/46585/3) concluded that [this nodejs package](https://www.npmjs.com/package/graphite) was simple enough that it should be possible to just use it from a function node. I went through the process of installing Graphite in a docker container and have it running now (v.1.1.10 is now running and collecting basic stats on my machine, which I can view in localhost:80).

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/417d106848d6c3a85d9ba7d9c23273c8c139941c.png)

I next did `npm install graphite` and that gave me `added 1 package, and audited 2 packages in 1s. found 0 vulnerabilities`.

With that done, I put the code from the NPM usage into the function node, and this is where I am in over my head.

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

Questions (silly, I know)...

1. How should lines 1 and 2 be changed? As is, I am getting the error (in the debug pane):  
`"ReferenceError: require is not defined (line 1, col 16)"`

2. If I send a msg.payload value of 4 into the function node, I presume that I would just swap out the value "23" in the above and put msg.payload. Same thing with sending tag names in line 7.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 March 2023 21:45 UTC](https://discourse.nodered.org/t/sending-data-to-graphite-using-node-red/76998/2 "2023-03-27T21:45:08Z")

</div>

> [@grant1](#):
>
> I next did `npm install graphite` and that gave me `added 1 package, and audited 2 packages in 1s. found 0 vulnerabilities`

There is no need to install. Simply enter `graphite` in the function node setup tab

> [@grant1](#):
>
> I am getting the error (in the debug pane):  
> `"ReferenceError: require is not defined (line 1, col 16)"`

Remove line 1 - the `require` is performed by node-red because you entered `graphite` in the function node setup tab.

---

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [27 March 2023 22:02 UTC](https://discourse.nodered.org/t/sending-data-to-graphite-using-node-red/76998/3 "2023-03-27T22:02:42Z")

</div>

Thank you! I felt silly asking, but I knew it would be easy.

As a quick n' dirty test, I am sending a random value between 1400 and 1475 every 10 seconds via an MQTT node like this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3c9f6c1ff818a002f0459a3e519a397eeace3c71.png)

The function node details:

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2a73e0d379d894cbc40f1af75ff891fecb004a9c.png)  
(not yet doing anything with the tags...more on that tomorrow)

Viewed in Graphite:

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

Viewed in Grafana:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/77cebd2784a73a414d84f9348f4645a20842d171.png)

Thank you @Steve-Mcl Let's consider this nut cracked open!

---

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [28 March 2023 11:42 UTC](https://discourse.nodered.org/t/sending-data-to-graphite-using-node-red/76998/4 "2023-03-28T11:42:00Z")

</div>

In case anyone wants to send fields and tags into Graphite, here is a sample flow using the fields `temperature` and `humidity` and the tags `kitchen` and `basement`.

```auto
[{"id":"018b2e9311f9102f","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"field","payload":"76","payloadType":"num","x":230,"y":140,"wires":[["8360371aecdeedd7"]]},{"id":"26dc41e58f0ae76a","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tag","payload":"kitchen","payloadType":"str","x":220,"y":200,"wires":[["8360371aecdeedd7"]]},{"id":"8360371aecdeedd7","type":"join","z":"d1c87ae83bc41c60","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":430,"y":160,"wires":[["761b71bf3299516d","f5bd3f179bf7d970"]]},{"id":"f5bd3f179bf7d970","type":"function","z":"d1c87ae83bc41c60","name":"temperature","func":"var client = graphite.createClient('http://localhost');\n\nvar metrics = { temperature: msg.payload.field };\nclient.write(metrics, function (err) {\n // if err is null, your data was sent to graphite!\n});\nvar tags = { 'room': msg.payload.tag };\nclient.writeTagged(metrics, tags, function (err) {\n // if err is null, your data was sent to graphite!\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"graphite","module":"graphite"}],"x":670,"y":160,"wires":[["b418ad50d7acd19e"]]},{"id":"b418ad50d7acd19e","type":"debug","z":"d1c87ae83bc41c60","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":840,"y":160,"wires":[]},{"id":"761b71bf3299516d","type":"debug","z":"d1c87ae83bc41c60","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":660,"y":200,"wires":[]},{"id":"c907ffb8790843d8","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"field","payload":"67","payloadType":"num","x":230,"y":280,"wires":[["48c62bb750bb8215"]]},{"id":"16d38e28389bda55","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tag","payload":"basement","payloadType":"str","x":215,"y":336,"wires":[["48c62bb750bb8215"]]},{"id":"48c62bb750bb8215","type":"join","z":"d1c87ae83bc41c60","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":430,"y":300,"wires":[["e8e228b0db792efb","59dd5a55f5e267ca"]]},{"id":"59dd5a55f5e267ca","type":"function","z":"d1c87ae83bc41c60","name":"temperature","func":"var client = graphite.createClient('http://localhost');\n\nvar metrics = { temperature: msg.payload.field };\nclient.write(metrics, function (err) {\n // if err is null, your data was sent to graphite!\n});\nvar tags = { 'room': msg.payload.tag };\nclient.writeTagged(metrics, tags, function (err) {\n // if err is null, your data was sent to graphite!\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"graphite","module":"graphite"}],"x":680,"y":300,"wires":[["e9405e9ad624500f"]]},{"id":"e9405e9ad624500f","type":"debug","z":"d1c87ae83bc41c60","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":300,"wires":[]},{"id":"e8e228b0db792efb","type":"debug","z":"d1c87ae83bc41c60","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":685,"y":336,"wires":[]},{"id":"2fc8e4f361ac3191","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"field","payload":"41","payloadType":"num","x":210,"y":500,"wires":[["eb2eb3f2df63881c"]]},{"id":"df6a6a624fc6b877","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tag","payload":"kitchen","payloadType":"str","x":200,"y":560,"wires":[["eb2eb3f2df63881c"]]},{"id":"eb2eb3f2df63881c","type":"join","z":"d1c87ae83bc41c60","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":410,"y":520,"wires":[["f38a3ab4453ec72a","c7502d45273d040e"]]},{"id":"c7502d45273d040e","type":"function","z":"d1c87ae83bc41c60","name":"humidity","func":"var client = graphite.createClient('http://localhost');\n\nvar metrics = { humidity: msg.payload.field };\nclient.write(metrics, function (err) {\n // if err is null, your data was sent to graphite!\n});\nvar tags = { 'room': msg.payload.tag };\nclient.writeTagged(metrics, tags, function (err) {\n // if err is null, your data was sent to graphite!\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"graphite","module":"graphite"}],"x":640,"y":520,"wires":[["2b979d8421835efc"]]},{"id":"2b979d8421835efc","type":"debug","z":"d1c87ae83bc41c60","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":820,"y":520,"wires":[]},{"id":"f38a3ab4453ec72a","type":"debug","z":"d1c87ae83bc41c60","name":"debug 8","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":560,"wires":[]},{"id":"85cd83cb406bcc2e","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"field","payload":"54","payloadType":"num","x":210,"y":640,"wires":[["f32ad0b6ace1b8a1"]]},{"id":"049bd3fc819e32dd","type":"inject","z":"d1c87ae83bc41c60","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tag","payload":"basement","payloadType":"str","x":195,"y":696,"wires":[["f32ad0b6ace1b8a1"]]},{"id":"f32ad0b6ace1b8a1","type":"join","z":"d1c87ae83bc41c60","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":410,"y":660,"wires":[["813823ad5cc66aba","80a59bdbe37854b0"]]},{"id":"80a59bdbe37854b0","type":"function","z":"d1c87ae83bc41c60","name":"humidity","func":"var client = graphite.createClient('http://localhost');\n\nvar metrics = { humidity: msg.payload.field };\nclient.write(metrics, function (err) {\n // if err is null, your data was sent to graphite!\n});\nvar tags = { 'room': msg.payload.tag };\nclient.writeTagged(metrics, tags, function (err) {\n // if err is null, your data was sent to graphite!\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"graphite","module":"graphite"}],"x":650,"y":660,"wires":[["0b985e785fb07972"]]},{"id":"0b985e785fb07972","type":"debug","z":"d1c87ae83bc41c60","name":"debug 9","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":660,"wires":[]},{"id":"813823ad5cc66aba","type":"debug","z":"d1c87ae83bc41c60","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":665,"y":696,"wires":[]}]

```

In Grafana's query editor, the nomenclature is as follows:

`fieldname;tagname=tagvalue`  
example: `humidity;room=basement`

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/96989df25fa1275ca079260f1ea90e4f217b6e7b.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:** [11 April 2023 11:42 UTC](https://discourse.nodered.org/t/sending-data-to-graphite-using-node-red/76998/5 "2023-04-11T11:42:30Z")

</div>

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