How to send a temperature data that is more than 30 celcius and save it in excel csv file

Hi all, I'm trying to send some specific data of temperature in celcius that received an inputs from COMPORT3 which I connect with raspberry pi pico and when the temperature is above 30, it will Line notify to me.

Here is the node I created:

And in the function I use this code:
code function

So in my case, all data that it receive from the sensor raspberry pi pico, every data has saved in csv, but what I want is that for every temperature that is above 30, it willl be save in csv, and for temperature which is less than 30, it will not save in csv, so any idea on how should I fix the code in the function?

First post the request, then there (hopefully) will be a temperature as a result, then test if it is > 30, then write it.
Note that the temperature > '30' is testing for a string not a number. If the result from the request is a string, first convert it to a number.

You need to filter the temps using a switch node, also best to create a boolean for this. You can then direct every temp over 30 to the cvs. The message would only be sent when temp raises above 30, until it drops below, then a message telling you it's below once, until it raises again
e.g.

[{"id":"2318c14a.40be8e","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":70,"y":2100,"wires":[["7ca626ff.2e90b8"]]},{"id":"7ca626ff.2e90b8","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"pass_change","pt":"msg","to":"$$.payload > 30 ? true : false","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":2160,"wires":[["bfa66ba369fb31a2"]]},{"id":"a7306afd.cf622","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"35","payloadType":"num","x":70,"y":2180,"wires":[["7ca626ff.2e90b8"]]},{"id":"eda5a105.767418","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":70,"y":2140,"wires":[["7ca626ff.2e90b8"]]},{"id":"8f12cc3e.7de6f8","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"40","payloadType":"num","x":70,"y":2220,"wires":[["7ca626ff.2e90b8"]]},{"id":"bfa66ba369fb31a2","type":"switch","z":"bf9e1e33.030598","name":"","property":"pass_change","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":480,"y":2160,"wires":[["034205c1528902a8","3af70233.3a3b6e"],["3af70233.3a3b6e"]]},{"id":"034205c1528902a8","type":"debug","z":"bf9e1e33.030598","name":"csv","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":2160,"wires":[]},{"id":"3af70233.3a3b6e","type":"rbe","z":"bf9e1e33.030598","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"pass_change","x":230,"y":2260,"wires":[["2788d766.3f2aa8"]]},{"id":"2788d766.3f2aa8","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.message","tot":"msg"},{"t":"set","p":"url","pt":"msg","to":"http://websit.com","tot":"str"},{"t":"set","p":"headers","pt":"msg","to":"{\"content-type\":\"application/x-www-form-urlencoded\",\"Authorization\":\"[scheme] [auth token]\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":2260,"wires":[["92640cf9.6f2a4"]]},{"id":"92640cf9.6f2a4","type":"debug","z":"bf9e1e33.030598","name":"http","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":570,"y":2260,"wires":[]}]

[edit] Or if incoming payload is a string

[{"id":"2318c14a.40be8e","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"str","x":70,"y":2100,"wires":[["7ca626ff.2e90b8"]]},{"id":"7ca626ff.2e90b8","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"pass_change","pt":"msg","to":"$number($$.payload) > 30 ? true : false","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":2160,"wires":[["bfa66ba369fb31a2"]]},{"id":"a7306afd.cf622","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"35","payloadType":"str","x":70,"y":2180,"wires":[["7ca626ff.2e90b8"]]},{"id":"eda5a105.767418","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"str","x":70,"y":2140,"wires":[["7ca626ff.2e90b8"]]},{"id":"8f12cc3e.7de6f8","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"40","payloadType":"str","x":70,"y":2220,"wires":[["7ca626ff.2e90b8"]]},{"id":"bfa66ba369fb31a2","type":"switch","z":"bf9e1e33.030598","name":"","property":"pass_change","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":480,"y":2160,"wires":[["034205c1528902a8","3af70233.3a3b6e"],["3af70233.3a3b6e"]]},{"id":"034205c1528902a8","type":"debug","z":"bf9e1e33.030598","name":"csv","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":2160,"wires":[]},{"id":"3af70233.3a3b6e","type":"rbe","z":"bf9e1e33.030598","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"pass_change","x":230,"y":2260,"wires":[["2788d766.3f2aa8"]]},{"id":"2788d766.3f2aa8","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.message","tot":"msg"},{"t":"set","p":"url","pt":"msg","to":"http://websit.com","tot":"str"},{"t":"set","p":"headers","pt":"msg","to":"{\"content-type\":\"application/x-www-form-urlencoded\",\"Authorization\":\"[scheme] [auth token]\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":2260,"wires":[["92640cf9.6f2a4"]]},{"id":"92640cf9.6f2a4","type":"debug","z":"bf9e1e33.030598","name":"http","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":570,"y":2260,"wires":[]}]

I believe that the data that have received from the sensor is a string, but how do I convert that input {string} into a number in the function node-red? And is there like a command when the data is more than 30 celcius then it will write down in the csv?

In a function node, use parseInt, parseFloat or Number() to convert string to number.

No there is no such "command". There are nodes that you can wire together to achieve this.

  1. a switch node can determine if a value is > 30
  2. a file node can write the value to a CSV file.

Here is a little flow which

  • Injects as strings "25", "30" or "35.97".
  • Converts the text to a number.
  • Checks if the value >= 30 and if it is,
  • Displays the value.

[{"id":"0d424c52e680beb0","type":"inject","z":"3942cc17cf38667e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"25","payloadType":"str","x":210,"y":140,"wires":[["4a4217991302609d"]]},{"id":"c690aabd2a170679","type":"inject","z":"3942cc17cf38667e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"30","payloadType":"str","x":210,"y":180,"wires":[["4a4217991302609d"]]},{"id":"d3d5ba1fb194f6fc","type":"inject","z":"3942cc17cf38667e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"35.97","payloadType":"str","x":210,"y":220,"wires":[["4a4217991302609d"]]},{"id":"4a4217991302609d","type":"change","z":"3942cc17cf38667e","name":"Convert to number","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":180,"wires":[["5441eea04688397c"]]},{"id":"5441eea04688397c","type":"switch","z":"3942cc17cf38667e","name":">= 30?","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"30","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":580,"y":180,"wires":[["595773fc8a966bc0"]]},{"id":"595773fc8a966bc0","type":"debug","z":"3942cc17cf38667e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":180,"wires":[]}]

ps I suspect that the problem with your function is that it passes on the message regardless of the temperature.

Cross posted and answered on Stack Overflow here:
https://stackoverflow.com/questions/71734215/how-to-allow-node-red-to-send-only-data-temperature-that-is-more-than-30-degre/71735054#71735054

P.s. You really need to change that AccessToken now you've published it to the world in multiple places on the internet.

1 Like

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