Location-based light trigger

Hi

I’ve created this nodered flow to turn on/off a light if it is dark and if I’m home. However, I feel like this isn’t running the way it should be and that some settings might be wrong (MSG payloads, bigtimer at wrong place…). I feel like a lot of data is triggered every minute which isn’t needed.

Any tips or improvements for this flow?

https://imgur.com/a/mxqtFk9

The way I do it is using the Times of the Day sensors in HA (https://www.home-assistant.io/integrations/tod/). I have one setup for each specific time of the day and for all my “normal” lighting, I have scenes created that mirror the tod of sensors:

- platform: tod
  name: Early Morning
  # after: sunrise
  # after_offset: '-01:00'
  after: '05:30'
  before: '08:00'

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

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

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

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

- platform: tod
  name: Overnight
  after: '23:59'
  before: '05:30'

Then, in NodeRed, I created a “mode manager” that triggers on the various tod sensors and stores the current “mode” in global:

Finally, I use those in NodeRed to test for when lights should come on or be off:

This is the code for my “Setup Payload” function:

msg.payload = {};

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

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

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

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

return msg;

I can help you setup something simpler (or as complex) as mine is if you want. It’s actually a lot easier than it looks. In your case (a single light if you are home or not and is nighttime), it can be done easily using the sun sensor as well. The big timer node can (and should) be removed as you don’t need it if you use the tod sensor(s).

it’s difficult to tell without the real code but i think bigtimer outputs “on” or “off” from the 1st output. then change 2nd switch accordingly.
try to put a few debug node after bigtimer and see what is it spitting out to be sure…

Wow. I will have a closer look at this later this evening because this is quite something to grasp haha.

1 Like

I have already tried the debug, but as far as I can tell, bigtimer gives either a 0 or a 1 as payload to trigger. I have put “on” for the ON msg and “off” for the OFF msg but I’m not sure what this does as I do not see this data anywhere in the debugger. It works fine with the switch on 0/1, but no clue why.

The thing which “worries” me is that my lights on/off service get triggered every minute, which is also flooding my log viewer etc. I already disabled repeat output in bigtimer.
I also feel that bigtimer is working on its own, and isn’t only getting triggered when my switch triggers it when I’m home…

Ah. I think I noticed in bigtimer what you meant. I changed the connection between bigtimer and the switch to the first “box” and adjusted the switch to “on/off”. I suppose this should stop the constant triggers as this is exactly the point of the second box of bigtimer.

I now hope this will work if I go away from home during my set time, but also return within set time, since now bigtimer will only output on/off once.

You can test it by changing the state in home assistant manually. Developers>states and find the entity, set state…

I have found the fault, bigtimer doesn’t care about my override whether I’m home or not, if it is dark it will trigger the light on/off services. It works on its own within my flow. Seems like I have to find a different time methode.

time-range-switch will do.