I’m trying to create a single flow that controls my front-yard WLED string lights based on different things like month of the year, time of day, which NFL team is currently playing, if we’re home or not. Any recommendations on best way to do this in Node Red? I’m thinking a trigger every 30s that runs a Function node that acts as a large ‘if’ statement checking all my various scenarios which then outputs the relevant wled preset name I can then service_call on. I currently have a dozen+ state-triggered flows and its getting too difficult to handle the overlap and falling back to ‘lower priority’ wled sets…
Some examples of my scenarios:
If its after sundown and before 1030pm in October on a school night, turn on my ‘halloween’ wled preset.
If its after sundown and before 1130pm in December on a weekend night, turn on my ‘christmas’ wled preset.
If my NFL team is playing, override everything else and put on my team’s colors. When my team stops playing, revert back to whatever
If my spouse’s NFL team and my team are both playing, override everything else and put on the "both teams’ colors’ wled preset.
etc etc.
I could make a really long ‘check status’ after ‘check status’ flow with higher ‘priority’ things like NFL colors up early, but that seems complicated to build and maintain when I could conceivable just make a single Function node…
Recommendations?
Of course, if a Function node is ideal, now I gotta learn how to write a function node…
It creates a sensor.eagles (or sensors.ravens for my spouse) that rotates through a few set values like “IN” (currently playing), “PRE” (playing later in the week), “POST” (already played this week), “BYE” (on bye week), or “NOT_FOUND” (out of season/season over).
So something like:
IF(eagles == “IN” and ravens != “IN”), then turn on Eagles wled preset
ELSEIF (eagles and ravens are both != “IN”, after sunset, before 1030pm on a weeknight in October), then turn on Halloween wled preset
ELSEIF (sun is up or after 1130pm on a weekend night), then turn off wled controller
ELSE, then turn off WLED.
Obviously I’d have to put more work into making sure the IF statements are comprehensive, but hopefully that gets the point across.
Bottom line: IF/ELSEIF-ing off of multiple sensors with different data types…
A few thoughts… One, possibly check out the State Machine node. I’ve not used it, but it might be helpful for what you’re trying to do.
Two, it might make organization, testing, and maintenance easier if you separate out the “determine state” logic from the “control lights” step. I.e., make a Dropdown (Input Select) Helper with all your presets, then change your logic to set the value of the Input Select (e.g., "If its after sundown and before 1030pm in October on a school night, set InputSelect to “halloween”.) Then, separately, a much simpler Switch node to send the appropriate value to the light string based on the current value of the Input Select (probably triggered when the value of the Input Select changes.)
Finally, since this is inherently complex logic with lots of overlaps and nuances, whatever tools or methods you use, make sure they are ones you are comfortable with. Maintenance is going to be the biggest part of this, so “best” or “most efficient” is totally dependent on how your mind works and what “makes sense” to you.