You didn't paste it in between ``` please EDIT your last post to correct this.
OK try this to get the idea...
flow surrounded by ``` so that you can import it...
[{"id":"7ec7ffcd.3ac498","type":"inject","z":"9a50778a.ed93a","name":"PIR1-OFF","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":132.5,"y":165,"wires":[["84a1dbfa.6173a8"]]},{"id":"56c07777.086f9","type":"inject","z":"9a50778a.ed93a","name":"PIR1-ON","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":132,"y":209,"wires":[["84a1dbfa.6173a8"]]},{"id":"81635019.8a1f28","type":"inject","z":"9a50778a.ed93a","name":"PIR2-OFF","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":127,"y":304,"wires":[["29430074.7cd92"]]},{"id":"729cd490.f7fdd4","type":"inject","z":"9a50778a.ed93a","name":"PIR2-ON","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":126.5,"y":348,"wires":[["29430074.7cd92"]]},{"id":"84a1dbfa.6173a8","type":"change","z":"9a50778a.ed93a","name":"","rules":[{"t":"set","p":"PIR1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":306.5,"y":185,"wires":[["d0b0375.0457c48"]]},{"id":"29430074.7cd92","type":"change","z":"9a50778a.ed93a","name":"","rules":[{"t":"set","p":"PIR2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":302,"y":323,"wires":[["d0b0375.0457c48"]]},{"id":"d0b0375.0457c48","type":"switch","z":"9a50778a.ed93a","name":"PIR1 STATE","property":"PIR1","propertyType":"flow","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":538.5,"y":240,"wires":[["936b1670.26f43"],["ad2a52e6.e304a8"]]},{"id":"936b1670.26f43","type":"switch","z":"9a50778a.ed93a","name":"PIR2 STATE","property":"PIR2","propertyType":"flow","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":721.5,"y":172,"wires":[["4c64d149.5070a8"],["5cbd381e.f58b2"]]},{"id":"ad2a52e6.e304a8","type":"switch","z":"9a50778a.ed93a","name":"PIR2 STATE","property":"PIR2","propertyType":"flow","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":723,"y":276,"wires":[["bc642787.6d91f8"],["abf9401b.bbf108"]]},{"id":"4c64d149.5070a8","type":"debug","z":"9a50778a.ed93a","name":"PIR1 OFF PIR2 OFF","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":960.5,"y":83,"wires":[]},{"id":"bc642787.6d91f8","type":"debug","z":"9a50778a.ed93a","name":"PIR1 ON PIR2 OFF","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":953,"y":235,"wires":[]},{"id":"5cbd381e.f58b2","type":"debug","z":"9a50778a.ed93a","name":"PIR1 OFF PIR2 ON","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":960,"y":153,"wires":[]},{"id":"abf9401b.bbf108","type":"debug","z":"9a50778a.ed93a","name":"PIR1 ON PIR2 ON","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":952,"y":293,"wires":[]}]
Flow requires both a PIR1 and PIR2 input before it starts...
There are many ways to do this but hopefully this way will help you understand what you need to do...
sorry there is some problem with my keyboard
[{"id":"76332ed6.a6db3","type":"mqtt in","z":"8eb88f3b.85ef2","name":"","topic":"NODE01","qos":"2","broker":"ce2b3222.963ee","x":60,"y":260,"wires":[["5dc1f66e.f73328"]]},{"id":"5dc1f66e.f73328","type":"function","z":"8eb88f3b.85ef2","name":"datanode1","func":"var output = msg.payload.split(\",\");\n \nvar temp = parseFloat(output[0]);\nvar humidity = parseFloat(output[1]);\nvar pir = parseInt(output[2]);\nvar ldr = parseInt(output[3]);\nvar msg0 = {payload:temp};\nvar msg1 = {payload:humidity};\nvar msg2 = {payload:pir};\nvar msg3 = {payload:ldr};\nreturn [msg0 , msg1 , msg2 , msg3];\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","outputs":4,"noerr":0,"x":210,"y":260,"wires":[["1554e13f.4c59ef"],["effa5112.982a7"],["b5c683b0.1f49d","d23ed03a.2e08a","e53998e.4dc2468","1554e13f.4c59ef"],["b5c683b0.1f49d","e53998e.4dc2468"]]},{"id":"b6f533da.49d11","type":"trigger","z":"8eb88f3b.85ef2","op1":"1","op2":"0","op1type":"num","op2type":"num","duration":"5","extend":true,"units":"s","reset":"","bytopic":"all","name":"TIME OUT","x":870,"y":260,"wires":[["dfcf0f10.5751"]]},{"id":"dfcf0f10.5751","type":"debug","z":"8eb88f3b.85ef2","name":"MOVEMENT DETECTED","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":850,"y":360,"wires":[]},{"id":"bccd3ae5.bfccf8","type":"switch","z":"8eb88f3b.85ef2","name":"CONDITION(FAN)","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"0","vt":"num"},{"t":"btwn","v":"10","vt":"num","v2":"15","v2t":"num"},{"t":"btwn","v":"16","vt":"num","v2":"20","v2t":"num"},{"t":"btwn","v":"21","vt":"num","v2":"25","v2t":"num"},{"t":"btwn","v":"26","vt":"num","v2":"50","v2t":"num"}],"checkall":"true","repair":false,"outputs":5,"x":430,"y":100,"wires":[["282ffb4.26aec04"],["d079e9f6.acc6d8"],["eb28c799.893088"],["879261c.e5c20a"],["688a3f53.613a4"]]},{"id":"d079e9f6.acc6d8","type":"debug","z":"8eb88f3b.85ef2","name":"SPEED 25%","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":670,"y":80,"wires":[]},{"id":"eb28c799.893088","type":"debug","z":"8eb88f3b.85ef2","name":"SPEED 50%","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":670,"y":120,"wires":[]},{"id":"282ffb4.26aec04","type":"debug","z":"8eb88f3b.85ef2","name":"FAN OFF","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":680,"y":40,"wires":[]},{"id":"879261c.e5c20a","type":"debug","z":"8eb88f3b.85ef2","name":"SPEED 75%","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":670,"y":160,"wires":[]},{"id":"688a3f53.613a4","type":"debug","z":"8eb88f3b.85ef2","name":"SPEED 100%","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":680,"y":200,"wires":[]},{"id":"e4f8b311.196e1","type":"switch","z":"8eb88f3b.85ef2","name":"CONDITION(LIGHT)","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"200","vt":"num"},{"t":"btwn","v":"200","vt":"num","v2":"500","v2t":"num"},{"t":"btwn","v":"500","vt":"num","v2":"750","v2t":"num"},{"t":"gte","v":"750","vt":"num"}],"checkall":"true","repair":false,"outputs":4,"x":560,"y":440,"wires":[["8ed94bd6.4d6c28"],["de8fbd40.a6f5d"],["60b3efdb.0a646"],["c9433f6d.a9fbd"]]},{"id":"8ed94bd6.4d6c28","type":"debug","z":"8eb88f3b.85ef2","name":"NO LIGHT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":830,"y":420,"wires":[]},{"id":"de8fbd40.a6f5d","type":"debug","z":"8eb88f3b.85ef2","name":"1 LIGHT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":820,"y":460,"wires":[]},{"id":"60b3efdb.0a646","type":"debug","z":"8eb88f3b.85ef2","name":"2 LIGHT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":820,"y":500,"wires":[]},{"id":"effa5112.982a7","type":"debug","z":"8eb88f3b.85ef2","name":"HUMIDITY","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":450,"y":180,"wires":[]},{"id":"7cadd8d8.6aea88","type":"debug","z":"8eb88f3b.85ef2","name":"NO MOVEMENT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":610,"y":320,"wires":[]},{"id":"b5c683b0.1f49d","type":"switch","z":"8eb88f3b.85ef2","name":"PIR (1)","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":440,"y":260,"wires":[["680e7da0.1c1504"]]},{"id":"680e7da0.1c1504","type":"switch","z":"8eb88f3b.85ef2","name":"VALUE OF LDR","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"0","vt":"num","v2":"1024","v2t":"num"}],"checkall":"true","repair":false,"outputs":1,"x":660,"y":260,"wires":[["b6f533da.49d11"]]},{"id":"d23ed03a.2e08a","type":"switch","z":"8eb88f3b.85ef2","name":"PIR (0)","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":420,"y":320,"wires":[["7cadd8d8.6aea88"]]},{"id":"e53998e.4dc2468","type":"switch","z":"8eb88f3b.85ef2","name":"PIR(1,0)","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":340,"y":440,"wires":[["e4f8b311.196e1"],["8ed94bd6.4d6c28"]]},{"id":"1554e13f.4c59ef","type":"switch","z":"8eb88f3b.85ef2","name":"PIR(1,0)","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":200,"y":100,"wires":[["bccd3ae5.bfccf8"],["282ffb4.26aec04"]]},{"id":"ce2b3222.963ee","type":"mqtt-broker","z":"8eb88f3b.85ef2","name":"","broker":"192.168.11.52","port":"1883","clientid":"clientID01","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":null,"birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willRetain":null,"willPayload":""}]
take a look at my flow.
Attach a debug node to each of the outputs of the function node and see what shows up.
value of ldr, pir, temp,humidity
OK so you are sending temp AND PIR to the PIR(1,0)
node.
But you only check to see if msg.payload
is 1
or 0
i.e. PIR on or OFF.
If msg.payload
is 1 you then check to see if msg.payload
is either LESS THAN 0 or between 10 & 50. But the switch only passes on values of 1, which isn't one of the options.
My suggestion would be to store the PIR value as a flow variable ( see my earlier flow example uses a Change
node to set flow.PIR
to msg.payload
) and then from the temp output from your function have a Switch
node that checks on the status of flow.PIR
The world needs more people like you, mate.
This really helped me a lot thank you
hi Srijan,
I am facing the same problem.
can you help me to split a string into 4 real separate values?
i am trying to write the function code but not getting the result.
Here is one way to do it. Use a change
node to remove the [ ]
with two replaces of nothing, then use a jsaoata expression - $split(payload,',')
- to 'split' the remainder into an array
[{"id":"846aa32f.9c4a4","type":"change","z":"196c2b5b.d805c5","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"[","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"]","fromt":"str","to":"","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"$split(payload,',')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":360,"wires":[["df4e419b.5e2c4"]]}]
or just feed it into a json node which will output the array...
and then maybe into a split node to send 4 values... if needed.
Much better than my idea!!
Thanks so much for posting the example flow. It helps us beginners tremendously and makes learning Node Red a little easier.
Hi, I read the things above but can't seem to get the values of t1 and t2 to put in a graph.
I got this far:
var output = msg.payload.split(",");
var t1 = parseInt(output[0]);
var t2 = parseInt(output[1]);
var p = parseFloat(output[2]);
var msg0 = {payload:t1};
var msg1 = {payload:t2};
var msg2 = {payload:p};
return [msg0 , msg1 , msg2];
But I guess I get NaN because msg0 is: t1:20.26 But I just want the number/value.
Correct, you would need to remove everything up to the :
before you try to use parseInt
function. Try splitting again on the colon, to separate the key from the value, like this:
var output = msg.payload.split(",");
var [k1, v1] = split(output[0], ':');
var t1 = parseInt(v1);
I like to use a change
node with a JSONata expression that uses a regular expression to find all of the key:val
pairs, and reassemble them into a single object:
Here is the expression I used:
payload.$match(/([^:]*):([-.\d]+)[,\n]?/).[groups] {
$[0]: $number($[1])
}
The nice thing about having all the keys and values is a single msg object is that you can pass the same data around to multiple ui nodes, and each one can choose which value to display by specifying the field name.
But I know that's pretty advanced syntax, and most people don't like it because it may be hard for someone else to maintain that code in the future. If you prefer to use a Javascript function, you may be able to get away with eval
uating that string wrapped in curly braces, like so:
eval("msg.payload = {" + str + "}");
return msg;
But that is usually not good practice -- it relies too heavily on the format of the data string to be valid JS object syntax.
Hi shrickus, Thanks for the help I'm going to try the first part of your reply, that's already difficult enough for me :-). I know almost nothing about coding that's why I like Node red.
I did this:
var output = msg.payload.split(",");
var [k1, v1] = split(output[0], ':');
var t1 = parseInt(output[0]);
var t2 = parseInt(output[1]);
var p = parseFloat(output[2]);
var msg0 = {payload:t1};
var msg1 = {payload:t2};
var msg2 = {payload:p};
return [msg0 , msg1 , msg2];
But got the above error. Like I said I'm a Noob in coding. Do not Know what k1 and v1 does?
Try
msg = msg.payload.split(":").slice(1,4).map(e => {
return {payload:parseFloat(e)}
});
return msg;
E1cid, Thanks.
var output = msg.payload.split(",").slice(1,4).map(e => {
return {payload:parseFloat(e)}
});
var t1 = parseInt(output[0]);
var t2 = parseInt(output[1]);
var p = parseFloat(output[2]);
var msg0 = {payload:t1};
var msg1 = {payload:t2};
var msg2 = {payload:p};
return [msg0 , msg1 , msg2];