Node-Red help for beginner

I am trying to teach myself Node-Red automations…:slight_smile: starting with a simple one. Turns on lights at Sunset, turns them off at 11PM.

This is my flow :slight_smile:

The issue is that any time after sunset, the state refreshes itself and turns the lights back on.

In a related issue, a separate automation turns the fan on when the temperature is over 80F. And off if it falls below. I got this to work, except that if i decide to turn the fan off with the switch, after a few minutes it refreshes the state and turns the fan back on…

This is the flow:

I am missing something basic about how loops work in Node-Red and how to stop a loop from recurring…Can someone point me in the right direction ?

Thanks in advance !

Maybe i figured the first problem using eztimer…

Eztimer node sends out a True after Sunset and a False at 11PM. The switch changes that to 2 separate outputs one to turn on the lights the second to turn off. I have not tested. Does it sound like it could work ? Thanks for any help !

1 Like

I’m still learning as well but I have an alternative solution for your first question using the big timer node:

[{"id":"4df685db.b408fc","type":"subflow","name":"Security Lights On","info":"","category":"","in":[{"x":20,"y":80,"wires":[{"id":"db877ee7.4b219"},{"id":"6331b553.35c13c"}]}],"out":[{"x":400,"y":60,"wires":[{"id":"db877ee7.4b219","port":0},{"id":"6331b553.35c13c","port":0}]}],"env":[],"status":{"x":400,"y":160,"wires":[{"id":"db877ee7.4b219","port":0},{"id":"6331b553.35c13c","port":0}]}},{"id":"db877ee7.4b219","type":"api-call-service","z":"4df685db.b408fc","name":"Coach lights on (25%)","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_on","entityId":"light.coach","data":"{\"transition\":3,\"brightness_pct\":25}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":200,"y":80,"wires":[[]]},{"id":"6331b553.35c13c","type":"api-call-service","z":"4df685db.b408fc","name":"Lanai lights on (10%)","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_on","entityId":"light.lanai","data":"{\"transition\":3,\"brightness_pct\":10}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":200,"y":140,"wires":[[]]},{"id":"583b5c28.942304","type":"subflow","name":"Lanai, Coach, Entryway lights off?","info":"","category":"","in":[{"x":240,"y":240,"wires":[{"id":"126cbf5f.894661"},{"id":"e51a6444.ab76a8"},{"id":"b1150990.a46b88"}]}],"out":[{"x":640,"y":240,"wires":[{"id":"e51a6444.ab76a8","port":1},{"id":"126cbf5f.894661","port":1},{"id":"b1150990.a46b88","port":1}]}],"env":[]},{"id":"e51a6444.ab76a8","type":"api-current-state","z":"583b5c28.942304","name":"Coach lights off?","server":"26956a45.92d866","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.coach","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":450,"y":180,"wires":[[],[]]},{"id":"126cbf5f.894661","type":"api-current-state","z":"583b5c28.942304","name":"Lanai lights off?","server":"26956a45.92d866","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lanai","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":440,"y":220,"wires":[[],[]]},{"id":"b1150990.a46b88","type":"api-current-state","z":"583b5c28.942304","name":"Entryway lights off?","server":"26956a45.92d866","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.entryway","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":450,"y":260,"wires":[[],[]]},{"id":"6d01bd69.eb5cf4","type":"switch","z":"fdebb431.1a00e8","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":270,"y":120,"wires":[["52506023.4d3b2"],["d5d2f818.742148","5f2e4717.fe8078","713917ae.0b1918"]]},{"id":"d5d2f818.742148","type":"api-current-state","z":"fdebb431.1a00e8","name":"Lanai lights on?","server":"26956a45.92d866","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lanai","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":420,"y":180,"wires":[["8579f348.d48ac"],[]]},{"id":"5f2e4717.fe8078","type":"api-current-state","z":"fdebb431.1a00e8","name":"Coach lights on?","server":"26956a45.92d866","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.coach","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":430,"y":240,"wires":[["73361147.5bc23"],[]]},{"id":"512a706b.d48e2","type":"api-call-service","z":"fdebb431.1a00e8","name":"Lights off","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_off","entityId":"light.lanai, light.coach, light.entryway, light.dining","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":860,"y":300,"wires":[["91cae457.31d5c8"]]},{"id":"da4fc58.6a54638","type":"bigtimer","z":"fdebb431.1a00e8","outtopic":"","outpayload1":"","outpayload2":"","name":"Sunrise - Sunset","comment":"","lat":"27.918805","lon":"-81.866830","starttime":"5004","endtime":"5003","starttime2":0,"endtime2":0,"startoff":0,"endoff":0,"startoff2":0,"endoff2":0,"offs":0,"outtext1":"on","outtext2":"off","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"day6":0,"month6":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"d6":0,"w6":0,"xday1":0,"xmonth1":0,"xday2":0,"xmonth2":0,"xday3":0,"xmonth3":0,"xday4":0,"xmonth4":0,"xday5":0,"xmonth5":0,"xday6":0,"xmonth6":0,"xd1":0,"xw1":0,"xd2":0,"xw2":0,"xd3":0,"xw3":0,"xd4":0,"xw4":0,"xd5":0,"xw5":0,"xd6":0,"xw6":0,"suspend":false,"random":false,"repeat":true,"atstart":false,"odd":false,"even":false,"x":110,"y":80,"wires":[[],[],["6d01bd69.eb5cf4"]]},{"id":"b2decf80.da593","type":"inject","z":"fdebb431.1a00e8","name":"","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":140,"wires":[["6d01bd69.eb5cf4"]]},{"id":"8579f348.d48ac","type":"api-call-service","z":"fdebb431.1a00e8","name":"Lanai Lights on (100%)","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_on","entityId":"light.lanai","data":"{\"brightness_pct\":100}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":630,"y":180,"wires":[["512a706b.d48e2"]]},{"id":"73361147.5bc23","type":"api-call-service","z":"fdebb431.1a00e8","name":"Coach Lights on (100%)","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_on","entityId":"light.coach","data":"{\"brightness_pct\":100}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":650,"y":240,"wires":[["512a706b.d48e2"]]},{"id":"f8d9e5e.8768d18","type":"inject","z":"fdebb431.1a00e8","name":"","topic":"","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":180,"wires":[["6d01bd69.eb5cf4"]]},{"id":"713917ae.0b1918","type":"api-call-service","z":"fdebb431.1a00e8","name":"Entryway Lights on (100%)","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_on","entityId":"light.entryway","data":"{\"brightness_pct\":100}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":460,"y":300,"wires":[["512a706b.d48e2"]]},{"id":"5bb2ac7f.f822b4","type":"comment","z":"fdebb431.1a00e8","name":"At sunrise, set brightness to 100% and turn everything off.","info":"","x":550,"y":140,"wires":[]},{"id":"c0803698.46ae78","type":"comment","z":"fdebb431.1a00e8","name":"Turn on the Coach and Lanai lights at sunset, and set brightness to 25%.","info":"","x":600,"y":40,"wires":[]},{"id":"91cae457.31d5c8","type":"delay","z":"fdebb431.1a00e8","name":"1sec","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":990,"y":300,"wires":[["58fb4338.5712ac"]]},{"id":"58fb4338.5712ac","type":"subflow:583b5c28.942304","z":"fdebb431.1a00e8","name":"","env":[],"x":1220,"y":300,"wires":[["8bc5c11f.06689"]]},{"id":"8bc5c11f.06689","type":"api-call-service","z":"fdebb431.1a00e8","name":"Lights off","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_off","entityId":"light.lanai, light.coach, light.entryway, light.dining","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1440,"y":300,"wires":[["58fb4338.5712ac"]]},{"id":"8dd4e4dc.03cfc8","type":"api-current-state","z":"fdebb431.1a00e8","name":"Security lights on?","server":"26956a45.92d866","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.entryway","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":770,"y":80,"wires":[[],["570e86f5.22abd8"]]},{"id":"52506023.4d3b2","type":"subflow:4df685db.b408fc","z":"fdebb431.1a00e8","name":"Security Lights On","env":[],"x":450,"y":80,"wires":[["a61291cc.88e85"]]},{"id":"570e86f5.22abd8","type":"subflow:4df685db.b408fc","z":"fdebb431.1a00e8","name":"Security Lights On","env":[],"x":970,"y":80,"wires":[[]]},{"id":"a61291cc.88e85","type":"delay","z":"fdebb431.1a00e8","name":"1sec","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":610,"y":80,"wires":[["8dd4e4dc.03cfc8"]]},{"id":"26956a45.92d866","type":"server","z":"","name":"Hass.io","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

To your second question, and I know there’s a better way to do this but a “current state” node will help. It’s basically; “is this already on? Yes - then do this. No - then do this”.

There are three of them in the flow I posted above, they should be able to show you how to use them.

I misread your second question. In your “over 80?” state node, is “Output only on state change” checked or unchecked?

Here is an example flow for some of my lights. The bigtimer is a very flexible node.

All my lighting that is on timers:

Most of the timed lighting happens based on sunset/sunrise, and there is a random before/after that is added in with the schedex nodes.

Thanks @PickleSlice. On the Big timer, I see you are using Output 3. I am not clear on how to use Output 1 vs. Output 3.

As per the help : “The first output sends out a message at your set ON or OFF times (and if the repeat tickbox is ticked - every minute)”. My question is what message ? Is it whatever is in these boxes in the config ? (i put in ON and OFF)
image .

In the help : “The third output sends an optional message for ON and OFF both states - suitable for speech output or a log (this happens only at ON or OFF times, NOT every minute).” Where does one put this optional “message” ? Is it here : ? (again, I put in the ON and OFF)

image .

If that is the case, then is it the same using Output 1 and the ON Msg boxes or Output 3 and the ON Text boxes ?

:slight_smile:

Oh, and yes, in Over 80 the Output only on state change is checked. The behavior seems to be that sometimes the temperature is measured at 81 then goes back to 80. that is a state change and i believe it will register as a new trigger event ?. The automation starts and turns on the fans. But what if i had switched them off manually because i left the room and do not intend to come back there until tomorrow and won’t need the fans there anymore ?

1 Like

Do you have a motion sensor in there?

Do you use this room in a way that presence would keep the motion sensor active?

No, and not practical given the size/shape of the room. I was reading about certain nodes that can store information in NodeRed and can be used to remember states of entities. The idea would be that when i manually switch the fans off, that information gets stored somewhere and stops the automation from triggering until the next day for example. Now as to how to implement that i have no clue since I only started playing with NodeRed this week end :slight_smile:

1 Like

I haven’t done it yet, but I believe what you’re talking about is a Function node that can save itself, although I’m not totally sure.

Flow or Global variables in Node-Red

You could also work with a Traffic Light in this case. If you turn off then you send a message to the traffic light which stops all other messages to go through.
You can then reset this traffic light every morning again.

Otherwise, you can also do this via a function note by saving a variable. You can also save this to a file on a disk so it survives restarts :slight_smile:

Let me know if you still need help with this. I should have some examples in my nodered config.