Best way to implement "away"

Certainly one of the most common uses for home automation has to be the “away” state: programming lights to come on and off at various times while I’m gone. But what’s the most logical way to do this in HA?

To make one light come on at a specific time, I can use an Automation. I could add a delay, then turn it off. But what if I want 5 lights coming on and off, at various times, on a schedule? Do I need multiple Automations or is there a simpler, easier-to-maintain way to pull this all together?

1 Like

If you use Appdaemon there’s one called Occusim too.

There’s one for node red too:


If I understand the original question, it’s not so much how to do presence detection, but how to design the logic to perform a number of functions when the user toggles an “away” flag.

I have the same question. I have a number of automations I activate when I’m going to be away overnight or longer. I probably could mash them all together into one mess of spaghetti code, but I like the modular simplicity of one automation for each light or other device which gets special “away” treatment. My lazy solution to date has been to simply name them all with the same first word (“Away…”) so they all sort together in the list of automations. Then just tap each one.

I was thinking of creating “one automation to rule them all” which turns them on all or off with one toggle. Maybe I’d set up a button, and that would trigger the new automation.

Or maybe there’s a better way?

I guess… write a script that just turns on a group of automations… and another that turns them off.

It seems like a good candidate for something built in.

That I wouldn’t do. :slight_smile: This screams for errors and wrongly switched on and off automations.

I’m doing it another way. I’ve setup a few modes, that get controlled by one automation. In every other automation, that is in any way connected to these timings from the modes, I reference that “mode == true”. Will say, the automation only runs, when eg. the light_mode is true and vacation_mode is off.

I found this the easiest way to control things. I don’t need to control this or that light, I just control the light_mode. Eg. the light in my hallway starts when the light_mode gets turned on. The lights in the livingroom don’t need to be turned on that early, so they wait for fifteen minutes before they turn on.

I’d recommend we give this a read:
Making Home Assistant’s Presence Detection not so Binary – Phil Hawthorne

If we think it through, you will find out things are not that elegant when we only have home and away. If we can get this whole away/home/just left/vacation thing under control, arguably we could unlock a lot of ideas on automation.

I have been progressively switching all my HA automations to pyscript. Presence management (ie. what happens when presence switches to away, arriving, home) was one of the first to make the transition. It’s crazy how much more powerful, cleaner and easier to manage and maintain things have become, compared to the messy native HA automations. Half a dozen obscure automations with inline Jinja and unmanageable yaml have become one clean and well structured script.

I realize that this is something that targets mostly programmers, but I suggest taking a look even if you aren’t.

Pyscript - new integration for easy and powerful Python scripting

OK, you guys motivated me to look into scripting.

Just a plain old HA script, created from within the UI, seems to do the trick.

I already have a few automations which turn lights on at various pseudo-random times. Likewise, a few automations which turn those same lights off again.

In the UI, I selected Add Script. I gave it a name and entity ID. For Action Type I selected Call Service. Under Service, I selected Automation: Turn on. Under Targets I selected + Pick Entity. From there I picked my few automations which turn lights on.

I did the same, in a new script, for the “off” automations.

Running these scripts does exactly what I want. It enables or disables the automations which give my house a “lived in” look. Next step is to add a button in Lovelace to run the scripts.

All three of the options I listed are not for presence detection. Instead they randomly switch entities (or follow a pattern from a previous day) to “fake” someone being home to deter burglars.

1 Like

Ahh. I get it now. Good idea!

As said above, it is not a good idea to let automations switch automations on and off. It is prone to produce errors and failure in some situations. I’d look for another way. :slight_smile:

I use a modified version (since I don’t use blueprints :wink:) of the blueprint used in this thread:

Since I don’t actually use the blueprint I can’t completely vouch that it works as intended but you can try it out. If it doesn’t work I can give you the pieces to use it my way without using the blueprint.

I wish Automations had a ‘loop’ construct. Turn the light on, wait a while, turn it off, wait… and repeat.

They have…!? :wink:

  - service: light.turn_on
  - delay: "05:00:00"
  - service: light.turn_off
  - service: automation.trigger
    # call itself

Disadvantage: when HA will be restarted, the currently running automation will end, so the call to re-run thsi script is terminated as well. Btw. this is not a very good example, coding wise, as you normally never-ever call a script in a loop… But for examples sake… :slight_smile:

There are also three different loops available, see here:

And if all of this isn’t helping, there are more constructs, that could achieve what you want. Sorry, can’t see that, automations have loops and more… :slight_smile:

1 Like

Thanks, obviously I should look into Scripts.

You should. :rofl: But in all fairness, I find it very hard to spot (and understand at first), why the instructions for “automation actions” are sorted and named under scripts… I know, that scripts are a sequence of actions, nonetheless I find the naming weird. :slight_smile: