[Home Assistant] Improve NodeRed Flow

Hi! I want to ask your assistance on how to further improve the flow as seen below.
If possible, for the part that asks the day, it will have one input with 7 outputs so that I can save space and copy only one node. TIA!

[{"id":"6028eff4.99415","type":"tab","label":"Broadlink Light Timers","disabled":false,"info":""},{"id":"5429e45.fa4cd1c","type":"function","z":"6028eff4.99415","name":"Time Now Vs Time On Set","func":"const states = global.get('homeassistant').homeAssistant.states;\nconst now = new Date();\nconst alarm = states['input_datetime.gf_sala_switch_broadlink_salasidelighton'].attributes;\n\nif(now.getHours() === alarm.hour && now.getMinutes() === alarm.minute) {\n    return msg;\n}","outputs":1,"noerr":0,"x":1200,"y":160,"wires":[["aca5d374.45605"]]},{"id":"aca5d374.45605","type":"api-call-service","z":"6028eff4.99415","name":"Turn On Sidelight","server":"37230eeb.6f5a62","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.gf_sala_switch_broadlink_salasidelight","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1430,"y":180,"wires":[[]]},{"id":"bc91d1b4.f8c8d","type":"api-current-state","z":"6028eff4.99415","name":"Is turn on timer enabled?","server":"37230eeb.6f5a62","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"input_boolean.gf_sala_switch_broadlink_salasidelight_turnontimer","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":950,"y":160,"wires":[["5429e45.fa4cd1c"],[]]},{"id":"24eb6ca5.034994","type":"api-current-state","z":"6028eff4.99415","name":"Will I turn it on Mon?","server":"37230eeb.6f5a62","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"input_boolean.gf_sala_switch_broadlink_salasidelight_monday","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":637,"y":100,"wires":[["bcff05d6.2f0d78","bc91d1b4.f8c8d"],[]]},{"id":"b3836258.3fa38","type":"api-current-state","z":"6028eff4.99415","name":"Will I turn it on Tue?","server":"37230eeb.6f5a62","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"input_boolean.gf_sala_switch_broadlink_salasidelight_tuesday","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":631,"y":154,"wires":[["bcff05d6.2f0d78","bc91d1b4.f8c8d"],[]]},{"id":"9eaaba13.e75438","type":"api-current-state","z":"6028eff4.99415","name":"Will I turn it on Wed?","server":"37230eeb.6f5a62","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"input_boolean.gf_sala_switch_broadlink_salasidelight_wednesday","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":641,"y":206,"wires":[["bcff05d6.2f0d78","bc91d1b4.f8c8d"],[]]},{"id":"1535c871.22d738","type":"api-current-state","z":"6028eff4.99415","name":"Will I turn it on Thu?","server":"37230eeb.6f5a62","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"input_boolean.gf_sala_switch_broadlink_salasidelight_thursday","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":641,"y":259,"wires":[["bcff05d6.2f0d78","bc91d1b4.f8c8d"],[]]},{"id":"a8c5d03.1c48f3","type":"api-current-state","z":"6028eff4.99415","name":"Will I turn it on Fri?","server":"37230eeb.6f5a62","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"input_boolean.gf_sala_switch_broadlink_salasidelight_friday","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":631,"y":312,"wires":[["bcff05d6.2f0d78","bc91d1b4.f8c8d"],[]]},{"id":"197d63d.5b0149c","type":"api-current-state","z":"6028eff4.99415","name":"Will I turn it on Sat?","server":"37230eeb.6f5a62","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"input_boolean.gf_sala_switch_broadlink_salasidelight_saturday","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":631,"y":365,"wires":[["bcff05d6.2f0d78","bc91d1b4.f8c8d"],[]]},{"id":"579efeb9.b3411","type":"api-current-state","z":"6028eff4.99415","name":"Will I turn it on Sun?","server":"37230eeb.6f5a62","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"input_boolean.gf_sala_switch_broadlink_salasidelight_sunday","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":641,"y":417,"wires":[["bcff05d6.2f0d78","bc91d1b4.f8c8d"],[]]},{"id":"bcff05d6.2f0d78","type":"api-current-state","z":"6028eff4.99415","name":"Is turn off timer enabled?","server":"37230eeb.6f5a62","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"input_boolean.gf_sala_switch_broadlink_salasidelight_turnofftimer","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":950,"y":300,"wires":[["89fde4c5.aed678"],[]]},{"id":"89fde4c5.aed678","type":"function","z":"6028eff4.99415","name":"Time Now Vs Time Off Set","func":"const states = global.get('homeassistant').homeAssistant.states;\nconst now = new Date();\nconst alarm = states['input_datetime.gf_sala_switch_broadlink_salasidelightoff'].attributes;\n\nif(now.getHours() === alarm.hour && now.getMinutes() === alarm.minute) {\n    return msg;\n}","outputs":1,"noerr":0,"x":1200,"y":300,"wires":[["e18807a9.347028"]]},{"id":"e18807a9.347028","type":"api-call-service","z":"6028eff4.99415","name":"Turn Off Sidelight","server":"37230eeb.6f5a62","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.gf_sala_switch_broadlink_salasidelight","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1430,"y":260,"wires":[[]]},{"id":"adc8923b.43114","type":"function","z":"6028eff4.99415","name":"Checks if it is Sun","func":"var day = new Date().getDay();\n\nvar isSunday = (day === 0) \nvar isMonday = (day === 1)\nvar isTuesday = (day === 2)\nvar isWednesday = (day === 3)\nvar isThursday = (day === 4)\nvar isFriday = (day === 5)\nvar isSaturday = (day === 6)\n\nreturn {\n    payload: isSunday,\n};","outputs":1,"noerr":0,"x":267,"y":420,"wires":[["98c559a7.781ec8"]]},{"id":"40eca676.467ef8","type":"function","z":"6028eff4.99415","name":"Checks if it is Sat","func":"var day = new Date().getDay();\n\nvar isSunday = (day === 0) \nvar isMonday = (day === 1)\nvar isTuesday = (day === 2)\nvar isWednesday = (day === 3)\nvar isThursday = (day === 4)\nvar isFriday = (day === 5)\nvar isSaturday = (day === 6)\n\nreturn {\n    payload: isSaturday,\n};","outputs":1,"noerr":0,"x":267,"y":368,"wires":[["9224d6f9.75fba8"]]},{"id":"dc2abc62.41d29","type":"function","z":"6028eff4.99415","name":"Checks if it is Fri","func":"var day = new Date().getDay();\n\nvar isSunday = (day === 0) \nvar isMonday = (day === 1)\nvar isTuesday = (day === 2)\nvar isWednesday = (day === 3)\nvar isThursday = (day === 4)\nvar isFriday = (day === 5)\nvar isSaturday = (day === 6)\n\nreturn {\n    payload: isFriday,\n};","outputs":1,"noerr":0,"x":257,"y":318,"wires":[["d969d8cc.9340c8"]]},{"id":"b33f5860.3ed8d8","type":"function","z":"6028eff4.99415","name":"Checks if it is Thu","func":"var day = new Date().getDay();\n\nvar isSunday = (day === 0) \nvar isMonday = (day === 1)\nvar isTuesday = (day === 2)\nvar isWednesday = (day === 3)\nvar isThursday = (day === 4)\nvar isFriday = (day === 5)\nvar isSaturday = (day === 6)\n\nreturn {\n    payload: isThursday,\n};","outputs":1,"noerr":0,"x":267,"y":265,"wires":[["1535c871.22d738"]]},{"id":"7adecd2.68d0a34","type":"function","z":"6028eff4.99415","name":"Checks if it is Wed","func":"var day = new Date().getDay();\n\nvar isSunday = (day === 0) \nvar isMonday = (day === 1)\nvar isTuesday = (day === 2)\nvar isWednesday = (day === 3)\nvar isThursday = (day === 4)\nvar isFriday = (day === 5)\nvar isSaturday = (day === 6)\n\nreturn {\n    payload: isWednesday,\n};","outputs":1,"noerr":0,"x":267,"y":210,"wires":[["8896bb2c.cc8828"]]},{"id":"723dd18d.ec40c","type":"function","z":"6028eff4.99415","name":"Checks if it is Tue","func":"var day = new Date().getDay();\n\nvar isSunday = (day === 0) \nvar isMonday = (day === 1)\nvar isTuesday = (day === 2)\nvar isWednesday = (day === 3)\nvar isThursday = (day === 4)\nvar isFriday = (day === 5)\nvar isSaturday = (day === 6)\n\nreturn {\n    payload: isTuesday,\n};","outputs":1,"noerr":0,"x":267,"y":160,"wires":[["36dd5d65.45da12"]]},{"id":"bdbe5f90.9ef1d","type":"function","z":"6028eff4.99415","name":"Checks if it is Mon","func":"var day = new Date().getDay();\n\nvar isSunday = (day === 0) \nvar isMonday = (day === 1)\nvar isTuesday = (day === 2)\nvar isWednesday = (day === 3)\nvar isThursday = (day === 4)\nvar isFriday = (day === 5)\nvar isSaturday = (day === 6)\n\nreturn {\n    payload: isMonday,\n};","outputs":1,"noerr":0,"x":267,"y":100,"wires":[["2861666e.0af24a"]]},{"id":"dd17d5d7.024d78","type":"trigger-state","z":"6028eff4.99415","name":"Time Now","server":"37230eeb.6f5a62","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.time","entityidfiltertype":"substring","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":80,"y":240,"wires":[["bdbe5f90.9ef1d","723dd18d.ec40c","7adecd2.68d0a34","b33f5860.3ed8d8","dc2abc62.41d29","40eca676.467ef8","adc8923b.43114"],[]]},{"id":"2861666e.0af24a","type":"switch","z":"6028eff4.99415","name":"Is it Mon?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":437,"y":100,"wires":[["24eb6ca5.034994"],[]]},{"id":"36dd5d65.45da12","type":"switch","z":"6028eff4.99415","name":"Is it Tue?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":437,"y":160,"wires":[["b3836258.3fa38"],[]]},{"id":"8896bb2c.cc8828","type":"switch","z":"6028eff4.99415","name":"Is it Wed?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":437,"y":209,"wires":[["9eaaba13.e75438"],[]]},{"id":"37c96913.cdd746","type":"switch","z":"6028eff4.99415","name":"Is it Thu?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":437,"y":260,"wires":[[],[]]},{"id":"d969d8cc.9340c8","type":"switch","z":"6028eff4.99415","name":"Is it Fri?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":437,"y":316,"wires":[["a8c5d03.1c48f3"],[]]},{"id":"9224d6f9.75fba8","type":"switch","z":"6028eff4.99415","name":"Is it Sat?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":437,"y":360,"wires":[["197d63d.5b0149c"],[]]},{"id":"98c559a7.781ec8","type":"switch","z":"6028eff4.99415","name":"Is it Sun?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":437,"y":420,"wires":[["579efeb9.b3411"],[]]},{"id":"37230eeb.6f5a62","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

What type are the blue nodes? Can you link to it so we know what you're working with?

I am using this sample to turn on or off the Tuya device based on the time that I set. Instead of just choosing between weekdays and weekends, I prefer to choose between the 7 days of the week.

Sample Automation

See below comment for the name of the nodes:

@celeritas0725 what module do those nodes come from? They look like Home Assistant nodes, but it would help if you could confirm exactly what module you installed?

Those were from Home Assistant:

image

Nick, my guess is that this is that type of HA install that comes with preinstalled Node-RED with HA nodes. It appears to be near impossible for users of it to find which set of nodes they are, nor the version number of it. As it comes preinstalled they never had to know which of the types it was, and updating to a newer version of HA brings updates to the nodes/node-red too, which means the version number of HA itself is often used.

@celeritas0725 on the right side in the sidebar, on the info tab, what does it say when you click one of the blue nodes in your flow? Does it show something like node-red-contrib-home-assistant or node-red-contrib-home-assistant-websocket somewhere?

It says "node-red-contrib-home-assistant-websocket".
Apologies, my intentions might not be clear. This is working, I just want to maximize the space by making it simple with less nodes, if possible.

The idea is that there’s 2 sets of HA nodes in the flows library, that have the same looking nodes in it with the same name, but differences in interface and how they work. So if you install the wrong set and import your flow to see the configuration it might fail or come up with incorrect info. If you use the node-red integration in HA you get one of those libraries preinstalled, so you don’t have to be aware which you use. It’s simply there from the start.
Quite a few people answering here don’t use HA, or don’t always use it via the integration, which is why it matters which you have and use.

1 Like

So, putting aside all of the HA discussion, your original question was how to simplify the flow.

At the moment you have a lot of nodes doing the work to see what day it is and to run the appropriate branch of your flow. All of your Function/Switch nodes could be replaced by a single Function node:

[{"id":"6478f40c.bc3c1c","type":"function","z":"4356c935.910e68","name":"","func":"var response = [null,null,null,null,null,null,null]\n\n// getDay returns 0 for sunday. Do a bit of maths so 0 is Monday\nvar day = (new Date().getDay()+6)%7;\n\nresponse[day] = msg;\n\nreturn response","outputs":7,"noerr":0,"x":370,"y":540,"wires":[[],[],[],[],[],[],[]]}]

This node has 7 outputs, one for each day of the week, and will pass a message to the corresponding output.

3 Likes

This is the exact look that I am looking for. However, it is not working when I inserted it in the flow. No output on the debug message after the node with text "Is it Wed?".

The Function node I provided replaces all of the Function and Switch nodes - so you shouldn't have a node labeled Is it Wed? any more:

EDIT: Nick beat me to it

@knolleary's functon (elegant coding Nick!) replaces all your function and switch nodes. i.e. it will only return an output on the leg for that day of the week.

For example, if today is Wednesday then the only output will be out the third output of the function node.

In addition the msg coming out will be the msg that came into the function node

Thank you knolleary! This is to confirm that the flow is working and at the same time, neat. :blush:

Thank you for the explanation zenofmud.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.