csongorvarga

Monitor sensor update in Node-Red

Jan 28th, 2021
823
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [{"id":"6e7bb784.d090d8","type":"function","z":"bf09f6e7.5b0d88","name":"Last update","func":"// Update the status with current timestamp\nvar now = new Date();\nvar yyyy = now.getFullYear();\nvar mm = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based\nvar dd  = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nvar hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nvar mmm  = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nvar ss  = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\nnode.status({fill:\"blue\",shape:\"ring\",text:\"Last update: \"+dd + \".\" + mm + \".\" + yyyy + \" \" + hh + \":\" + mmm + \":\" + ss});\n\nmsg.payload = dd + \".\" + mm + \".\" + yyyy + \" \" + hh + \":\" + mmm + \":\" + ss;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":2890,"y":100,"wires":[["6bcc7487.c8b05c"]]},{"id":"955dd95.5df2c28","type":"function","z":"bf09f6e7.5b0d88","name":"Time since last update","func":"var temp = context.get(\"last\");\nvar current = new Date();\nmsg.payload = \"No data\";\n\nif (msg.topic===\"update\") {\n\n    if (temp!==undefined) {\n        current = current - temp;\n        current = Math.floor(current/1000);\n        var minute = Math.floor(current/60);\n        var hour = Math.floor(minute/60);\n        var day = Math.floor(hour/24);\n        if (current>24*60*60) {\n            msg.payload = \"Last update \" + day + \" days, \" + hour%24 + \" hours, \" + minute%60 + \" minutes, \" + current%60 + \" seconds ago\";\n        } else if (current>60*60) {\n            msg.payload = \"Last update \" + hour%24 + \" hours, \" + minute%60 + \" minutes, \" + current%60 + \" seconds ago\";\n        } else if (current>60) {\n            msg.payload = \"Last update \" + minute%60 + \" minutes, \" + current%60 + \" seconds ago\";\n        } else {\n            msg.payload = \"Last update \" + current%60 + \" seconds ago\";\n        }\n    \n    }\n    node.status({fill:\"blue\",shape:\"ring\",text:msg.payload});\n    return msg;\n\n    \n} else {\n    context.set(\"last\",current);\n}\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":2920,"y":180,"wires":[["e5d7807b.7484a"]]},{"id":"24f6372c.aafef8","type":"inject","z":"bf09f6e7.5b0d88","name":"Data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2620,"y":140,"wires":[["6e7bb784.d090d8","955dd95.5df2c28"]]},{"id":"6ca0aad6.6c8b04","type":"inject","z":"bf09f6e7.5b0d88","name":"Update","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"update","payload":"","payloadType":"date","x":2640,"y":200,"wires":[["955dd95.5df2c28"]]},{"id":"6bcc7487.c8b05c","type":"debug","z":"bf09f6e7.5b0d88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":3130,"y":100,"wires":[]},{"id":"e5d7807b.7484a","type":"debug","z":"bf09f6e7.5b0d88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":3150,"y":180,"wires":[]}]
RAW Paste Data