Hey,
So I’ve created a pretty elaborate flow to control my lights from ONE xiaomi Mi Cube.
It functions like this:
Flip 90 - changes the light that’s selected
Tap Twice - toggles the light that’s selected
Rotate - Dim/brighten the light that’s selected (not yet implemented)
Shake - Turn off all lights.
I’ve also created a TTS function to announce which light is currently selected through Google Home. I have it so it will set the volume to 50% (if it isn’t already) and then return to whatever the previous volume was.
It’s all working great, but I have two function nodes for the return volume section. I’m trying to understand stored variable nodes, but I can’t seem to wrap my head around the code.
Can someone with a little more experience help me streamline this a little more please?
THANK YOU!
Function node 1:
var volume = { payload: msg.data.attributes.volume_level };
return [volume];
Function node 2:
msg.payload =
{
“data”:{
“entity_id”:“media_player.living_room_speaker”,
“volume_level”: msg.payload
}
}
return msg;
Full code:
[{"id":"57d252ac.ecc8ac","type":"tab","label":"Xiaomi Cube","disabled":false,"info":""},{"id":"c1027b8e.5aaad8","type":"inject","z":"57d252ac.ecc8ac","name":"Flip 90","topic":"","payload":"flip90","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":300,"wires":[["7e0bf332.2d5bcc","f913aba3.22c688"]]},{"id":"47841701.e9fee8","type":"inject","z":"57d252ac.ecc8ac","name":"Tap Twice","topic":"","payload":"tap_twice","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":340,"wires":[["7e0bf332.2d5bcc"]]},{"id":"68a2ba0b.d05c54","type":"inject","z":"57d252ac.ecc8ac","name":"Rotate","topic":"","payload":"rotate","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":380,"wires":[["7e0bf332.2d5bcc"]]},{"id":"7e0bf332.2d5bcc","type":"switch","z":"57d252ac.ecc8ac","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"flip90","vt":"str"},{"t":"eq","v":"tap_twice","vt":"str"},{"t":"eq","v":"rotate","vt":"str"},{"t":"eq","v":"shake","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":330,"y":380,"wires":[["df5a8e61.946cf"],["223a0bcb.83bf14","606e9a9.c404b64","64e1caa6.3479c4","897b1e79.a0d6"],["7172102f.1d59e","c9348085.480de","68d84eb4.cd39d","594b61fb.1de12"],["4023fb56.157574"]]},{"id":"df5a8e61.946cf","type":"api-call-service","z":"57d252ac.ecc8ac","name":"Input Selection","server":"1b36a1cc.1e72ee","service_domain":"input_select","service":"select_next","data":"{ \"entity_id\" : \"input_select.room_selection\" }","mergecontext":"","x":560,"y":120,"wires":[[]]},{"id":"897b1e79.a0d6","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Family Room on?","server":"1b36a1cc.1e72ee","halt_if":"Family Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":570,"y":220,"wires":[["f57b7780.8817b8"]]},{"id":"f57b7780.8817b8","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Dining Room on?","server":"1b36a1cc.1e72ee","halt_if":"Dining Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":770,"y":220,"wires":[["fc052e39.28b76"]]},{"id":"fc052e39.28b76","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Kitchen on?","server":"1b36a1cc.1e72ee","halt_if":"Kitchen","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":950,"y":220,"wires":[["440d5e3e.e4387"]]},{"id":"f6cc8f59.de638","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Living Room on?","server":"1b36a1cc.1e72ee","halt_if":"Living Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":970,"y":340,"wires":[["f05be7c1.728818"]]},{"id":"b302164b.367a38","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Dining Room on?","server":"1b36a1cc.1e72ee","halt_if":"Dining Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":770,"y":340,"wires":[["f6cc8f59.de638"]]},{"id":"684e4d9d.feff74","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Dining Room on?","server":"1b36a1cc.1e72ee","halt_if":"Dining Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":770,"y":260,"wires":[["abe5da13.d6f5c8"]]},{"id":"abe5da13.d6f5c8","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Kitchen on?","server":"1b36a1cc.1e72ee","halt_if":"Kitchen","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":950,"y":260,"wires":[["4f28a800.84ea28"]]},{"id":"bb98ec2f.c27f8","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Kitchen on?","server":"1b36a1cc.1e72ee","halt_if":"Kitchen","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":950,"y":300,"wires":[["1619c786.30f138"]]},{"id":"223a0bcb.83bf14","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Living Room on?","server":"1b36a1cc.1e72ee","halt_if":"Living Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":570,"y":260,"wires":[["684e4d9d.feff74"]]},{"id":"606e9a9.c404b64","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Living Room on?","server":"1b36a1cc.1e72ee","halt_if":"Living Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":570,"y":300,"wires":[["54f6b0e8.8395b"]]},{"id":"54f6b0e8.8395b","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Family Room on?","server":"1b36a1cc.1e72ee","halt_if":"Family Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":770,"y":300,"wires":[["bb98ec2f.c27f8"]]},{"id":"64e1caa6.3479c4","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Family Room on?","server":"1b36a1cc.1e72ee","halt_if":"Family Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":570,"y":340,"wires":[["b302164b.367a38"]]},{"id":"cdd514ce.639278","type":"api-call-service","z":"57d252ac.ecc8ac","name":"TTS Kitchen","server":"1b36a1cc.1e72ee","service_domain":"tts","service":"google_say","data":"{\"entity_id\":\"media_player.living_room_speaker\", \"message\":\"Kitchen Selected\"}","mergecontext":"","x":1130,"y":940,"wires":[[]]},{"id":"1ec7f64a.9f41ea","type":"api-call-service","z":"57d252ac.ecc8ac","name":"TTS Family Room","server":"1b36a1cc.1e72ee","service_domain":"tts","service":"google_say","data":"{\"entity_id\":\"media_player.living_room_speaker\", \"message\":\"Family Room Selected\"}","mergecontext":"","x":1150,"y":860,"wires":[[]]},{"id":"7e23d838.5af7b8","type":"api-call-service","z":"57d252ac.ecc8ac","name":"TTS Dining Room","server":"1b36a1cc.1e72ee","service_domain":"tts","service":"google_say","data":"{\"entity_id\":\"media_player.living_room_speaker\", \"message\":\"Dining Room Selected\"}","mergecontext":"","x":1150,"y":900,"wires":[[]]},{"id":"73e949a8.68f508","type":"api-call-service","z":"57d252ac.ecc8ac","name":"TTS Living Room","server":"1b36a1cc.1e72ee","service_domain":"tts","service":"google_say","data":"{\"entity_id\":\"media_player.living_room_speaker\", \"message\":\"Living Room Selected\"}","mergecontext":"","x":1150,"y":820,"wires":[[]]},{"id":"4f28a800.84ea28","type":"api-call-service","z":"57d252ac.ecc8ac","name":"Family Room Light Toggle","server":"1b36a1cc.1e72ee","service_domain":"light","service":"toggle","data":"{ \"entity_id\" : \"light.family_room\" }","mergecontext":"","x":1230,"y":260,"wires":[["c43d1ebe.f5f35"]]},{"id":"ea2d9fed.94922","type":"server-state-changed","z":"57d252ac.ecc8ac","name":"TTS","server":"1b36a1cc.1e72ee","entityidfilter":"input_select.room_selection","entityidfiltertype":"substring","haltifstate":"","x":330,"y":840,"wires":[["d04457f4.6c9f58"]]},{"id":"f6f2892b.7bcb28","type":"switch","z":"57d252ac.ecc8ac","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Living Room","vt":"str"},{"t":"eq","v":"Family Room","vt":"str"},{"t":"eq","v":"Dining Room","vt":"str"},{"t":"eq","v":"Kitchen","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":930,"y":840,"wires":[["73e949a8.68f508"],["1ec7f64a.9f41ea"],["7e23d838.5af7b8"],["cdd514ce.639278"]]},{"id":"1614cb4f.08d575","type":"comment","z":"57d252ac.ecc8ac","name":"Light Toggle","info":"","x":550,"y":180,"wires":[]},{"id":"60eb0889.80ad88","type":"comment","z":"57d252ac.ecc8ac","name":"Light Dimming","info":"","x":550,"y":400,"wires":[]},{"id":"f05be7c1.728818","type":"api-call-service","z":"57d252ac.ecc8ac","name":"Kitchen Light Toggle","server":"1b36a1cc.1e72ee","service_domain":"light","service":"toggle","data":"{ \"entity_id\" : \"light.kitchen\" }","mergecontext":"","x":1240,"y":340,"wires":[["c43d1ebe.f5f35"]]},{"id":"1619c786.30f138","type":"api-call-service","z":"57d252ac.ecc8ac","name":"Dining Room Light Toggle","server":"1b36a1cc.1e72ee","service_domain":"light","service":"toggle","data":"{ \"entity_id\" : \"light.dining_room\" }","mergecontext":"","x":1230,"y":300,"wires":[["c43d1ebe.f5f35"]]},{"id":"440d5e3e.e4387","type":"api-call-service","z":"57d252ac.ecc8ac","name":"Living Room Light Toggle","server":"1b36a1cc.1e72ee","service_domain":"light","service":"toggle","data":"{ \"entity_id\" : \"light.living_room\" }","mergecontext":"","x":1230,"y":220,"wires":[["c43d1ebe.f5f35"]]},{"id":"7172102f.1d59e","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Family Room on?","server":"1b36a1cc.1e72ee","halt_if":"Family Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":570,"y":440,"wires":[["75028ae.de18d74"]]},{"id":"75028ae.de18d74","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Dining Room on?","server":"1b36a1cc.1e72ee","halt_if":"Dining Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":770,"y":440,"wires":[["ac81d5ab.fd5a48"]]},{"id":"ac81d5ab.fd5a48","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Kitchen on?","server":"1b36a1cc.1e72ee","halt_if":"Kitchen","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":950,"y":440,"wires":[["bb422442.46e978"]]},{"id":"b718bc1c.05147","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Living Room on?","server":"1b36a1cc.1e72ee","halt_if":"Living Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":970,"y":560,"wires":[["3f80fd9a.583ae2"]]},{"id":"2a1b8eba.94a0b2","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Dining Room on?","server":"1b36a1cc.1e72ee","halt_if":"Dining Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":770,"y":560,"wires":[["b718bc1c.05147"]]},{"id":"42747e6a.d1c58","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Dining Room on?","server":"1b36a1cc.1e72ee","halt_if":"Dining Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":770,"y":480,"wires":[["725c4c99.f177f4"]]},{"id":"725c4c99.f177f4","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Kitchen on?","server":"1b36a1cc.1e72ee","halt_if":"Kitchen","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":950,"y":480,"wires":[["e1f8508f.00857"]]},{"id":"8380b5a1.4276a8","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Kitchen on?","server":"1b36a1cc.1e72ee","halt_if":"Kitchen","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":950,"y":520,"wires":[["d967ad03.a97d5"]]},{"id":"c9348085.480de","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Living Room on?","server":"1b36a1cc.1e72ee","halt_if":"Living Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":570,"y":480,"wires":[["42747e6a.d1c58"]]},{"id":"68d84eb4.cd39d","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Living Room on?","server":"1b36a1cc.1e72ee","halt_if":"Living Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":570,"y":520,"wires":[["c43efe3d.f0eb9"]]},{"id":"c43efe3d.f0eb9","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Family Room on?","server":"1b36a1cc.1e72ee","halt_if":"Family Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":770,"y":520,"wires":[["8380b5a1.4276a8"]]},{"id":"594b61fb.1de12","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Family Room on?","server":"1b36a1cc.1e72ee","halt_if":"Family Room","override_topic":true,"override_payload":true,"entity_id":"input_select.room_selection","x":570,"y":560,"wires":[["2a1b8eba.94a0b2"]]},{"id":"e1f8508f.00857","type":"api-call-service","z":"57d252ac.ecc8ac","name":"Family Room Light Dimming","server":"1b36a1cc.1e72ee","service_domain":"light","service":"toggle","data":"","mergecontext":"","x":1220,"y":480,"wires":[[]]},{"id":"3f80fd9a.583ae2","type":"api-call-service","z":"57d252ac.ecc8ac","name":"Kitchen Light Dimming","server":"1b36a1cc.1e72ee","service_domain":"light","service":"toggle","data":"","mergecontext":"","x":1240,"y":560,"wires":[[]]},{"id":"d967ad03.a97d5","type":"api-call-service","z":"57d252ac.ecc8ac","name":"Dining Room Light Dimming","server":"1b36a1cc.1e72ee","service_domain":"light","service":"toggle","data":"","mergecontext":"","x":1220,"y":520,"wires":[[]]},{"id":"bb422442.46e978","type":"api-call-service","z":"57d252ac.ecc8ac","name":"Living Room Light Dimming","server":"1b36a1cc.1e72ee","service_domain":"light","service":"toggle","data":"","mergecontext":"","x":1220,"y":440,"wires":[[]]},{"id":"c43d1ebe.f5f35","type":"debug","z":"57d252ac.ecc8ac","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1510,"y":280,"wires":[]},{"id":"be88fab2.994e38","type":"server-state-changed","z":"57d252ac.ecc8ac","name":"Xiaomi Cube","server":"1b36a1cc.1e72ee","entityidfilter":"binary_sensor.xiaomicube1","entityidfiltertype":"substring","haltifstate":"","x":110,"y":240,"wires":[["7e0bf332.2d5bcc","f913aba3.22c688"]]},{"id":"4023fb56.157574","type":"api-call-service","z":"57d252ac.ecc8ac","name":"All off","server":"1b36a1cc.1e72ee","service_domain":"light","service":"turn_off","data":"{ \"entity_id\": [\"light.living_room\", \"light.family_room\", \"light.dining_room\", \"light.kitchen\" ] }","mergecontext":"","x":530,"y":640,"wires":[[]]},{"id":"f323c764.9fb568","type":"inject","z":"57d252ac.ecc8ac","name":"Shake","topic":"","payload":"shake","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":420,"wires":[["7e0bf332.2d5bcc"]]},{"id":"d04457f4.6c9f58","type":"traffic","z":"57d252ac.ecc8ac","name":"","property_allow":"payload","filter_allow":"on","ignore_case_allow":false,"negate_allow":false,"send_allow":false,"property_stop":"payload","filter_stop":"off","ignore_case_stop":false,"negate_stop":false,"send_stop":false,"default_start":false,"differ":false,"x":650,"y":840,"wires":[["f6f2892b.7bcb28","eab1ebab.653568","eaa2e437.e8e238"]]},{"id":"eab1ebab.653568","type":"stoptimer","z":"57d252ac.ecc8ac","duration":"5","units":"Second","payloadtype":"str","payloadval":"","name":"5 Sec","x":590,"y":940,"wires":[["b6bc170.73bfee8"],[]]},{"id":"b6bc170.73bfee8","type":"change","z":"57d252ac.ecc8ac","name":"Off","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":940,"wires":[["d04457f4.6c9f58"]]},{"id":"4597c763.682aa8","type":"api-call-service","z":"57d252ac.ecc8ac","name":"Set volume to 50%","server":"1b36a1cc.1e72ee","service_domain":"media_player","service":"volume_set","data":"{\"entity_id\": \"media_player.living_room_speaker\", \"volume_level\": 0.5 }","mergecontext":"","x":1530,"y":720,"wires":[[]]},{"id":"cd27457f.5dc038","type":"switch","z":"57d252ac.ecc8ac","name":"50% ?","property":"data.attributes.volume_level","propertyType":"msg","rules":[{"t":"neq","v":"0.5","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":930,"y":720,"wires":[["4597c763.682aa8","4ee734cd.ce092c"]]},{"id":"94c74a11.889608","type":"delay","z":"57d252ac.ecc8ac","name":"","pauseType":"delay","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1320,"y":760,"wires":[["efaec75b.3b2478"]]},{"id":"efaec75b.3b2478","type":"api-call-service","z":"57d252ac.ecc8ac","name":"Set volume to Previous","server":"1b36a1cc.1e72ee","service_domain":"media_player","service":"volume_set","data":"{}","mergecontext":"","x":1510,"y":760,"wires":[[]]},{"id":"4ee734cd.ce092c","type":"function","z":"57d252ac.ecc8ac","name":"","func":"var volume = { payload: msg.data.attributes.volume_level };\nreturn [volume];","outputs":1,"noerr":0,"x":1070,"y":760,"wires":[["5fd839fc.562a48"]]},{"id":"5fd839fc.562a48","type":"function","z":"57d252ac.ecc8ac","name":"","func":"msg.payload =\n{\n \"data\":{\n \"entity_id\":\"media_player.living_room_speaker\",\n \"volume_level\": msg.payload\n }\n}\nreturn msg;","outputs":1,"noerr":0,"x":1190,"y":760,"wires":[["94c74a11.889608"]]},{"id":"eaa2e437.e8e238","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Volume","server":"1b36a1cc.1e72ee","halt_if":"","override_topic":true,"override_payload":true,"entity_id":"media_player.living_room_speaker","x":800,"y":720,"wires":[["cd27457f.5dc038"]]},{"id":"dc7f8113.d62c8","type":"function","z":"57d252ac.ecc8ac","name":"","func":"var newMsg = { payload: msg.data.attributes.volume_level };\nmsg.payload =\n{ \"data\":{\n \"entity_id\":\"media_player.living_room_speaker\",\n \"volume_level\": newMsg\n }\n}\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":1160,"wires":[[]]},{"id":"f913aba3.22c688","type":"api-current-state","z":"57d252ac.ecc8ac","name":"Playing already?","server":"1b36a1cc.1e72ee","halt_if":"playing","override_topic":true,"override_payload":true,"entity_id":"media_player.living_room_speaker","x":330,"y":740,"wires":[["9756293.cd8b2d8"]]},{"id":"9756293.cd8b2d8","type":"change","z":"57d252ac.ecc8ac","name":" ","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":740,"wires":[["d04457f4.6c9f58"]]}]