Created
December 31, 2021 19:17
-
-
Save gtrabanco/2b8704b64a4a57e1faf1bf163ecf5843 to your computer and use it in GitHub Desktop.
Subflow limpieza condicional
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [{"id":"6d1ab575c8ee62e1","type":"subflow","name":"Conditional Clean Segments","info":"Requirements:\n- [Extra configuration for the vacuum](https://gist.github.com/gtrabanco/87320443dcd30ac10990f87b0c423e19)","category":"","in":[{"x":60,"y":260,"wires":[{"id":"291669f61c673f20"}]}],"out":[{"x":1780,"y":180,"wires":[{"id":"fd8a618a491d08a4","port":0}]},{"x":1780,"y":260,"wires":[{"id":"fd8a618a491d08a4","port":1}]}],"env":[{"name":"has_map_sensor","type":"str","value":"binary_sensor.vacuum_has_pending_map","ui":{"label":{"en-US":"Entity of binary sensor for pending map"},"type":"input","opts":{"types":["str"]}}},{"name":"segments","type":"json","value":"[]","ui":{"label":{"en-US":"Array of segments to be cleaned ("},"type":"input","opts":{"types":["num","json"]}}},{"name":"segments_sensor","type":"str","value":"sensor.map_segments","ui":{"label":{"en-US":"Sensor that has the segments"},"type":"input","opts":{"types":["str"]}}},{"name":"mqtt_prefix","type":"str","value":"valetudo","ui":{"label":{"en-US":"MQTT Vacuum Prefix"},"type":"input","opts":{"types":["str"]}}},{"name":"mqtt_identifier","type":"str","value":"robot","ui":{"label":{"en-US":"MQTT Vacuum Indentifier"},"type":"input","opts":{"types":["str"]}}},{"name":"vacuum_entity","type":"str","value":"","ui":{"label":{"en-US":"Vacuum entity name"},"type":"input","opts":{"types":["str"]}}},{"name":"fan","type":"str","value":"low","ui":{"label":{"en-US":"Fan Speed when map pending"},"type":"select","opts":{"opts":[{"l":{"en-US":"Low (default)"},"v":"low"},{"l":{"en-US":"Medium"},"v":"medium"},{"l":{"en-US":"High"},"v":"high"},{"l":{"en-US":"Turbo"},"v":"turbo"},{"l":{"en-US":"Max"},"v":"max"}]}}}],"meta":{},"color":"#DDAA99","status":{"x":1780,"y":340,"wires":[{"id":"bdbf5883296aeede","port":0}]}},{"id":"bf7a27554454c7eb","type":"comment","z":"6d1ab575c8ee62e1","name":"Segments","info":"View your segments in:\n- sensor.map_segments","x":1140,"y":340,"wires":[]},{"id":"aa4213830e3437b2","type":"change","z":"6d1ab575c8ee62e1","name":"Clean All The house","rules":[{"t":"set","p":"payload","pt":"msg","to":"START","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"data.topic","tot":"msg"},{"t":"set","p":"qos","pt":"msg","to":"1","tot":"num"},{"t":"set","p":"retain","pt":"msg","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1160,"y":220,"wires":[["279fb65b6f353a62","2de8584b85241208"]]},{"id":"23a854c5ac8adcdd","type":"change","z":"6d1ab575c8ee62e1","name":"Fan Speed: Low","rules":[{"t":"set","p":"payload","pt":"msg","to":"low","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"data.topic","tot":"msg"},{"t":"set","p":"qos","pt":"msg","to":"1","tot":"str"},{"t":"set","p":"retain","pt":"msg","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":180,"wires":[["75932e11e50e4d96","279fb65b6f353a62"]]},{"id":"75932e11e50e4d96","type":"delay","z":"6d1ab575c8ee62e1","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":960,"y":180,"wires":[["aa4213830e3437b2"]]},{"id":"f6ddb4a72db2203e","type":"change","z":"6d1ab575c8ee62e1","name":"Fan Speed: High","rules":[{"t":"set","p":"payload","pt":"msg","to":"high","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"data.topic","tot":"msg"},{"t":"set","p":"qos","pt":"msg","to":"1","tot":"str"},{"t":"set","p":"retain","pt":"msg","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":300,"wires":[["4e5109993830bed1","279fb65b6f353a62"]]},{"id":"23703f8d37192087","type":"switch","z":"6d1ab575c8ee62e1","name":"Clean Type","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"full","vt":"str"},{"t":"eq","v":"continue","vt":"str"},{"t":"eq","v":"segments","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":510,"y":260,"wires":[["23a854c5ac8adcdd"],["aa4213830e3437b2"],["f6ddb4a72db2203e"]]},{"id":"4e5109993830bed1","type":"delay","z":"6d1ab575c8ee62e1","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":960,"y":300,"wires":[["9fd5683fa72ddc40"]]},{"id":"279fb65b6f353a62","type":"mqtt out","z":"6d1ab575c8ee62e1","name":"","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"","x":1350,"y":100,"wires":[]},{"id":"291669f61c673f20","type":"function","z":"6d1ab575c8ee62e1","name":"Check pending map or no segments","func":"const haCtx = global.get(\"homeassistant\");\nconst configCtx = haCtx.homeAssistant;\n\nconst pendingMapSensor = env.get(\"has_map_sensor\") ||\n \"binary_sensor.vacuum_has_pending_map\";\nconst segmentsSensor = env.get(\"segments_sensor\") ||\n \"sensor.map_segments\";\nconst vacuum_entity = env.get(\"vacuum_entity\") ||\n \"vacuum.robot\";\n\nconst mqtt_prefix = env.get(\"mqtt_prefix\");\nconst vacuum_identifier = env.get(\"mqtt_identifier\");\n\n//Status\nlet status = \"unknown\";\nlet finalSegments = [];\nconst currentNumberOfSegments = (\n ! configCtx.states[segmentsSensor] ||\n isNaN(parseInt(configCtx.states[segmentsSensor].state)))?\n 0:\n parseInt(configCtx.states[segmentsSensor].state);\n \nif (\n configCtx.states[vacuum_entity] &&\n configCtx.states[vacuum_entity].status == \"paused\"\n) {\n status = \"continue\";\n} else if (\n currentNumberOfSegments > 0\n) {\n status = \"segments\"\n \n // Segments configuration\n let optSegments = [];\n if (msg.payload.segments) {\n optSegments = isNaN(msg.payload.segments) ?\n msg.payload.segments:\n [msg.payload.segments];\n } else {\n optSegments = Array.isArray(env.get(\"segments\")) ?\n env.get(\"segments\"):\n [env.get(\"segments\")];\n }\n const vacuum_segments = configCtx.states[segmentsSensor] ?\n configCtx.states[segmentsSensor].attributes:\n [];\n // Add named segments\n optSegments.map((segment) => {\n if (isNaN(segment)) {\n Object.keys(vacuum_segments).forEach((segmentId) => {\n if (segment.toLowerCase() == vacuum_segments[segmentId].toLowerCase()) {\n finalSegments.push(segmentId);\n return\n }\n });\n \n return\n }\n \n finalSegments.push(segment);\n });\n \n if (finalSegments.length == 0) {\n status = \"full\"\n }\n} else if (\n configCtx.states[pendingMapSensor] &&\n configCtx.states[pendingMapSensor].state == \"on\"\n){\n status = \"full\"\n}\n\nnode.warn(configCtx.states[vacuum_entity].state);\nnode.warn(configCtx.states[segmentsSensor].state);\nnode.warn(configCtx.states[pendingMapSensor].state);\n\n\nreturn {\n payload: status,\n data: {\n vacuum_entity: vacuum_entity,\n prefix: mqtt_prefix,\n identifier: vacuum_identifier,\n topic: `${mqtt_prefix}/${vacuum_identifier}/MapSegmentationCapability/clean/set`,\n segments: finalSegments\n }\n};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":260,"wires":[["23703f8d37192087"]]},{"id":"2de8584b85241208","type":"delay","z":"6d1ab575c8ee62e1","name":"","pauseType":"delay","timeout":"30","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1360,"y":260,"wires":[["bdbf5883296aeede"]]},{"id":"bdbf5883296aeede","type":"function","z":"6d1ab575c8ee62e1","name":"","func":"const haCtx = global.get(\"homeassistant\");\nconst configCtx = haCtx.homeAssistant;\n\nconst vacuum_entity = env.get(\"vacuum_entity\") ||\n \"vacuum.robot\";\n\nconst statusNode = configCtx.states[vacuum_entity]?\n configCtx.states[vacuum_entity].state == \"cleaning\":\n false;\n \nmsg.payload = statusNode ? \"on\" : \"off\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1500,"y":260,"wires":[["fd8a618a491d08a4"]]},{"id":"fd8a618a491d08a4","type":"switch","z":"6d1ab575c8ee62e1","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1650,"y":220,"wires":[[],[]]},{"id":"9fd5683fa72ddc40","type":"function","z":"6d1ab575c8ee62e1","name":"","func":"msg = {\n data: msg.data,\n payload: {\n \"segment_ids\": msg.data.segments,\n \"iterations\": 2,\n \"customOrder\": true\n },\n topic: msg.data.topic,\n qos: 1,\n retain: 0\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1140,"y":300,"wires":[["279fb65b6f353a62","2de8584b85241208"]]},{"id":"edaabdef18354fe5","type":"comment","z":"6d1ab575c8ee62e1","name":"Configure your MQTT","info":"","x":1400,"y":60,"wires":[]}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment