Change a input_boolean at sunrise or specific time

I’m moving all my devices from SmartThings to HA and still new so please forgive me if this is a dum question. I’m used to the modes in SmartThings so I created a input_boolean that gets triggered when I have Alexa trigger good night it flips the boolean to on. I’m using this as check in node red so my motion sensors don’t turn on the lights when its bedtime, but I want to change it back on in the morning so they start working again. Is there a way to accomplish this or a better way of switching between day and night modes?

input_boolean:
  night_mode:
    name: Night Mode
    initial: off
    icon: mdi:weather-night

1 Like

In Node-RED you can use the inject node to trigger a flow at a specific time (like every day at 6am). For more complex needs there is also the bigtimer node.

There is a node for sun events, too, but i haven’t used it so far, so I cannot say, how it is working. I use the sun2 integration for sun events and feel comfortable with it so far (it’s available from HACS). I trigger my flow with a state event node in that case.

I use node-red-node-suncalc to change a sensor in HA based on the golden-hour.

I use the HA time of day sensors to create “modes” that I use in NodeRed to do this. At bedtime, we tell Google Home “goodnight” and that flips an input_boolean that triggers good night mode. Then, my early morning TOD sensor turns it off and starts waking up the house.

@code-in-progress this is what i’m looking to do, but i’m not sure what a a time of day sensor is. Is that just a node?

Can I see what you have configured in the Early Morning and Set Early Morning? The first one is a event state node from what I can tell, but not sure what node is Set Early Morning

@tobi-bo if i use the inject mode, what do I use? Timestamp as payload and put the time in topic?

I’ve been watching/reading about node-red, but having a difficult time understanding between the payload and topics.

Basically, the TOD sensor creates a binary_sensor that switches on and off based upon times. I use them instead of NodeRed nodes because I still have automations in HA that use them as well and I get a good visual indicator of what mode the house is in. Pair that with the Workday sensor and you have a fantastic way to manage house modes:

image

- platform: workday
  country: US
  workdays: [mon, tue, wed, thu, fri]
  excludes: [sat, sun, holiday]
  add_holidays:
    - '2020-10-09'
    - '2020-10-15'
    - '2020-10-16'

- platform: tod
  name: Early Morning
  after: sunrise
  after_offset: '-00:45'
  before: '08:00'

- platform: tod
  name: Morning
  after: '08:00'
  before: '12:00'

- platform: tod
  name: Afternoon
  after: '12:00'
  before: sunset
  before_offset: '-02:30'

- platform: tod
  name: Evening
  after: sunset
  after_offset: '-02:30'
  before: '20:30'

- platform: tod
  name: Night
  after: '20:30'
  before: '23:59'

Here is my set early morning subflow. What it does is sets a global variable in NodeRed so that I don’t have to constantly query HA for which mode it is in. This allows me to use global.get(“mode”) in Function nodes or Switch nodes easily.

[{"id":"21a6fc58.38b1dc","type":"subflow","name":"Manage Mode","info":"","category":"","in":[{"x":50,"y":30,"wires":[{"id":"2ce8e867.5f6b28"}]}],"out":[],"env":[],"color":"#DDAA99","status":{"x":1140,"y":380,"wires":[{"id":"2ce8e867.5f6b28","port":1},{"id":"f3b51d67.7bb6","port":0}]}},{"id":"e4281ed.028aee","type":"switch","z":"21a6fc58.38b1dc","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"binary_sensor.early_morning","vt":"str"},{"t":"eq","v":"binary_sensor.morning","vt":"str"},{"t":"eq","v":"binary_sensor.afternoon","vt":"str"},{"t":"eq","v":"binary_sensor.evening","vt":"str"},{"t":"eq","v":"binary_sensor.night","vt":"str"},{"t":"eq","v":"binary_sensor.overnight","vt":"str"},{"t":"eq","v":"binary_sensor.workday_sensor","vt":"str"},{"t":"eq","v":"input_boolean.good_night","vt":"str"}],"checkall":"true","repair":false,"outputs":8,"x":470,"y":220,"wires":[["8615c65b.c1f818"],["183d0170.84c41f"],["7d00b6e8.140058"],["1d2f8951.5940af"],["54c8ab49.30f31c"],["aff9e205.29df4"],["553e19ae.e47fd"],["aff9e205.29df4"]]},{"id":"8615c65b.c1f818","type":"change","z":"21a6fc58.38b1dc","name":"Set Global Mode","rules":[{"t":"set","p":"#:(file)::mode","pt":"global","to":"Early Morning","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":120,"wires":[["f3b51d67.7bb6"]]},{"id":"f3b51d67.7bb6","type":"function","z":"21a6fc58.38b1dc","name":"Set Various Global Variables","func":"var mode = global.get(\"mode\", \"file\");\n\nglobal.set(\"night_lights_active\", mode == \"Early Morning\" || mode == \"Evening\" || mode == \"Night\" || mode ==\" Overnight\", \"file\");\n\nreturn msg;","outputs":1,"noerr":0,"x":1000,"y":200,"wires":[[]]},{"id":"183d0170.84c41f","type":"change","z":"21a6fc58.38b1dc","name":"Set Global Mode","rules":[{"t":"set","p":"#:(file)::mode","pt":"global","to":"Morning","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":160,"wires":[["f3b51d67.7bb6"]]},{"id":"7d00b6e8.140058","type":"change","z":"21a6fc58.38b1dc","name":"Set Global Mode","rules":[{"t":"set","p":"#:(file)::mode","pt":"global","to":"Afternoon","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":200,"wires":[["f3b51d67.7bb6"]]},{"id":"1d2f8951.5940af","type":"change","z":"21a6fc58.38b1dc","name":"Set Global Mode","rules":[{"t":"set","p":"#:(file)::mode","pt":"global","to":"Evening","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":240,"wires":[["f3b51d67.7bb6"]]},{"id":"54c8ab49.30f31c","type":"change","z":"21a6fc58.38b1dc","name":"Set Global Mode","rules":[{"t":"set","p":"#:(file)::mode","pt":"global","to":"Night","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":280,"wires":[["f3b51d67.7bb6"]]},{"id":"aff9e205.29df4","type":"change","z":"21a6fc58.38b1dc","name":"Set Global Mode","rules":[{"t":"set","p":"#:(file)::mode","pt":"global","to":"Overnight","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":320,"wires":[["f3b51d67.7bb6"]]},{"id":"553e19ae.e47fd","type":"change","z":"21a6fc58.38b1dc","name":"Set Global Mode","rules":[{"t":"set","p":"#:(file)::workday","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":360,"wires":[[]]},{"id":"2ce8e867.5f6b28","type":"switch","z":"21a6fc58.38b1dc","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":170,"y":240,"wires":[["e4281ed.028aee"],[]]}]

1 Like

@code-in-progress Thank you for the explanation! I think the time of day sensors are a great idea and think this should work for me.

That is a good point I didn’t think about node red having to query HA constantly for the mode. I already have so many questions like what is the input for and why is there 2 switches? Also why would this only be for the morning? Wouldn’t it tell you what the mode is in node red when it sets the global variable? I may have to come back to this once I get better in node red.

1 Like

Ask away! I’m always happy to answer questions.

Yeah, that subflow is actually called for each mode change. I just named them differently for each event node. I wrote this flow sometime last year and I haven’t revisited it in awhile. :slight_smile:

There are 2 switches in the subflow because the first one switches between payload on/off and the second switches on topic. I could rewrite with it a single function node (and probably will now that you brought it up).

[Edit]: Got it cleaned up a bit:

I appreciate it, very interested in learning! HA has so much more customization than SmartThings.

That single function node is much easier to understand now. I looked up global variables and think I have it right, but I think I have something wrong on my event state.

If event state is true then send state to the function, but mine keep saying running which makes believe I did something wrong

This is what I did to declare the global variable.
image

Did you setup the TOD sensors in your configuration.yaml AND restart HA? Always make sure to check the Developer Tools page in HA and search for what you think your entity should be.

And yeah, HA is 1000% more advanced (and better) than SmartThings. I ditched SmartThings years ago when Hubitat came out and then jumped from that to HA.

Oh, for your function node, you’ll need something a bit different. What you are setting is the variable “Mode” to on/off. Not what you want. Try this:

topic = msg.topic.replace('binary_sensor.', '');
global.set('mode', topic);

You will get an a variable called ‘mode’ set to whatever the sensor is named: "mode": "morning" if you named your TOD sensor “morning”.

Yes I have learned to use the developer tools more often. My old motion sensors use a state of 0 and 255 when sensing motion which I never knew.

I did add the TOD to my configuration.yaml and restarted HA. Have them displayed on my lovelace so I know there working and had to restart node red to see them since they were not displaying.

image

Would you be able to give me a screenshot on how this is configured? Sorry i’m not following.

Sure. So, I kind of changed things up a bit (because my previous solution was kind of sucky lol). I renamed my TOD sensors to have “Mode” in the name, (binary_sensor.morning_mode instead of just binary_sensor.morning). After that, I have a single event-state node using regex to find just those entities that feeds into the function node below. I clean up the msg.topic so it just has the name of the mode in it, or my input_boolean for good night. Also, if you are not using file-based storage in NodeRed, you can remove the “file” parameter from the global.set().

image

var topic = msg.topic.toLowerCase().replace("binary_sensor.", "").replace("_mode", " ").replace(" sensor", "").replace("input_boolean.", "").replace("_"," ");

if(msg.payload == "on") {
    if(topic != "workday")
        global.set("mode", topic, "file");
    else
        global.set("workday", msg.payload == "on", "file");
}
status = "Set " + topic + ": " + msg.payload + " : Mode: " + global.get("mode", "file");

node.status({fill:"green", shape: "ring", text: status});

return msg;

I’ll probably have to go with your previous version until I can figure out what you just did…lol I get what you did, but I need to parse through it to understand. If it’s ok, I may reach out to you later once I have time to go through it. My wife and kids have been going crazy past week since the home is no longer smart…lol

The reason why the nodes were still running was because that “Output on Connect” option was not checked. It just switched to afternoon and the other nodes were off so that part seems to be working.

Last piece, how do I start the flow to check what the global variable is? based on the sucky version…lol When the binary_sensor changes to morning, I want it to start turning off and on certain lights. I’m thinking events state node, but I’m not sure how to call the global variable to check.

LOL no problem. You can always DM me if you want help setting it up in a more advanced state.

That’s what the individual event-state nodes do. Because the binary_sensors turn on and off when they change, you can trap those events and act on them. That’s what I do in this part of the flow:

The global variable part is used in other flows:

msg.payload = {};

var mode    = global.get("mode", "file");

node.status({fill:"green", shape: "ring", text: mode });

mode = mode.replace(" ", "_");

msg.payload = {
    domain: "scene",
    service: "turn_on",
    data: {
        entity_id: "scene.kitchen_" + mode
    }
};

return msg;

I think i’m with you now! I was thinking we were using the global variable in the event state node so that it wasn’t constantly checking HA for the current mode so everything stays local. This may be a dumb question, but what’s the point of defining the global variable for the mode when you could just use the event state of the binary_sensor? Isn’t it also pinging HA for the status?

1 Like

Nope. The beauty of the global variable is that NodeRed has it stored locally, so you don’t have to do a round trip back to HA to figure out what mode the house is in.

Consider the 2 flows:

Requires 5 (or more, depending on how many “modes” you setup) calls back to HA to get the state of the TOD sensors.

This one, requires no calls back to HA. Plus, this could be simplified even further to this:

This one requires no calls to HA at all to get the mode, plus you can check the payload being sent from the motion sensor. So, from 11 nodes down to 3 and the first example doesn’t even take into account turning off the lights after a motion event.

The current-state node doesn’t make a call to HA it uses the last state received from HA.

1 Like

From the global homeassisstant object? How often does that get updated?

@code-in-progress i’ll get there eventually! Thank you again for all the help!

1 Like