Closed alexsahka closed 1 year ago
Hmm, no, I don't think that'll work, but depending on what you need the value for one of these options might work for you?
[ { "id": "1cc8b12467a1b9b3", "type": "hal2Thing", "z": "a12b67b864e2c98f", "eventHandler": "488bfca26161ef73", "thingType": "97013256515b3580", "name": "Z2M Window", "attributes": [], "topicFilter": "", "topicFilterType": "mqtt", "topicPrefix": "", "notes": "", "outputs": 0, "x": 350, "y": 1720, "wires": [] }, { "id": "c424fc47d3f7798e", "type": "inject", "z": "a12b67b864e2c98f", "name": "Door closed (true)", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "{\"battery\":100,\"contact\":true,\"device_temperature\":25,\"last_seen\":\"2023-03-04T17:03:27-08:00\",\"linkquality\":40,\"power_outage_count\":373,\"temperature\":25,\"voltage\":3025}", "payloadType": "json", "x": 150, "y": 1700, "wires": [ [ "1cc8b12467a1b9b3" ] ] }, { "id": "e3f9f80227811654", "type": "inject", "z": "a12b67b864e2c98f", "name": "Door openned (false)", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "{\"battery\":100,\"contact\":false,\"device_temperature\":25,\"last_seen\":\"2023-03-04T17:03:27-08:00\",\"linkquality\":40,\"power_outage_count\":373,\"temperature\":25,\"voltage\":3025}", "payloadType": "json", "x": 160, "y": 1760, "wires": [ [ "1cc8b12467a1b9b3" ] ] }, { "id": "069f5e76761fa970", "type": "hal2Event", "z": "a12b67b864e2c98f", "eventHandler": "e228014191d14be6", "name": "Contact's been in state true exactly five mins", "topic": "", "thing": "1cc8b12467a1b9b3", "typeSel": "hal2Thing", "item": "af0460be.09d73", "operator": "true", "change": "1", "compareValue": "", "compareType": "num", "outputValue": "payload", "outputType": "state", "ratelimit": false, "ratetype": "all", "rate": "1", "rateUnits": "hour", "delay": true, "delayExtend": false, "delayReset": true, "delayValue": "300", "x": 230, "y": 1880, "wires": [ [] ] }, { "id": "24e163332fbbcb8e", "type": "hal2Gate", "z": "a12b67b864e2c98f", "name": "Contact's been in state true at least five mins", "rules": [ { "thing": "1cc8b12467a1b9b3", "thingtype": "1dbfb9cbebf7c0b8", "item": "af0460be.09d73", "operator": "true", "value": "", "type": "num" }, { "thing": "1cc8b12467a1b9b3", "thingtype": "1dbfb9cbebf7c0b8", "item": "af0460be.09d73", "operator": "last_change_gte", "value": "300", "type": "num" } ], "checkall": "true", "if": false, "outputs": 2, "x": 230, "y": 1820, "wires": [ [], [] ] }, { "id": "be4e5540243af46e", "type": "inject", "z": "a12b67b864e2c98f", "name": "trigger", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 110, "y": 1940, "wires": [ [ "dd73d16a9e43253c" ] ] }, { "id": "19ccac24e1ac1b03", "type": "debug", "z": "a12b67b864e2c98f", "name": "debug 3", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "statusVal": "", "statusType": "auto", "x": 660, "y": 1940, "wires": [] }, { "id": "dd73d16a9e43253c", "type": "hal2Value", "z": "a12b67b864e2c98f", "name": "All contact item info", "action": "get", "thing": "1cc8b12467a1b9b3", "thingType": "1dbfb9cbebf7c0b8", "item": "af0460be.09d73", "outputValue": "payload", "outputType": "msg", "info": true, "x": 280, "y": 1940, "wires": [ [ "0caf83827a77bf3f" ] ] }, { "id": "0caf83827a77bf3f", "type": "function", "z": "a12b67b864e2c98f", "name": "Convert to seconds", "func": "var now = new Date();\nvar change = Math.round((now - msg.item.last_change) / 1000);\nmsg.payload = \"Contact changed state \"+change+\" seconds ago\";\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 490, "y": 1940, "wires": [ [ "19ccac24e1ac1b03" ] ] }, { "id": "488bfca26161ef73", "type": "hal2EventHandler", "name": "Windows Doors", "maxlisteners": "50", "heartbeat": "3600" }, { "id": "97013256515b3580", "type": "hal2ThingType", "name": "Z2M Xiaomi MCCGQ11LM door / window sensor", "contextStore": "memoryOnly", "nodestatus": " 🚪%Contact%⏳%Contact_closed_counter%🌡️%Temperature%°F📶%Linkquality%🔋%Battery%%⚡%Voltage%V🕢%LastSeen%", "nodestatusType": "str", "statusFn": "// item & attribute objects are\n// passed to function as objects\n\nreturn '';", "items": [ { "name": "Battery", "id": "38773bb6.1923d4", "topicFilterType": "StrEnd", "topicFilterValue": "", "topicSuffix": "", "type": "status", "ingress": "488ff997.1fae98", "egress": "5a6ab0b3.43636", "notes": "", "output": "1" }, { "name": "Voltage", "id": "a308050c.ec17f8", "topicFilterType": "StrEnd", "topicFilterValue": "", "topicSuffix": "", "type": "status", "ingress": "b891197f.d119c8", "egress": "5a6ab0b3.43636", "notes": "", "output": "1" }, { "name": "Linkquality", "id": "977167c1.ae6448", "topicFilterType": "StrEnd", "topicFilterValue": "", "topicSuffix": "", "type": "status", "ingress": "7838e8e7.d95508", "egress": "5a6ab0b3.43636", "notes": "", "output": "1" }, { "name": "Temperature", "id": "df60e448.dfe898", "topicFilterType": "StrEnd", "topicFilterValue": "", "topicSuffix": "", "type": "status", "ingress": "5306119b.55bd2", "egress": "5a6ab0b3.43636", "notes": "", "output": "1" }, { "name": "Contact", "id": "af0460be.09d73", "topicFilterType": "StrEnd", "topicFilterValue": "", "topicSuffix": "", "type": "status", "ingress": "d2515770.de6b18", "egress": "acd4d5b2.e46cf8", "notes": "", "output": "1" }, { "name": "LastSeen", "id": "800ca8b7.4b7328", "topicFilterType": "str", "topicFilterValue": "", "topicSuffix": "", "type": "status", "ingress": "9f7dd133.3df26", "egress": "5a6ab0b3.43636", "notes": "", "output": "1" }, { "name": "Contact_closed_counter", "id": "5a23656.d93b79c", "topicFilterType": "str", "topicFilterValue": "", "topicSuffix": "", "type": "status", "ingress": "6e073250.e8a16c", "egress": "5a6ab0b3.43636", "notes": "", "output": "1" }, { "name": "Alive", "id": "1", "topicFilterType": "StrEnd", "topicFilterValue": "/availability", "topicSuffix": "", "readOnly": true, "type": "status", "ingress": "d3a785ac.dc0378", "egress": "" } ], "attributes": [], "ingress": [ { "name": "Pass-through", "id": "c7ccebbd.016228", "fn": "// msg object is passed to function\n\nreturn msg.payload;" }, { "name": "Battery", "id": "488ff997.1fae98", "fn": "// msg object is passed to function\nif (msg.payload.battery) {\n msg.payload = Number(msg.payload.battery);\n return msg.payload;\n}\n" }, { "name": "Voltage", "id": "b891197f.d119c8", "fn": "// msg object is passed to function\nif (msg.payload.voltage) {\n voltage = Number(msg.payload.voltage)* 0.001;\n // return msg.payload * 0.001;\n msg.payload = Math.round(voltage* 100) / 100;\n return msg.payload\n}" }, { "name": "Linkquality", "id": "7838e8e7.d95508", "fn": "// msg object is passed to function\nif (msg.payload.linkquality) {\n msg.payload = Number(msg.payload.linkquality);\n return msg.payload;\n}" }, { "name": "Temperature", "id": "5306119b.55bd2", "fn": "// msg object is passed to function\nif (msg.payload.temperature) {\n tempc = Number(msg.payload.temperature);\n tempf = tempc * 9/5 + 32;\n msg.payload = Math.round(tempf* 10) / 10;\n return msg.payload\n}" }, { "name": "Contact", "id": "d2515770.de6b18", "fn": "msg.payload = (msg.payload.contact == true) ? 'closed' : 'opened';\nreturn msg.payload;" }, { "name": "LastSeen", "id": "9f7dd133.3df26", "fn": "// msg object is passed to function\nif (msg.payload.last_seen) {\n msg.payload = String(msg.payload.last_seen);\n return msg.payload;\n}" }, { "name": "closed_counter", "id": "6e073250.e8a16c", "fn": "var counter = 0;\r\nlet intervalID = context.get('intervalID') ?? undefined;\r\n\r\nfunction startCounter() {\r\n if (intervalID != undefined) { clearInterval(intervalID) }\r\n intervalID = setInterval(function () {\r\n counter++;\r\n if (counter <= 9) {\r\n node.send({ payload: counter });\r\n } else {\r\n stopCounter();\r\n }\r\n }, 1000);\r\n\r\n context.set('intervalID', intervalID);\r\n}\r\n\r\nfunction stopCounter() {\r\n clearInterval(intervalID);\r\n context.set('intervalID', undefined);\r\n}\r\n\r\nif (msg.payload.contact == true) {\r\n startCounter();\r\n} else if (msg.payload.contact == false) {\r\n stopCounter()\r\n} else {\r\n node.error('Invalid input. Must be \"start\" or \"stop\".');\r\n}" }, { "name": "LWT", "id": "d3a785ac.dc0378", "fn": "// msg object is passed to function\n\nmsg.payload = (msg.payload === 'online');\nreturn msg.payload;" } ], "egress": [ { "name": "Pass-through", "id": "5a6ab0b3.43636", "fn": "// msg object is passed to function\n\nreturn msg;" }, { "name": "Contact 2 DB", "id": "acd4d5b2.e46cf8", "fn": "msg.payload = (msg.payload.contact == true) ? '1' : '0';\nreturn msg;" } ], "thingStatus": true, "thingCommand": false, "thingOutput": false, "hbCheck": true, "hbType": "lwt", "hbTTL": "10", "hbLWT": "d3a785ac.dc0378", "hbFilterVal": "/availability", "hbFilterType": "StrEnd", "hbPropVal": "payload", "hbPropType": "msg", "filterFunction": "0", "outputs": "1" }, { "id": "e228014191d14be6", "type": "hal2EventHandler", "name": "Event handler", "contextStore": "memory", "maxlisteners": "51", "heartbeat": "10" } ]
Thank you
Here is the counter code: Is it possible to adopt and use it as an ingress, to count how long the door was opened?
var counter = 0; let intervalID = context.get('intervalID') ?? undefined;
function startCounter() { if (intervalID != undefined) { clearInterval(intervalID) } intervalID = setInterval(function () { counter++; if (counter <= 600) { node.send({ payload: counter }); } else { stopCounter(); } }, 1000);
}
function stopCounter() { clearInterval(intervalID); context.set('intervalID', undefined); }
if (msg.payload.contact == true) { startCounter(); } else if (msg.payload.contact == false) { stopCounter() } else { node.error('Invalid input. Must be "start" or "stop".'); }
[{"id":"1cc8b12467a1b9b3","type":"hal2Thing","z":"15cf35e317766a1e","eventHandler":"488bfca26161ef73","thingType":"97013256515b3580","name":"Z2M Window","attributes":[],"topicFilter":"","topicFilterType":"mqtt","topicPrefix":"","notes":"","outputs":0,"x":430,"y":660,"wires":[]},{"id":"c424fc47d3f7798e","type":"inject","z":"15cf35e317766a1e","name":"Door closed (true)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"battery\":100,\"contact\":true,\"device_temperature\":25,\"last_seen\":\"2023-03-04T17:03:27-08:00\",\"linkquality\":40,\"power_outage_count\":373,\"temperature\":25,\"voltage\":3025}","payloadType":"json","x":230,"y":640,"wires":[["1cc8b12467a1b9b3"]]},{"id":"e3f9f80227811654","type":"inject","z":"15cf35e317766a1e","name":"Door openned (false)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"battery\":100,\"contact\":false,\"device_temperature\":25,\"last_seen\":\"2023-03-04T17:03:27-08:00\",\"linkquality\":40,\"power_outage_count\":373,\"temperature\":25,\"voltage\":3025}","payloadType":"json","x":240,"y":700,"wires":[["1cc8b12467a1b9b3"]]},{"id":"488bfca26161ef73","type":"hal2EventHandler","name":"Windows Doors","maxlisteners":"50","heartbeat":"3600"},{"id":"97013256515b3580","type":"hal2ThingType","name":"Z2M Xiaomi MCCGQ11LM door / window sensor","contextStore":"memoryOnly","nodestatus":" 🚪%Contact%⏳%Contact_closed_counter%🌡️%Temperature%°F📶%Linkquality%🔋%Battery%%⚡%Voltage%V🕢%LastSeen%","nodestatusType":"str","statusFn":"// item & attribute objects are\n// passed to function as objects\n\nreturn '';","items":[{"name":"Battery","id":"38773bb6.1923d4","topicFilterType":"StrEnd","topicFilterValue":"","topicSuffix":"","type":"status","ingress":"488ff997.1fae98","egress":"5a6ab0b3.43636","notes":"","output":"1"},{"name":"Voltage","id":"a308050c.ec17f8","topicFilterType":"StrEnd","topicFilterValue":"","topicSuffix":"","type":"status","ingress":"b891197f.d119c8","egress":"5a6ab0b3.43636","notes":"","output":"1"},{"name":"Linkquality","id":"977167c1.ae6448","topicFilterType":"StrEnd","topicFilterValue":"","topicSuffix":"","type":"status","ingress":"7838e8e7.d95508","egress":"5a6ab0b3.43636","notes":"","output":"1"},{"name":"Temperature","id":"df60e448.dfe898","topicFilterType":"StrEnd","topicFilterValue":"","topicSuffix":"","type":"status","ingress":"5306119b.55bd2","egress":"5a6ab0b3.43636","notes":"","output":"1"},{"name":"Contact","id":"af0460be.09d73","topicFilterType":"StrEnd","topicFilterValue":"","topicSuffix":"","type":"status","ingress":"d2515770.de6b18","egress":"acd4d5b2.e46cf8","notes":"","output":"1"},{"name":"LastSeen","id":"800ca8b7.4b7328","topicFilterType":"str","topicFilterValue":"","topicSuffix":"","type":"status","ingress":"9f7dd133.3df26","egress":"5a6ab0b3.43636","notes":"","output":"1"},{"name":"Contact_closed_counter","id":"5a23656.d93b79c","topicFilterType":"str","topicFilterValue":"","topicSuffix":"","type":"status","ingress":"6e073250.e8a16c","egress":"5a6ab0b3.43636","notes":"","output":"1"},{"name":"Alive","id":"1","topicFilterType":"StrEnd","topicFilterValue":"/availability","topicSuffix":"","readOnly":true,"type":"status","ingress":"d3a785ac.dc0378","egress":""}],"attributes":[],"ingress":[{"name":"Pass-through","id":"c7ccebbd.016228","fn":"// msg object is passed to function\n\nreturn msg.payload;"},{"name":"Battery","id":"488ff997.1fae98","fn":"// msg object is passed to function\nif (msg.payload.battery) {\n msg.payload = Number(msg.payload.battery);\n return msg.payload;\n}\n"},{"name":"Voltage","id":"b891197f.d119c8","fn":"// msg object is passed to function\nif (msg.payload.voltage) {\n voltage = Number(msg.payload.voltage)* 0.001;\n // return msg.payload * 0.001;\n msg.payload = Math.round(voltage* 100) / 100;\n return msg.payload\n}"},{"name":"Linkquality","id":"7838e8e7.d95508","fn":"// msg object is passed to function\nif (msg.payload.linkquality) {\n msg.payload = Number(msg.payload.linkquality);\n return msg.payload;\n}"},{"name":"Temperature","id":"5306119b.55bd2","fn":"// msg object is passed to function\nif (msg.payload.temperature) {\n tempc = Number(msg.payload.temperature);\n tempf = tempc * 9/5 + 32;\n msg.payload = Math.round(tempf* 10) / 10;\n return msg.payload\n}"},{"name":"Contact","id":"d2515770.de6b18","fn":"msg.payload = (msg.payload.contact == true) ? 'closed' : 'opened';\nreturn msg.payload;"},{"name":"LastSeen","id":"9f7dd133.3df26","fn":"// msg object is passed to function\nif (msg.payload.last_seen) {\n msg.payload = String(msg.payload.last_seen);\n return msg.payload;\n}"},{"name":"closed_counter","id":"6e073250.e8a16c","fn":"var counter = 0;\r\nlet intervalID = context.get('intervalID') ?? undefined;\r\n\r\nfunction startCounter() {\r\n if (intervalID != undefined) { clearInterval(intervalID) }\r\n intervalID = setInterval(function () {\r\n counter++;\r\n if (counter <= 9) {\r\n node.send({ payload: counter });\r\n } else {\r\n stopCounter();\r\n }\r\n }, 1000);\r\n\r\n context.set('intervalID', intervalID);\r\n}\r\n\r\nfunction stopCounter() {\r\n clearInterval(intervalID);\r\n context.set('intervalID', undefined);\r\n}\r\n\r\nif (msg.payload.contact == true) {\r\n startCounter();\r\n} else if (msg.payload.contact == false) {\r\n stopCounter()\r\n} else {\r\n node.error('Invalid input. Must be \"start\" or \"stop\".');\r\n}"},{"name":"LWT","id":"d3a785ac.dc0378","fn":"// msg object is passed to function\n\nmsg.payload = (msg.payload === 'online');\nreturn msg.payload;"}],"egress":[{"name":"Pass-through","id":"5a6ab0b3.43636","fn":"// msg object is passed to function\n\nreturn msg;"},{"name":"Contact 2 DB","id":"acd4d5b2.e46cf8","fn":"msg.payload = (msg.payload.contact == true) ? '1' : '0';\nreturn msg;"}],"thingStatus":true,"thingCommand":false,"thingOutput":false,"hbCheck":true,"hbType":"lwt","hbTTL":"10","hbLWT":"d3a785ac.dc0378","hbFilterVal":"/availability","hbFilterType":"StrEnd","hbPropVal":"payload","hbPropType":"msg","filterFunction":"0","outputs":"1"}]