Light Automation Based on Alarm State

I’ve been toying with my Exterior Lighting automation and trying to get tricky with presence detection, etc to keep them from going off while I’m entertaining, etc.

Came to the conclusion (after adding the Envisalink to my alarm), that I always set the alarm to “stay” before I go to bed, and that is a great trigger to turn off specific lights (internal and external).

However…what happens if I’m away and I don’t set the alarm to stay? How can I make the following flow work when the alarm is set to “away” (and possibly for days at a time)? In that case I would like them to then adhere to a schedule and turn off at a set time, for example 23:30.

I guess a simpler question might be “how can I make BigTimer or some other node only take effect when the alarm is set to ‘away’ mode?”

I think I have similar. I set a state of “LATEEVENING” at 22:00 each night. At 23:00 it is changed to night…unless the alarm has not been set.

So in this case the alarm is the night time trigger for your lights (I do this as well, Also do it on disarm if it’s night time , ie coming home, getting up In the morning)

But if you’re away then the night time state is set at 22:00 as the alarm is already on.

I am moving some of these functions from openHAB where they are rules, into node red. That main time decision where it’s night or lateevening is the first rule I attempted

Can you share how you have this override in Node Red?

So top left these are all the HA elements that set base times. Eg Morning_time_on = 5am, Morning Time Off = 8am, Coffee Machine time off 6pm etc etc Heating Time off = 22:00 and lamp lighting time off = 23:00 (they’re lateevening and night). They get fed into flow variables so they’re independent from the rest of the flow.

The actual figuring out the time of day is triggered by a timestamp each minute that pulls HHmm from the time, and goes into a function. I did have a switch node, but I didn’t find it flexible enough as for me, but I think it would work for your lighting as that part of it was fine. It was just between statements, eg if msg_payload between morning_time_end and coffee_machine_time_off then into a change node for Set DAY.

But the bit you care about, it out of the Time Decision function, I take the LATEEVENING and NIGHT anchors, and get the current alarm state, and then if armed set NIGHT, otherwise set LATEEVENING.

Sorry, I was fiddling with this last night, as I needed to change the afternoon aspect as I want the coffee machine off at 6pm but I have a sunset - 1 hour time as well for some lights, and in summer, sunset is after 6pm so I needed to be able to switch it round somewhat.

Node red is interesting…but I find it a bit convoluted to do something that might be 10 - 15 lines of java code in openhab…and tbh a little harder to debug as well…but it’s fun to play with.

Here’s that time decision node in the top right, which I think you could use:

That’s very impressive. Do you think it would be possible to share the flow? I would love to play with that without recreating the wheel lol :slight_smile:

[{"id":"af7a5936.5847c8","type":"tab","label":"Time Of Day","disabled":false,"info":""},{"id":"3ac1b9df.e32dd6","type":"inject","z":"af7a5936.5847c8","name":"","topic":"current_time","payload":"","payloadType":"date","repeat":"60","crontab":"","once":false,"onceDelay":0.1,"x":115,"y":892.2858457565308,"wires":[["b8089dad.0bb29"]]},{"id":"e00c2433.d063b8","type":"server-state-changed","z":"af7a5936.5847c8","name":"Coffee Machine Time Off","server":"48eefced.ce1b34","entityidfilter":"input_number.coffee_machine_time_off","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":121,"y":113,"wires":[["bff71fbd.f859b"]]},{"id":"c00b3dfd.30acb","type":"server-state-changed","z":"af7a5936.5847c8","name":"Lamp lighting time off","server":"48eefced.ce1b34","entityidfilter":"input_number.lamp_lighting_time_off","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":112,"y":197,"wires":[["bff71fbd.f859b"]]},{"id":"4006a3ab.c1046c","type":"server-state-changed","z":"af7a5936.5847c8","name":"Heating Time off","server":"48eefced.ce1b34","entityidfilter":"input_number.heating_time_off","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":94,"y":155,"wires":[["bff71fbd.f859b"]]},{"id":"a30c601d.127fa","type":"server-state-changed","z":"af7a5936.5847c8","name":"Morning Time End","server":"48eefced.ce1b34","entityidfilter":"input_number.morning_time_end","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":105,"y":71,"wires":[["bff71fbd.f859b"]]},{"id":"3b1b5153.26560e","type":"server-state-changed","z":"af7a5936.5847c8","name":"Morning Time Start","server":"48eefced.ce1b34","entityidfilter":"input_number.morning_time_start","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":103,"y":26,"wires":[["bff71fbd.f859b"]]},{"id":"bff71fbd.f859b","type":"function","z":"af7a5936.5847c8","name":"Save HA TImes to Flow variables.","func":"var time = 0;\nif (Number(msg.payload) < 25)\n    time = Number(msg.payload + \"00\");\nelse\n    time = Number(msg.payload);\n\n\nvar topic = msg.topic.split(\".\")[1];\nflow.set(msg.topic,msg.payload);\nflow.set(topic,time);\nnode.warn(topic + ' is set to ' + msg.payload + \" Full time set to \" + String(time)); \nreturn msg;","outputs":1,"noerr":0,"x":1034.3929290771484,"y":129.9285764694214,"wires":[["c2efe21a.2a18e"]]},{"id":"2e14383a.70a2f8","type":"server-state-changed","z":"af7a5936.5847c8","name":"Sunset Minus One","server":"48eefced.ce1b34","entityidfilter":"sensor.next_sunset","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":102,"y":252,"wires":[["6219dd86.5ba2f4"]]},{"id":"6219dd86.5ba2f4","type":"moment","z":"af7a5936.5847c8","name":"","topic":"sensor.next_sunset","input":"","inputType":"msg","inTz":"Pacific/Auckland","adjAmount":"1","adjType":"hours","adjDir":"subtract","format":"HHmm","locale":"POSIX","output":"payload","outputType":"msg","outTz":"Pacific/Auckland","x":577.8571472167969,"y":253.285737991333,"wires":[["bff71fbd.f859b"]]},{"id":"800cab20.6e24e8","type":"switch","z":"af7a5936.5847c8","name":"Time Decision","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"morning_time_start","vt":"flow","v2":"morning_time_end","v2t":"flow"},{"t":"btwn","v":"morning_time_end","vt":"flow","v2":"coffee_machine_time_off","v2t":"flow"},{"t":"btwn","v":"morning_time_end","vt":"num","v2":"sunset_minus_one","v2t":"num"},{"t":"btwn","v":"coffee_machine_time_off","vt":"flow","v2":"heating_time_off","v2t":"flow"},{"t":"btwn","v":"heating_time_off","vt":"num","v2":"2359","v2t":"num"},{"t":"btwn","v":"0","vt":"num","v2":"morning_time_start","v2t":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":7,"x":691,"y":682.821533203125,"wires":[["db5574c3.2610e8"],["b86ea1e0.87cbd"],["358d8a41.2bb4e6"],["ce87d82d.5bd618"],["c5abb238.cda2f"],["31263b3f.343414"],["31263b3f.343414"]]},{"id":"db5574c3.2610e8","type":"change","z":"af7a5936.5847c8","name":"Set MORNING","rules":[{"t":"set","p":"payload","pt":"msg","to":"MORNING","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":944.5356063842773,"y":414.1072359085083,"wires":[[]]},{"id":"b86ea1e0.87cbd","type":"change","z":"af7a5936.5847c8","name":"Set DAY","rules":[{"t":"set","p":"payload","pt":"msg","to":"DAY","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":945.4641990661621,"y":482.5715026855469,"wires":[[]]},{"id":"2054ebda.874944","type":"debug","z":"af7a5936.5847c8","name":"ToD State Switch","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1594.6431579589844,"y":901.3216342926025,"wires":[]},{"id":"b8089dad.0bb29","type":"moment","z":"af7a5936.5847c8","name":"Date Time HHmm","topic":"","input":"payload","inputType":"msg","inTz":"Pacific/Auckland","adjAmount":0,"adjType":"days","adjDir":"add","format":"HHmm","locale":"POSIX","output":"payload","outputType":"msg","outTz":"Pacific/Auckland","x":214.00001525878906,"y":779.7858543395996,"wires":[["8bc65567.ab5ef8"]]},{"id":"ce87d82d.5bd618","type":"change","z":"af7a5936.5847c8","name":"Set EVENING","rules":[{"t":"set","p":"payload","pt":"msg","to":"EVENING","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1011.2856063842773,"y":614.5358867645264,"wires":[[]]},{"id":"c5abb238.cda2f","type":"change","z":"af7a5936.5847c8","name":"Set LATEEVENING","rules":[{"t":"set","p":"payload","pt":"msg","to":"LATEEVENING","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1023.4999847412109,"y":668.7143878936768,"wires":[[]]},{"id":"7b75fce1.37b0a4","type":"change","z":"af7a5936.5847c8","name":"Set NIGHT","rules":[{"t":"set","p":"payload","pt":"msg","to":"NIGHT","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1413.6177673339844,"y":998.4089002609253,"wires":[["ef560763.b33a48"]]},{"id":"efa23829.631e18","type":"switch","z":"af7a5936.5847c8","name":"Alarm Night Modifier","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"armed_night","vt":"str"},{"t":"neq","v":"armed_night","vt":"str"},{"t":"eq","v":"armed_away","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":1143.5813598632812,"y":1001.6203165054321,"wires":[["7b75fce1.37b0a4"],["20060081.ad9bc"],["7b75fce1.37b0a4"]]},{"id":"31263b3f.343414","type":"change","z":"af7a5936.5847c8","name":"Set NIGHT","rules":[{"t":"set","p":"payload","pt":"msg","to":"NIGHT","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":974.9284591674805,"y":720.5358409881592,"wires":[[]]},{"id":"8af47ef6.84452","type":"api-current-state","z":"af7a5936.5847c8","name":"Get Alarm State","server":"48eefced.ce1b34","outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"is","override_topic":true,"entity_id":"alarm_control_panel.alarm","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":941.7143783569336,"y":1010.1430826187134,"wires":[["efa23829.631e18"]]},{"id":"ef560763.b33a48","type":"api-call-service","z":"af7a5936.5847c8","name":"Update Time of Day","server":"48eefced.ce1b34","service_domain":"input_select","service":"select_option","data":"{\"entity_id\":\"input_select.time_of_day\",\"option\":\"{{payload}}\"}","mergecontext":"","output_location":"payload","output_location_type":"msg","x":1508.0001907348633,"y":838.2858972549438,"wires":[["2054ebda.874944"]]},{"id":"358d8a41.2bb4e6","type":"change","z":"af7a5936.5847c8","name":"Set LATEAFTERNOON","rules":[{"t":"set","p":"payload","pt":"msg","to":"LATEAFTERNOON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1017.0193557739258,"y":549.4325122833252,"wires":[[]]},{"id":"c2efe21a.2a18e","type":"debug","z":"af7a5936.5847c8","name":"Current Time","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1277.01953125,"y":131.00390625,"wires":[]},{"id":"8bc65567.ab5ef8","type":"function","z":"af7a5936.5847c8","name":"Time Decision","func":"var morning_time_start = parseInt(flow.get(\"morning_time_start\"));\nvar morning_time_end =  parseInt(flow.get(\"morning_time_end\"));\nvar sunset_minus_one =  parseInt(flow.get(\"next_sunset\"));\nvar coffee_machine_time_off =  parseInt(flow.get(\"coffee_machine_time_off\"));\nvar heating_time_off =  parseInt(flow.get(\"heating_time_off\"));\nvar lamp_lighting_time_off =  parseInt(flow.get(\"lamp_lighting_time_off\"));\nvar mignight =  0;\n\nvar lateafternoon = coffee_machine_time_off;\nvar evening = sunset_minus_one;\n//dayloing savings 1800 < 2000\nif (coffee_machine_time_off < sunset_minus_one)\n{\n    evening = sunset_minus_one\n    lateafternoon = coffee_machine_time_off\n} \nelse if (coffee_machine_time_off > sunset_minus_one)\n{\n    evening = coffee_machine_time_off;\n}\n\n\nvar current_time =  parseInt(msg.payload);\nnode.warn(\"Current_time: \" + current_time + \" coffee machine off: \" + coffee_machine_time_off + \" heating time off: \" + heating_time_off)\n\n//node.warn(\"coffee machine time off: \" + coffee_machine_time_off)\n//node.warn(\"sunset_minus_one: \" + sunset_minus_one)\n\nif (current_time > lamp_lighting_time_off)\n    msg.payload = \"NIGHT\"\nelse if (current_time > heating_time_off)\n    msg.payload = \"LATEEVENING\"\nelse if (current_time > evening)\n    msg.payload = \"EVENING\";\nelse if (current_time > lateafternoon)\n    msg.payload = \"LATEAFTERNOON\";\nelse if (current_time > morning_time_end)\n    msg.payload = \"DAY\";\nelse if (current_time >  morning_time_start)\n    msg.payload = \"MORNING\";\nelse \n    msg.payload = \"NIGHT\";\n\n\n/*if (current_time > coffee_machine_time_off & current_time < sunset_minus_one)\n    msg.payload = \"EVENING\";\nelse if (current_time > sunset_minus_one & current_time < morning_time_end)\n    msg.payload = \"LATEAFTERNOON\";\nelse if (current_time > morning_time_end & current_time <sunset_minus_one)\n    msg.payload = \"DAY\";\nelse if (current_time >  morning_time_start)\n    msg.payload = \"MORNING\";\nelse \n    msg.payload = \"NIGHT\";\n */  \nnode.warn(\" Final state: \" + msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":382.61266326904297,"y":883.0988788604736,"wires":[["b7da3db0.6a259","f2dacf64.779d9","800cab20.6e24e8"]]},{"id":"b7da3db0.6a259","type":"debug","z":"af7a5936.5847c8","name":"Current Time","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":309.019495010376,"y":532.0039978027344,"wires":[]},{"id":"f2dacf64.779d9","type":"switch","z":"af7a5936.5847c8","name":"Time of Day Switch","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"NIGHT","vt":"str"},{"t":"eq","v":"MORNING","vt":"str"},{"t":"eq","v":"DAY","vt":"str"},{"t":"eq","v":"LATEAFTERNOON","vt":"str"},{"t":"eq","v":"EVENING","vt":"str"},{"t":"eq","v":"LATEEVENING","vt":"str"}],"checkall":"true","repair":false,"outputs":6,"x":680.8571548461914,"y":878.5714807510376,"wires":[["8af47ef6.84452"],["ef560763.b33a48"],["ef560763.b33a48"],["ef560763.b33a48"],["ef560763.b33a48"],["8af47ef6.84452"]]},{"id":"20060081.ad9bc","type":"change","z":"af7a5936.5847c8","name":"Set LATEEVENING","rules":[{"t":"set","p":"payload","pt":"msg","to":"LATEEVENING","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1330.857078552246,"y":927.1428937911987,"wires":[["ef560763.b33a48"]]},{"id":"48eefced.ce1b34","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

Give that a go. I just started trying to re-add the time decision switch node, that function annoys me…my whole reason for want to move to node red is to reduce lines of code…

Thank you! I don’t really code, so for me it’s really the only way. Of course, as of this morning I’m getting a 502 Bad Gateway error on NR…ugh.

I mean this is what it’s replacing (well a fraction of)…so the node read is much easier to read

Where do the Time Entitites come from in HA, I’m not familiar with that concept. Do you have an input field within HA that allows you to set the various times? If so, I’m very interested in learning more about that!

Is there not a simple way to have the following ONLY happen when the alarm is set to “away”, perhaps something more flexible than BigTimer?

The morning time on, off etc? they’re all input_number entities. I only use hours (makes it simpler).

Still looking for a way to utilize the “Override_off” function of BigTimer to turn my lights off at a specific time when my alarm is in away mode

Thank you. I wasn’t familiar with input_number or input_datetime, which are both very useful!