Domoticz Status Check (for switches)

SubFlow with status indication to check if a Domoticz Device (as IDX) has the same status as the one provided.

requires a msg.request object with the following keys:

msg.request.idx ==> the domoticz Device IDX

msg.request.value ==> could be ON or OFF

msg.request.type ==> Device type: 0 Group/Scenario; 1 Switch (On/Off); 4 Temperature; 5 Level % 0~100

msg.request.hyst ==> Hysteresis value for dimmers and temps (default value 0)

the subflow compare the result from the HTTP rest api and will return a msg.result key: msg.result=True ==> the status matches the provided one msg.result=False ==> the status does NOT matches the provided one msg.result=Failed ==> something went wrong

the msg object returned contains the http request result also.

Added an option to automatically generate a mqtt telegram to update Domoticz (out 2)

[{"id":"f8c7c698.ab3cb8","type":"subflow","name":"dzChecker","info":"","category":"","in":[{"x":70,"y":80,"wires":[{"id":"f6518297.060b9"}]}],"out":[{"x":680,"y":30,"wires":[{"id":"e2b301d8.67d24","port":0}]},{"x":680,"y":130,"wires":[{"id":"e2b301d8.67d24","port":2}]}],"env":[{"name":"dzHost","type":"str","value":"127.0.0.1"},{"name":"dzPort","type":"num","value":"8080"},{"name":"dzUpdate","type":"bool","value":"true"}],"status":{"x":680,"y":80,"wires":[{"id":"e2b301d8.67d24","port":1}]}},{"id":"f6518297.060b9","type":"function","z":"f8c7c698.ab3cb8","name":"create httpReq","func":"var dzHost = env.get('dzHost');\nvar dzPort = env.get('dzPort');\n\nvar dzReq = {\n    [1] : {\"url\":'http://'+dzHost+':'+dzPort+'/json.htm?type=devices&rid='+msg.request.idx, \"request\": msg.request},\n    [4] : {\"url\":'http://'+dzHost+':'+dzPort+'/json.htm?type=devices&rid='+msg.request.idx, \"request\": msg.request}, \n    [5] : {\"url\":'http://'+dzHost+':'+dzPort+'/json.htm?type=devices&rid='+msg.request.idx, \"request\": msg.request}, \n    [0] : {\"url\":'http://'+dzHost+':'+dzPort+'/json.htm?type=scenes&rid='+ msg.request.idx, \"request\": msg.request},  \n    \n}\nmsg = dzReq[msg.request.type];\nreturn msg;","outputs":1,"noerr":0,"x":210,"y":80,"wires":[["755244c3.ec493c"]]},{"id":"e2b301d8.67d24","type":"function","z":"f8c7c698.ab3cb8","name":"Evaluating","func":"\nvar mapDzCmd = {\n    [0] :{\"payload\":{\"command\": \"switchscene\", \"idx\": msg.request.idx, \"switchcmd\": (typeof msg.request.value == \"string\" ? msg.request.value.charAt(0) + msg.request.value.slice(1).toLowerCase() : \"\") } },\n    [1] :{\"payload\":{\"command\": \"switchlight\", \"idx\": msg.request.idx, \"switchcmd\": (typeof msg.request.value == \"string\" ? msg.request.value.charAt(0) + msg.request.value.slice(1).toLowerCase() : \"\") } },\n    [4] :{\"payload\":{\"idx\": msg.request.idx, \"nvalue\" : 0, \"svalue\" : msg.request.value.toString() } }, \n    [5] :{\"payload\":{\"command\": \"switchlight\", \"idx\": msg.request.idx, \"switchcmd\": \"Set Level\", \"level\": parseInt(msg.request.value) } }\n\n}\nvar hyst = (msg.request.hasOwnProperty('hyst') === true ? parseInt(msg.request.hyst) : 0 );\nvar sMsg = {\"status\":{fill:\"red\",shape:\"dot\",text:\"Failed for idx\" + msg.request.idx}};\nmsg.request.result = \"Failed\";\n\nvar mMsg = {};\nif (msg.payload.status == \"OK\") {\n    switch (msg.request.type) {\n    case 0:     // Gruppo\n    case 1:     // Interruttore\n        if (msg.payload.result[0].Data.toUpperCase() == msg.request.value.toUpperCase()) {\n            msg.request.result = true;\n            sMsg = {\"status\":{fill:\"green\",shape:\"ring\",text:\"True for idx\" + msg.request.idx}};\n            mMsg = null;\n        } else {\n            msg.request.result = false;\n            sMsg = {\"status\":{fill:\"red\",shape:\"ring\",text:\"False for idx\" + msg.request.idx}};\n            if (env.get('dzUpdate') === true){\n                mMsg = mapDzCmd[msg.request.type];\n            }\n        }\n        break;\n    case 4:     // Temperatura\n        if (Math.abs(parseInt(msg.payload.result[0].Data) - parseInt(msg.request.value)) <= hyst) {\n            msg.request.result = true;\n            sMsg = {\"status\":{fill:\"green\",shape:\"ring\",text:\"True for idx\" + msg.request.idx}};\n            mMsg = null;\n        } else {\n            msg.request.result = false;\n            sMsg = {\"status\":{fill:\"red\",shape:\"ring\",text:\"False for idx\" + msg.request.idx}};\n            if  (env.get('dzUpdate') === true){\n                mMsg = mapDzCmd[msg.request.type];\n            }\n        }\n        break;    \n    case 5:     // Valone percentuale 1~100\n        //node.warn(\"Calcolo: \" + Math.abs(parseInt(msg.payload.result[0].Level) - parseInt(msg.request.value)) + \" / Hyst: \" + hyst);\n        if (Math.abs(parseInt(msg.payload.result[0].Level) - parseInt(msg.request.value)) <= hyst) {\n            msg.request.result = true;\n            sMsg = {\"status\":{fill:\"green\",shape:\"ring\",text:\"True\"}};\n            mMsg = null;\n        } else {\n            msg.request.result = false;\n            sMsg = {\"status\":{fill:\"red\",shape:\"ring\",text:\"False\"}};\n            if (env.get('dzUpdate') === true){\n                mMsg = mapDzCmd[msg.request.type];\n            }\n        }\n    }\n\n}\n\nreturn [msg, sMsg, mMsg];","outputs":3,"noerr":0,"x":550,"y":80,"wires":[[],[],[]],"outputLabels":["Updated msg object","Status Object","Domoticz MQTT msg"]},{"id":"755244c3.ec493c","type":"http request","z":"f8c7c698.ab3cb8","name":"","method":"POST","ret":"obj","paytoqs":false,"url":"","tls":"","proxy":"","authType":"","x":390,"y":80,"wires":[["e2b301d8.67d24"]]}]

Flow Info

Created 6 years, 1 month ago
Updated 5 years, 2 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • function (x2)
  • http request (x1)
Other
  • subflow (x1)

Tags

  • domoticz
  • subflow
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option