I am TRYING to test some code for weather (rain) detection.
Originally it wasn't this smart, but now I am wanting to improve it.
I've cut down a lot of stuff and tried to cut it to what is THE problem.
If it is raining part of the message exists and it is parsed.
If it isn't raining that part doesn't exist and a dummy is made. (The value is set to 0
)
That is then sent on and rounded to 1 decimal place - yeah: deal with the numbers as given - and stored (flow context)
If the flow.MODE is NOT B then it adds the values.
That was painful as +
was being done as stick to the end of rather than ADD.
This was because the values were TEXT rather than numbers.
I got around that problem and it started to work.
Then it stopped and now doesn't seem to want to play the game.
No matter what I do I am NOT getting NUMBERS out of the function
node in msg.rain_now
The top function
node after the switch
node.
Ok, I could deal with the entire value given and tidy it up later on for display purposes.
That is a whole problem unto itself.
So TRYING to get it working:
Why is it that msg.rain_now
is TEXT and not NUMBER?
[{"id":"89c34e754b89497e","type":"inject","z":"05319bbbba06da29","name":"0.818181","props":[{"p":"data.rain[\"1h\"]","v":"0.818181","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":470,"y":810,"wires":[["4da4a97fbc646912"]]},{"id":"37a2bea6f1a3d08f","type":"inject","z":"05319bbbba06da29","name":"0.1616161616","props":[{"p":"data.rain[\"1h\"]","v":"0.1616161616","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":480,"y":860,"wires":[["4da4a97fbc646912"]]},{"id":"4da4a97fbc646912","type":"switch","z":"05319bbbba06da29","name":"Raining?","property":"data.rain[\"1h\"]","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":925,"y":910,"wires":[["bbf430450a163bba","f082cb9f8d1294b0","61d5dac37fcb1773"],["90d76570e9d18b83","52c64f8853124708"]],"l":false},{"id":"61d5dac37fcb1773","type":"function","z":"05319bbbba06da29","name":"Set `rain_now` to value.","func":"let rain_now = parseFloat(msg.data.rain[\"1h\"]).toFixed(1);\nmsg.rain_now = rain_now;\n\nnode.warn(\"Rain now \" + rain_now);\n//msg.something = 0.6;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1005,"y":910,"wires":[["2b5173aa2b316920","0b369a7bec8d4350"]],"l":false},{"id":"52c64f8853124708","type":"function","z":"05319bbbba06da29","name":"No rain detected","func":"msg.rain_now = 0;\nmsg.payload = 0;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1005,"y":950,"wires":[["0ad56c2f26fe2166"]],"l":false},{"id":"2b5173aa2b316920","type":"switch","z":"05319bbbba06da29","name":"==B?","property":"mode","propertyType":"flow","rules":[{"t":"eq","v":"B","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1075,"y":910,"wires":[["13659bc680d6437d"],["0ad56c2f26fe2166"]],"l":false},{"id":"0ad56c2f26fe2166","type":"junction","z":"05319bbbba06da29","x":1090,"y":1010,"wires":[["4f3f2b25c2f8d6d6"]]},{"id":"04eeefaf3ac83cdd","type":"function","z":"05319bbbba06da29","name":"Store rain.","func":"// If `reset` message received.\nif (msg.reset == \"reset\")\n{\n msg.rain_now = flow.get(\"rain\");\n return msg;\n}\n\nlet todays_rain = parseInt(flow.get(\"rain\")) || 0;\n//node.warn(\"Today's rain is \" + todays_rain);\n//node.status({ text: todays_rain });\n\nlet rain_now = parseInt(msg.rain_now);\n\n//todays_rain = todays_rain + rain_now;\n//todays_rain = parseFloat(todays_rain + rain_now).toFixed(1);\ntodays_rain = todays_rain + rain_now;\n\nnode.warn(\"Adding rain totals together\");\nnode.warn(\"rain now \" + rain_now);\nnode.warn(\"Today's rain \" + todays_rain);\n\nflow.set(\"rain\",todays_rain);\n\nnode.status({ text: todays_rain });\n\nmsg.payload = todays_rain;\nmsg.topic = null;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1380,"y":1010,"wires":[["ea17db4b610c2bca","f70d474fa4e6a312","0a7ddf199d71ddce","0543926b6e610c6f"]]}]
This is a rewrite of what I had and worked.
Well, it worked in that values seemed to be passed correctly.
But to simplify things, I moved things around and processed the original message to give me a usable message.
This becomes/became msg.rain_now
and it a number
with 1 needed decimal place accuracy.
From here on it is only one message that needs to be handled and if/when a new message arrives, the context value is added then stored back to the context.
If/when the ONE ONLY message arrives requesting the value, it is taken and then on-sent.
Should be easy, but I am getting text
rather than number
values sent.
As I'm already down this rabbit hole, going back to the older way - which had it's own set of problems - I would prefer to get this working.
So although you are seeing something that doesn't work - why would I ask for help if it did work? - I hope you can see what it is and HOW I want to do it.
Again: Thanks.