Scene change example using flowman

This is an example of using flowman to load a set of flows, delete a set of flows, and save a set of template flows (in preparation for loading them). Note that whilst making this, I find that flowman/addflow and flowman/delflow don't like to be pumped with messages too quickly - I think because of the async nature of the underlying NR flow saving mechanism. So I put in 1 msg per sec rate limiting; not an ideal solution.

Requires node-red-contrib-flowman

[{"id":"23945734.109c88","type":"getflows","z":"c7fab6c6.29dbd8","name":"","x":280,"y":180,"wires":[["4da195ca.3ff7dc","37d2ec94.212534"]]},{"id":"3d6d183b.ce46f8","type":"inject","z":"c7fab6c6.29dbd8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":180,"wires":[["23945734.109c88"]]},{"id":"4da195ca.3ff7dc","type":"debug","z":"c7fab6c6.29dbd8","name":"","active":true,"console":"false","complete":"true","x":870,"y":120,"wires":[]},{"id":"37d2ec94.212534","type":"function","z":"c7fab6c6.29dbd8","name":"for each 'scene-'","func":"\n\nvar msgs = {};\n\nfor (var i = 0; i < msg.payload.length; i++){\n    if (msg.payload[i].label.startsWith('scene-')){\n        msgs.newmsg = {\n            flowId: msg.payload[i].id\n        };\n        node.warn(\"found \" + msg.payload[i].label);\n        node.send(msgs.newmsg);\n        delete msgs.newmsg;\n        \n    }\n}\nreturn;","outputs":1,"noerr":0,"x":480,"y":180,"wires":[["495880aa.902b9"]]},{"id":"e4fd7a7f.670a78","type":"delflow","z":"c7fab6c6.29dbd8","name":"","enabled":true,"x":850,"y":180,"wires":[[]]},{"id":"ecba48d0.8fa3e8","type":"comment","z":"c7fab6c6.29dbd8","name":"Delete flows starting with 'scene-'","info":"","x":190,"y":140,"wires":[]},{"id":"b47c7775.0828c8","type":"inject","z":"c7fab6c6.29dbd8","name":"","topic":"","payload":"[\"scene-1\", \"scene-2\", \"scene-3\"]","payloadType":"json","repeat":"","crontab":"","once":false,"x":110,"y":280,"wires":[["e57d1b44.175d88"]]},{"id":"e57d1b44.175d88","type":"function","z":"c7fab6c6.29dbd8","name":"","func":"var scenepath = 'c:\\\\datanobackup\\\\scenes\\\\';\n\nvar msgs = {};\n\nfor (var i = 0; i < msg.payload.length; i++){\n    msgs.newmsg = {\n        filename: scenepath + msg.payload[i] + '.flow'\n    };\n    \n    node.send(msgs.newmsg);\n    \n    delete msgs.newmsg;\n}\nreturn;","outputs":1,"noerr":0,"x":270,"y":280,"wires":[["9a1aa612.d98bc8"]]},{"id":"9a1aa612.d98bc8","type":"file in","z":"c7fab6c6.29dbd8","name":"","filename":"","format":"utf8","chunk":false,"sendError":false,"x":410,"y":280,"wires":[["aced92d4.3ad29"]]},{"id":"aced92d4.3ad29","type":"json","z":"c7fab6c6.29dbd8","name":"","pretty":false,"x":550,"y":280,"wires":[["5df1f17.1c3531","de9716bf.1933d8"]]},{"id":"f4997c98.eb68d","type":"addflow","z":"c7fab6c6.29dbd8","name":"","x":1020,"y":280,"wires":[["232ea4e1.20182c"]]},{"id":"de9716bf.1933d8","type":"function","z":"c7fab6c6.29dbd8","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"x":690,"y":280,"wires":[["214f62c.606189e"]]},{"id":"5df1f17.1c3531","type":"debug","z":"c7fab6c6.29dbd8","name":"","active":true,"console":"false","complete":"false","x":700.8264312744141,"y":351.5555725097656,"wires":[]},{"id":"9268f778.f29c58","type":"comment","z":"c7fab6c6.29dbd8","name":"load a scene","info":"","x":130,"y":240,"wires":[]},{"id":"526143cb.8da50c","type":"comment","z":"c7fab6c6.29dbd8","name":"save scene templates","info":"","x":160,"y":440,"wires":[]},{"id":"b7e41d28.dc92","type":"inject","z":"c7fab6c6.29dbd8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":500,"wires":[["2b0f6985.587ce6"]]},{"id":"2b0f6985.587ce6","type":"getflows","z":"c7fab6c6.29dbd8","name":"","x":280,"y":500,"wires":[["dd11272b.0ffcb8"]]},{"id":"dd11272b.0ffcb8","type":"function","z":"c7fab6c6.29dbd8","name":"for each 'template-'","func":"for (var i = 0; i < msg.payload.length; i++){\n    \n    if (msg.payload[i].label.startsWith('template-')){\n        var newmsg = {\n            id: msg.payload[i].id\n        };\n        node.send(newmsg);\n    }\n}\nreturn;","outputs":1,"noerr":0,"x":490,"y":500,"wires":[["d62308db.5faeb8"]]},{"id":"d62308db.5faeb8","type":"getflows","z":"c7fab6c6.29dbd8","name":"","x":680,"y":500,"wires":[["a56f4162.cd1e2"]]},{"id":"a56f4162.cd1e2","type":"function","z":"c7fab6c6.29dbd8","name":"modify label and set filename","func":"var scenepath = 'c:\\\\datanobackup\\\\scenes\\\\';\n\nmsg.payload.label = msg.payload.label.replace(\"template-\", 'scene-');\n\nmsg.filename = scenepath + msg.payload.label + '.flow';\n\n\nreturn msg;","outputs":1,"noerr":0,"x":900,"y":500,"wires":[["d8013193.8adab"]]},{"id":"d8013193.8adab","type":"file","z":"c7fab6c6.29dbd8","name":"","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"true","x":1090,"y":500,"wires":[]},{"id":"232ea4e1.20182c","type":"debug","z":"c7fab6c6.29dbd8","name":"","active":true,"console":"false","complete":"true","x":1150,"y":280,"wires":[]},{"id":"214f62c.606189e","type":"delay","z":"c7fab6c6.29dbd8","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":849.8368682861328,"y":281.2778015136719,"wires":[["f4997c98.eb68d"]]},{"id":"495880aa.902b9","type":"delay","z":"c7fab6c6.29dbd8","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":670,"y":180,"wires":[["e4fd7a7f.670a78","4da195ca.3ff7dc"]]}]
btsimonh

Flow Info

created 2 months, 1 week ago

Node Types

Core
  • comment (x3)
  • debug (x3)
  • delay (x2)
  • file (x1)
  • file in (x1)
  • function (x5)
  • inject (x3)
  • json (x1)
Other

Tags

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