vinodsr / node-red-contrib-tuya-smart-device

a node-red module for managing tuya smart devices
https://flows.nodered.org/node/node-red-contrib-tuya-smart-device
MIT License
47 stars 15 forks source link

Example: how to use REFRESH #56

Closed msillano closed 3 years ago

msillano commented 3 years ago

The new tuyapi REFRESH operation (see issue54) can be easily handled with the following flow:

[{"id":"293b07da.95ad08","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"c30a6be5.9fdf78","type":"inject","z":"293b07da.95ad08","name":"SET FAST","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"dps\":\"_fast\",\"set\":true}","payloadType":"json","x":140,"y":80,"wires":[["eccbc7b6.1ffa78"]]},{"id":"62a20a49.6f81b4","type":"inject","z":"293b07da.95ad08","name":"SET SLOW","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"dps\":\"_fast\",\"set\":false}","payloadType":"json","x":150,"y":120,"wires":[["eccbc7b6.1ffa78"]]},{"id":"1f720d7e.950623","type":"inject","z":"293b07da.95ad08","name":"GET","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"operation\":\"GET\",\"dps\":\"_fast\"}","payloadType":"json","x":130,"y":160,"wires":[["eccbc7b6.1ffa78"]]},{"id":"ef226712.feeda8","type":"tuya-smart-device","z":"293b07da.95ad08","deviceName":"meter","deviceId":"486441603c6105*****","deviceKey":"84bb4******","deviceIp":"","retryTimeout":"2279","findTimeout":"6291","tuyaVersion":"3.3","x":750,"y":200,"wires":[["63a04dbd.11fc44"]]},{"id":"eccbc7b6.1ffa78","type":"function","z":"293b07da.95ad08","name":"fast control","func":"\nconst FASTDP = \"_fast\";\n// same values as tuya device node\nconst DEVID = \"486441603c6105*****\";\nconst DEVNAME = \"meter\";\n\n// builds the output message\nvar newMsg = {\n    \"payload\": {\n        \"deviceId\": DEVID,\n        \"deviceName\": DEVNAME, // as device node\n        \"data\": {\n            \"dps\": {}\n        }\n    }\n};\n//  SET/GET case\nif (msg.payload.dps === FASTDP) {\n    if (msg.payload.set !== undefined) { // is set\n        context.set(\"fast\", msg.payload.set);\n        newMsg.payload.data.dps[FASTDP] = msg.payload.set;\n        if (msg.payload.set) {\n            return ([{ payload: { operation: \"RFR\" } }, null, newMsg]);\n        } else {\n            return ([{ payload: \"stop\" }, null, newMsg]);\n        }\n    } else { // is get\n        newMsg.payload.data.dps[FASTDP] = context.get(\"fast\");\n        return ([null, null, newMsg]);\n    }\n}\n// MULTIPLE case\nif ((msg.payload.data !== undefined) && (msg.payload.data[FASTDP] !== undefined)) {\n    let fst = msg.payload.data[FASTDP];\n    delete msg.payload.data[FASTDP];\n    context.set(\"fast\", fst);\n    newMsg.payload.data.dps[FASTDP] = fst;\n    if (fst) {\n        return ([{ payload: { operation: \"RFR\" }}, msg, newMsg]);\n    } else {\n        return ([{ payload: \"stop\" }, msg, newMsg]);\n    }\n}\n// SCHEMA case\nif (msg.payload.schema !== undefined) {\n    newMsg.payload.data.dps[FASTDP] = context.get(\"fast\");\n    return ([null, msg, newMsg]);\n}\n\nreturn [null, msg];\n","outputs":3,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is deployed.\ncontext.set(\"fast\",0);","finalize":"","x":430,"y":220,"wires":[["3b402872.17d208"],["ef226712.feeda8"],["63a04dbd.11fc44"]]},{"id":"3b402872.17d208","type":"looptimer2","z":"293b07da.95ad08","duration":"6","units":"Second","maxloops":"20","maxtimeout":"3","maxtimeoutunits":"Minute","name":"fast rate","x":600,"y":180,"wires":[["ef226712.feeda8"],[]]},{"id":"c29a682a.613dd8","type":"inject","z":"293b07da.95ad08","name":"MULTIPLE FAST","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"multiple\":true,\"data\":{\"1\":true,\"_fast\":true}}","payloadType":"json","x":160,"y":200,"wires":[["eccbc7b6.1ffa78"]]},{"id":"7f00b50e.36125c","type":"inject","z":"293b07da.95ad08","name":"SCHEMA","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"operation\":\"GET\", \"schema\":true}","payloadType":"json","x":140,"y":280,"wires":[["eccbc7b6.1ffa78"]]},{"id":"63a04dbd.11fc44","type":"debug","z":"293b07da.95ad08","name":"TEST","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":240,"wires":[]},{"id":"aee9fe1a.db9fb","type":"inject","z":"293b07da.95ad08","name":"MULTIPLE SLOW","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"multiple\":true,\"data\":{\"1\":true,\"_fast\":false}}","payloadType":"json","x":170,"y":240,"wires":[["eccbc7b6.1ffa78"]]}]

Works with 4.0.1 modified. This flow adds a new DP to a device (e.g. the '_fast' dp, but you can change this) with values false|true. In FAST mode, the node 'fast rate' defines the sampling rate (e.g. smartLife uses 5 sec.) and the max. FAST mode duration. The new '_fast' DP is compatible with SET/GET/MULTIPLE/SCHEMA commands.

Best regards m.s. refresh02