Lighting controller ideas

Here you go...

[{"id":"3984433f.b0374c","type":"function","z":"553814a2.1248ec","name":"Make Observable","func":"\n/**\n* make properties of `object` Observable\n* @param {string} name  key name to store `object` in `global` or `flow` context\n* @param {object} object  the object to make observable\n*/\nfunction makeObservable(name, object) {\n    let store = null;\n    let nameParts = name.split(\".\");\n    if(nameParts.length < 2) {\n        node.error(\"name must be global.xxx or flow.xxx\", msg);\n        return null;\n    }\n\n    switch (nameParts[0]) {\n        case \"flow\":\n            store = flow;\n            break;\n        case \"global\":\n            store = global;\n            break;\n        default:\n            node.error(\"name must be global.xxx or flow.xxx\", msg);\n            return null;\n    }\n\n    nameParts.shift();//drop the first item\n    name = nameParts.join(\".\");\n\n    if (!object) {\n        object = store.get(name);\n    }\n    if (!object || typeof object != \"object\") {\n        node.error(`msg.payload or '${name}' is not an object`, msg);\n        return null;\n    }\n\n\n    observe(object);\n    store.set(name, object);\n\n    return object;\n\n    /**\n    * @param {string} name\n    * @param {any} value\n    */\n    function notify(name, value) {\n        var m = {\n            topic: name,\n            payload: value\n        }\n        node.send(m);\n    }\n\n    /**\n    * @param {object} obj  the object to make reactive\n    * @param {string} key  the item in the data object\n    */\n    function makeReactive(obj, key) {\n        let val = obj[key];\n        Object.defineProperty(obj, key, {\n            get() {\n                return val;\n            },\n            set(newVal) {\n                val = newVal;\n                notify(name + \".\" + key, val);\n            }\n        })\n    }\n\n    /**\n    * @param {object} obj  the object to observe\n    */\n    function observe(obj) {\n        for (let key in obj) {\n            if (obj.hasOwnProperty(key)) {\n                makeReactive(obj, key);\n            }\n        }\n    }\n}\n\nmsg.payload = makeObservable(msg.topic, msg.payload);\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2610,"y":480,"wires":[["d3ad2877.7bdec8"]]},{"id":"83d19ad4.6d3768","type":"inject","z":"553814a2.1248ec","name":"Observe global.sensor.values","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"global.sensor.values","payload":"{\"sensor1\":0,\"sensor2\":0,\"sensor3\":0,\"sensor4\":0}","payloadType":"json","x":2260,"y":360,"wires":[["3984433f.b0374c"]]},{"id":"e0229828.fbc678","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":2670,"y":620,"wires":[["97b6542.bc93aa8"]]},{"id":"49baae3b.93f0f","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":2670,"y":580,"wires":[["97b6542.bc93aa8"]]},{"id":"97b6542.bc93aa8","type":"change","z":"553814a2.1248ec","name":"","rules":[{"t":"set","p":"sensor.values.sensor2","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2890,"y":600,"wires":[[]]},{"id":"6cff8090.6601f","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"high","payloadType":"str","x":2190,"y":620,"wires":[["e30a0606.89df38"]]},{"id":"8cc38216.10979","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"low","payloadType":"str","x":2190,"y":580,"wires":[["e30a0606.89df38"]]},{"id":"e30a0606.89df38","type":"change","z":"553814a2.1248ec","name":"","rules":[{"t":"set","p":"sensor.values.sensor1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2430,"y":600,"wires":[[]]},{"id":"a0fba143.0a78a","type":"inject","z":"553814a2.1248ec","name":"Observe flow.plc_values","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"flow.plc_values","payload":"{\"DM0001\":0,\"DM0002\":0,\"DM0003\":0,\"DM0004\":0}","payloadType":"json","x":2250,"y":460,"wires":[["3984433f.b0374c"]]},{"id":"c3d1d0db.df3a8","type":"change","z":"553814a2.1248ec","name":"","rules":[{"t":"set","p":"plc_values.DM0001","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2420,"y":800,"wires":[[]]},{"id":"d7a4a609.804718","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2222","payloadType":"num","x":2190,"y":820,"wires":[["c3d1d0db.df3a8"]]},{"id":"4b832cc0.a11d94","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1111","payloadType":"num","x":2190,"y":780,"wires":[["c3d1d0db.df3a8"]]},{"id":"8173e9b.5b56e18","type":"change","z":"553814a2.1248ec","name":"","rules":[{"t":"set","p":"plc_values.DM0005","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2880,"y":800,"wires":[[]]},{"id":"eba61d84.683d9","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"66","payloadType":"num","x":2670,"y":820,"wires":[["8173e9b.5b56e18"]]},{"id":"ac24a89f.7fd018","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","v":"55","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"55","payloadType":"num","x":2670,"y":780,"wires":[["8173e9b.5b56e18"]]},{"id":"9b185180.5b2b3","type":"inject","z":"553814a2.1248ec","name":"Refresh flow.plc_values","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"flow.plc_values","x":2240,"y":500,"wires":[["3984433f.b0374c"]]},{"id":"45313bd2.09c144","type":"inject","z":"553814a2.1248ec","name":"Refresh global.sensor.values","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"global.sensor.values","x":2260,"y":400,"wires":[["3984433f.b0374c"]]},{"id":"d6f4bd71.a2c8e","type":"change","z":"553814a2.1248ec","name":"","rules":[{"t":"set","p":"sensor.values.sensor5","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2430,"y":680,"wires":[[]]},{"id":"f641e704.8a0338","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"123.456","payloadType":"num","x":2200,"y":700,"wires":[["d6f4bd71.a2c8e"]]},{"id":"21bf4e02.f248d2","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"543.210","payloadType":"num","x":2200,"y":660,"wires":[["d6f4bd71.a2c8e"]]},{"id":"698c39c4dd359765","type":"group","z":"553814a2.1248ec","name":"","style":{"stroke":"#ff7f7f","stroke-opacity":"0.86","label":true,"fill":"#ff7f7f","fill-opacity":"0.28"},"nodes":["9df73953f604e906","880a13bd644bad88","eac6e314211ea3f0","d3ad2877.7bdec8","b0703dcf4fb665d8","1d96f8b68f16c3b3","7e2c7bd2158d0f13","7749ee1d409c6e16","103d8b20eec86085","ffcfd386d852e8f3","e2d004abed594f59","82c2e22730c8deb1","1ea1e6eac0f30b6a"],"env":[],"x":2794,"y":279,"w":212,"h":242},{"id":"9df73953f604e906","type":"comment","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"WATCH THIS","info":"","x":2895,"y":440,"wires":[],"l":false},{"id":"880a13bd644bad88","type":"comment","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"WATCH THIS","info":"","x":2895,"y":420,"wires":[],"l":false},{"id":"eac6e314211ea3f0","type":"comment","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"WATCH THIS","info":"","x":2895,"y":420,"wires":[],"l":false},{"id":"d3ad2877.7bdec8","type":"debug","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"topic & \": \" & payload","statusType":"jsonata","x":2890,"y":480,"wires":[]},{"id":"b0703dcf4fb665d8","type":"comment","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"WATCH THIS","info":"","x":2875,"y":420,"wires":[],"l":false},{"id":"1d96f8b68f16c3b3","type":"comment","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"WATCH THIS","info":"","x":2915,"y":420,"wires":[],"l":false},{"id":"7e2c7bd2158d0f13","type":"comment","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"WATCH THIS","info":"","x":2855,"y":400,"wires":[],"l":false},{"id":"7749ee1d409c6e16","type":"comment","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"WATCH THIS","info":"","x":2935,"y":400,"wires":[],"l":false},{"id":"103d8b20eec86085","type":"comment","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"WATCH THIS","info":"","x":2895,"y":400,"wires":[],"l":false},{"id":"ffcfd386d852e8f3","type":"comment","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"WATCH THIS","info":"","x":2895,"y":380,"wires":[],"l":false},{"id":"e2d004abed594f59","type":"comment","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"WATCH THIS","info":"","x":2895,"y":360,"wires":[],"l":false},{"id":"82c2e22730c8deb1","type":"comment","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"WATCH THIS","info":"","x":2895,"y":340,"wires":[],"l":false},{"id":"1ea1e6eac0f30b6a","type":"comment","z":"553814a2.1248ec","g":"698c39c4dd359765","name":"WATCH THIS","info":"","x":2890,"y":320,"wires":[]}]
4 Likes