There is obviously many ways to do this. It is up to you and your experiences and so on. But there is Node-RED way also. (Write less code and use power of dedicated nodes). For you as the beginner I recommend to at least investigate this flow I created.
[{"id":"df6d6c66.b6c4e","type":"split","z":"d9f8d1d.5cd143","name":"","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":410,"y":820,"wires":[["fe502aa1.ee37c8","3ba2aff1.e8af5"]]},{"id":"d9822859.20a7b8","type":"inject","z":"d9f8d1d.5cd143","name":"","topic":"","payload":"15.4,50,23.8,79","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":"","x":220,"y":820,"wires":[["bc23045.f5103f8","df6d6c66.b6c4e"]]},{"id":"2f990562.44d8ea","type":"debug","z":"d9f8d1d.5cd143","name":"out 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":930,"y":760,"wires":[]},{"id":"fe502aa1.ee37c8","type":"change","z":"d9f8d1d.5cd143","name":"to number","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(msg.payload)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":820,"wires":[["9052efa8.e5f0a","d6f23ad6.490eb8"]]},{"id":"9052efa8.e5f0a","type":"switch","z":"d9f8d1d.5cd143","name":"route","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"},{"t":"eq","v":"3","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":750,"y":820,"wires":[["2f990562.44d8ea"],["35825546.228baa"],["d5e60d8b.47391"],["d97cb86c.da9478"]]},{"id":"35825546.228baa","type":"debug","z":"d9f8d1d.5cd143","name":"out 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":930,"y":800,"wires":[]},{"id":"d5e60d8b.47391","type":"debug","z":"d9f8d1d.5cd143","name":"out 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":930,"y":840,"wires":[]},{"id":"d97cb86c.da9478","type":"debug","z":"d9f8d1d.5cd143","name":"out 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":930,"y":880,"wires":[]},{"id":"2f3bddd7.cf80e2","type":"comment","z":"d9f8d1d.5cd143","name":"split","info":"Split comma separated string into separated messages.\nValue placed to msg.payload\nValue type will be still string.\n\nThere will be 4 messages out of this split node\n\nFor each msg also given a property \"parts\". \nIt will be used later on.","x":410,"y":760,"wires":[]},{"id":"d4a6401f.81de4","type":"comment","z":"d9f8d1d.5cd143","name":"convert","info":"Convert the value of msg.payload to the number using JSONata expression.\n","x":590,"y":760,"wires":[]},{"id":"60cbfa64.9cb4b4","type":"comment","z":"d9f8d1d.5cd143","name":"route","info":"Route every message to differet output by using switch node.\nAs property \"parts\" has been given to each message, we can use its \n\"index\" property to find out proper output\n\n","x":750,"y":760,"wires":[]},{"id":"bc23045.f5103f8","type":"debug","z":"d9f8d1d.5cd143","name":"source data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":410,"y":980,"wires":[]},{"id":"3ba2aff1.e8af5","type":"debug","z":"d9f8d1d.5cd143","name":"spltted","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":530,"y":940,"wires":[]},{"id":"d6f23ad6.490eb8","type":"debug","z":"d9f8d1d.5cd143","name":"converted","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":660,"y":900,"wires":[]}]
Since you are getting the data via MQTT - how do you create the data?
It might be a lot easier if you create the data in a format that will be easy to use in your 'datanode1 function.
I think splitting a string should not be considered too hard, and is a worthwhile thing to be able to do here.
As the values are only the values and there is nothing more so the one should guarantee that values coming in always in same order. Or what to do if some value is not there? Yes, the splitting is easy but to keep the meaning or the origin of data is much more important in this case. Creating the data in proper way will come handy in all means.
thanks sir , my problem is resolved
thanks to all
for ur time and help.
[{"id":"7e84782b.434118","type":"template","z":"8ffd8e0f.ee325","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This is the payload: {{payload}} !","output":"str","x":120,"y":380,"wires":[[]]}]
this is my code, in this code i want that if the value of pir is 1 then lights should turn on for 5 min and then automatically get off. but if its value again reaches to 1 again the light should glow for 5 min .
plzz help me out
Look at the trigger node
used the trigger node and even gave the delay of 5 min but a bit confused where to connect that trigger node
Try it in different places in your flow and see what different results you get. (TIP: start on the right hand size of your flow)
not getting ur point can u explain briefly
You say you are
But in your images only show a little bit of your flow. The best way for you to therefore work out where to connect the trigger node is to try. Try adding it to different places within your flow and see what difference it makes.
Put the trigger before the node that will turn on the light.
BTW - you have a 'hanging' delay node that won't do anything since it's output goes no where.
can i use function node for the same task to turn lights for 5min if movement is detected if yes
please give me some clues
Yes, but why would you want to? The trigger node does exactly what you want to achieve.
I am trying with it but not successful in my process
if u can create a flow showing the same will help me a lot
[{"id":"2c34fd24.8dcc8a","type":"inject","z":"73ce664f.9ecb98","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":149.5,"y":177,"wires":[["cc52cf20.3de798"]]},{"id":"21c05283.e1b196","type":"trigger","z":"73ce664f.9ecb98","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":449.5,"y":178,"wires":[["9e0d4b51.f74cc8"]]},{"id":"9e0d4b51.f74cc8","type":"debug","z":"73ce664f.9ecb98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":655.5,"y":178,"wires":[]},{"id":"cc52cf20.3de798","type":"switch","z":"73ce664f.9ecb98","name":"If true","property":"payload","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":289.5,"y":178,"wires":[["21c05283.e1b196"]]},{"id":"c6d79979.ab21","type":"inject","z":"73ce664f.9ecb98","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150.5,"y":229,"wires":[["cc52cf20.3de798"]]}]
NODE PIR(0,1) IS NOT ABLE TO CONTROL THE FAN AND LIGHT AS SHOWN IN THE FIGURE.
SO I THINK I SHOULD CREATE A FUNCTION NODE FOR THIS
IN WHICH IF PIR VALUE IS 1 THEN IT WILL TURN THE LIGHT AND FAN ON OTHERWISE ALL THE LIGHT AND FAN WILL BE OFF . THE CONTROL OF LIGHT AND FAN SHOULD BE TOTALLY OVER PIR .
ANY HELP FROM UR SIDE
There is no need to shout (please don't write in capitals!)
Without seeing the contents of your function and switches it is difficult to know what you are doing. Please export your flow and paste it in this thread in between ``` so it formats it correctly
But where I guess you are going wrong is that Node-RED doesn't remember the contents of old messages. So you need to save the value of PIR0 so when PIR1 changes it remembers what state PIR0 is ( and save PIR1 so that when ...)
You can read about how to do this
https://nodered.org/docs/writing-functions#storing-data
and can do it with a change node (eg set flow.pir1 to msg.payload)