Keith, a reader of this blog, had a rather interesting problem he was trying to solve, and as I was taking a look at it, I thought the solution could help many with how to break down a trickier problem into smaller bits.
What do we want to do?
Use a Hue Motion Sensor to detect motion and turn on lights if three conditionals are true
- There is an hour or less until sunrise, or the sun has already risen
- It is before midnight
- The brightness level in the room where the motion sensor is mounted is below a predetermined lux level (we’ll use 100 lux in the example, but this is completely up to you)
(In case you’re using an adblocker, please consider whitelisting this site. Any revenue from Google ads goes to the costs of running this site.)
The underpinning is of course a HomeKit Automation that runs every time the motion is detected. We then use a shortcut in that automation to get the logic working.
So how can we break down the problem into smaller bits? If we take a look at the three conditionals, we could solve one at a time and create a variable for each, where we store a number that can be used directly in an if-statement.
Conditional 1: There is an hour or less until sunrise, or the sun has already risen
In an earlier post, I explained how you can check if the sun has already risen using the Current Weather function. The logic for this problem is very similar, however, since we want to use an offset of one hour, we need to know how many minutes there are until the sunrise. Luckily, this is quite easy to do.
To get the time for the sunrise, we need to use the function Get Current Weather at Current Location. We then put Get Details of Weather Conditions after that and choose Sunrise Time as the detail we want.
Next, we’ll use the Get Time Between Dates function and use Current Date and Sunrise Time (this is a magic variable from earlier) as parameters and Minutes as the output. We’ll then save the minutes into a variable by using the action Set variable. Name the variable MinutesToSunrise so that it is easy to remember and set the input to Time Between Dates from earlier. This variable is all we need for the if-statement for the first conditional… if MinutesToSunrise is larger than 60 (minutes), it’s too early for the lights to be turned on.
Conditional 2: It is before midnight
When we use the function Get Current Weather, the time and date for the sunrise isn’t always for the current day. When I tested, I noticed that about half an hour after midnight, we are still getting the time and date of the sunrise for the day before (it seems to have updated before one o’clock at night). That means that just because the MinutesToSunrise is a negative number (meaning the sunrise has already happened), it doesn’t mean it’s not past midnight.
The good thing is we can use the day of month of the sunrise, and we are going to use that to see if the data is for yesterday instead of today (which can happen for almost an hour after midnight). We’ll save the day in the month from the sunrise as well as the day in the month for current time into variables. That way we can compare them, and if they don’t match, we know it is after midnight.
(Explanation: If it’s a half hour after midnight, but the Current Weather Sunrise data is still from the day before, the MinutesToSunrise will still be calculated from the last sunrise. This will get us a negative value. We don’t want to set a hardcoded time for when it should start looking for the next sunrise, since the time of sunrise varies a lot in different parts of the world and different times of year. However, if the day of the month of the current time and Current Weather Sunrise differs, we know that the first conditional will be true (MinutesToSunrise will be a negative number which is smaller than 60), and it’s past midnight since (else the two dates would be the same), and this is all we need for the second conditional.)
We start by creating the variable for the sunrise’s day of the month. Start with the Format Date action. When you select the date, choose Variables and then Sunrise Time. Next select Show More and set the Time Format to None and Date Format to Custom. Choose Format String and remove what is written in it and set it to the letter d (lower case). It’s important that it is in fact lower case, or you’ll get something completely different. This will give us the number for the day in the month.
Now add the Set Variable action and name the variable SunriseDay, and the input should be Formatted Date from above.
Now add another Format Date action. It will probably preselect a date for you which you need to delete. Select whatever it has set as default, press the little keyboard icon to the left and press the backspace key. Now select Variables, and from there Current Date. As with the earlier one, you want the Time Format to be set to None and Date Format to be set to Custom. Again, you need to edit the Format String so that it only contains the letter d (lower case).
Next add the Set Variable action and name the variable Today. The input should be Formatted Date from above (it’s probably preselected).
This is all we need for the second conditional.
Conditional 3: The brightness level in the room where the motion sensor is mounted is below a predetermined lux level
The Hue Motion Sensor is equipped with both a thermometer and a lux meter, and we’ll be able to use the built-in lux meter to get the brightness level of the room.
We start with the action Get the state of Home and choose the lux meter in the Motion sensor as the device. Use Current Light Level as the characteristic you want to query. This will get you the lux value of the room, but we want a number, so we have to use the Get Numbers from Input action. Choose Current Light Level from before as the input. We’ll then save that into a variable with the Set variable action. Name the variable LuxNow and set the input to Numbers from the last action.
We’ve now got the four variables we need for the conditionals for the logic to work. For the last part we’ll use three if-statements, the second nested inside the first and the third nested inside the second. In this case it doesn’t really matter in which order we check the conditionals (this isn’t always the case), but I think it’s more natural to check if the time is correct before checking the brightness in the room.
We’ll start with the first if-statement where we check if the variable MinutestoSunrise is less than or equal to 60. We can remove Otherwise and then add another if-statement that we drag in between the first If and End If.
In the second if-statement we set Today as the Input and Is as the condition. In the Text field we add the variable SunriseDay. (This will check if the day of the month of today is the same as the day of the month of the Sunrise date.) Again, we remove Otherwise.
We’ll now add the third if-statement, and we drag it below the second if-statement, but before the End Ifs. Here we choose LuxNow as the Input, select the conditional is less than, and set the lux number we want as the number. I’ll put 100. Once again, we remove Otherwise.
Lastly, we add a Control Home action, and drag it below the last if-statement, but before the first End If. Here we choose the lamp or lamps and set them to turn on.
I hope there are some ideas in this shortcut that you are able to use when solving other problems.
Hey Stefan,
I’ve been reading this post and also the other post on conditional sunrise, but can’t seem to work this out. I also don’t understand why I can’t choose a certain hour to begin and sunrise to end, or vice versa with sunset.
I want to have the light in our stairway to react as following:
1) On from 8h till 21h at 100%, but not after sunrise. So if the sun rises that day at 7h, the light don’t come on.If it only rises at 9h, light will turn on from 8h till 9h.
2) On from 21h till 23h at 50% (so we don’t wake the children with too mach light in the stairway)
3) Off from 23h till 8h
I can do option 2 and 3 from within the Hue app, but not 3. I prefer to get it to work within Homekit. Within homekit I can’t seem to get the lights not to go on between sunrise and sunset. (tried an automaization just for that, but that didn’t work)
Is there a way to get this to work?
Thanks!
Dave
Hi Dave,
That’s an excellent question!
I’m actually sitting and planning a post about the power of the Format Date function, which is really useful for solving your problem. By using the custom format (Hmm), and converting the time into a number, there are a lot of possibilities for conditionals. I’m going to give you a sneak peek to that with the solution to your problem. If you’ve got any questions about the shortcut, just ask away 😉
Thanks Stefan,
I just created the automation, I would’ve never figured it out myself. 😊 I think I’ve forgot one thing, I want the lights to turn off after 5 minutes. What do I need to add for this to work?
Just tried it, when passing the sensor the lights turn on, since it’s only 15h here, I think I missed something… The lights shouldn’t be turning on now.
Thanks!
For turning the lights off, I would use this HomeKit automation shortcut: http://homekitautomationtips.com/how-to-use-a-motion-sensor-to-turn-on-and-off-your-lights-with-homekit/
When you’re motion sensor has stopped seeing motion, it will trigger the Motion Sensor stops detecting motion event, which in turn will run the shortcut. It runs a loop checking if it still doesn’t see any motion. If it sees motion again, it will exit the shortcut, but if no motion has been detected after 90 seconds , it will turn off the lights. If you want it to run for 5 minutes, change the Repeat value to 100.
If you want, I can send you an email to the address you registered with your comment, and you can email me back screenshots of your shortcut. It’s hard to debug it without being able to see it.
Thanks Stefan, I had been looking at the other automation, but wasn’t sure how to include it here.
Sure, sent me a mail. Really appreciate it!
Thanks!
Dave
👍
Hi Stefan,
Thanks – It was actually an hour before Sunset rather than Sunrise that I was after but this is a great guide and it’s working perfectly for me after substituting sunrise for sunset 🙂
It is a little slower to respond than the previous shortcut I used which was just looking at movement and light but I guess that’s to be expected as it’s doing a lot more with the various lookups.
Ops, you’re absolutely correct. Sorry, I don’t understand how I mixed that up.
I made a correct and slightly simpler version for you, that uses the same idea as the one I made for Dave. It might be a little bit faster, but what might take a little bit of time is when it queries the Weather data for the sunset time. Again, if you have any questions, just let me know 🙂 And sorry again for the mixup.
OK, thank you for that, it’s very helpful and I’ll give it a try.
You’ve set SunsetTimeOffset to Calculation Result with the number being 100. Is that 100 minutes before Sunset so for an hour, it would be 60?
I’m wondering if I can implement this along with another time check so that there are two possible outcomes.. From one hour before Sunset until midnight, it runs one scene and then from midnight until an hour before Sunset, it runs another scene (effectively the same one but with dimmer lights). I think it would be more effective to run them both from one shortcut so it only does one ‘get current weather’.
Actually, the 100 is an hour… Sunset is at 6:00 PM, the custom format date will give you the number 1800. If you subtract 100 from that, it becomes 1700… which is 17:00 or 5:00 PM… The problem with this system is that we can’t subtract or add anything less than an hour. (If sunset was at 6:45 PM, and we added 30 minutes, it would be 1845 + 30 = 1875… and that doesn’t translate well. If you really want the ability to add minutes, we would have to convert all time attributes to minutes only… so for instance 10:15 AM would be (10 * 60) + 15)
Having two different outcomes is very simple… you just use an Otherwise on the outer if-statement.
If TimeNow is between SunsetTimeOffset and 2359
If LuxNow is less than or equal to 100
Set Lamp to 100%
End If
Oterwise
Set lamp to 50%
End If
If you want to, you can of course check the lux value after Otherwise with an if-statement.
I’m using that second method and I changed the weather from “Current Location” to specifically be my house – I figured that if it didn’t need to do a GPS lookup, that might also speed things up.
It does seem to react faster now.
Perfect! Glad to hear!
It’s all good – I’ve set up a second one from 00:01 to an hour after sunrise for the lights at a lower brightness using the same principles. I had to use just ‘1’ as the time because it wouldn’t accept leading zeros so I hope that’s going to be interpreted the same way..
Is it possible to clone automations at all? I’ve got three more rooms with similar setups and it would make things a lot easier if I could clone what I’ve already got.
As far as the shortcut is concerned, these are not time units, they are only numbers, so that’s why it doesn’t want leading zeros. So yes, 1 will work as 00:01.
And no, cloning or copying shortcuts is not possible at the moment. For normal, non-HomeKit shortcuts, just running on your iPhone, you’re actually able to point from one to another shortcut, but that doesn’t work in HomeKit. I wish Apple would add this ability sooner rather than later.
I’ve got all my shortcuts working now, from before sunset to midnight and then after midnight until after sunrise. They’re working well, thanks.
One further question – What’s the benefit of using your method of determining the current light level, ie getting the numbers from current light level, setting a variable and checking that as opposed to simply using:
IF [insert motion sensor name] [light level] is less than or equal to 10 lux
Isn’t that taking three steps to achieve what can be checked in a single IF statement?
Great that you’ve got them all working 🙂 I’m glad I could help.
That’s actually a very good question you asked, and you’re correct. You could absolutely put it directly into the if-statement, and that works perfectly fine. However, there has been some bugs with doing that before. For instance, before the last update, there was a bug (I think it was introduced in iOS 14) that made it impossible to check brightness levels in if-statements… they result would always be false. But by actually taking the value, forcing it to become a number and putting it into a variable was a workaround for that bug. What was even more bizarre about that bug was that shortcuts created before iOS 14 ran fine, but when trying to create a new one, it wouldn’t work. So, converting these values into numbers and putting them into a variable is sort of a belt and suspenders kind of approach.
I have an automation that I can’t seem to get running quite right, and recommendations would be helpful.
I have a bedroom that all lights are HomeKit, and I have a LogiCircle 2 camera (HomeKit secure video) that also functions as a motion sensor, has lux sensor, and a presence sensor in the home app.
My schedule is not consistent, so setting actions for a time of day trigger is not optimal.
I have a bedtime scene that I run when I go to sleep, one light is at 1%.
I would like to use the motion sensor to trigger all lights to on and 100% and white (circadian) if that one light is not at that 1%. I don’t want it to trigger the lights to full brightness when motion is detected while the bedtime scene is set. And bonus points if I can have the lights turn off when presence is not detected meaning I left the room (but not turn off the bedtime scene one light at 1% if that is already set to the bedtime scene, Im often still in this room so motion x timer would get old). Reason for motion to turn on vs presence is it reacts quicker when I walk in.
I hope I explained that ok but in essence I want all sensors to do nothing if Bedtime scene is set. (which includes one light at 1% and a purple color.)
Otherwise, use the motion sensor to turn on lights.
Presence sensor, not detecting people, turn off lights.
I’ve gotten close but it just doesn’t seem to be consistent, I think I’m missing something.
Hi Mat,
I started using “state” variables quite early with HomeKit by using smart plugs as variables. For instance, one of the smart plugs was a variable for sleeping, so if that was on, my shortcuts would take a specific path. As I started using more of them, I got a Raspberry Pi that I can use to create virtual switches that take on this job.
However, since you’ve got a specific night light that is set to 1% when your bedtime “state” is active, we can use that as the conditional in the shortcuts. I’ll call this light NightLight in the shortcuts.
I think you need to have two HomeKit automations converted into shortcuts.
The first is when the LogiCircle 2 detects motion:
If NightLight Brightness is 1%
Exit shortcut with Result
Otherwise
Set all the lights to in the bedroom to 100% and white (Control Home action)
End If
And the second automation is when the LogiCircle 2 does not detect presence:
If NightLight Brightness is 1%
Turn off all lights but NightLight in the bedroom (Control Home action)
Otherwise
Turn of all lights in the bedroom (Control Home action)
End If
—
Would this work? Did I understand the brief correctly? 🙂
I wanted to build a shortcut/automation that gradually turns lights from off to full brightness (or the opposite way) while also slowly changing their color in order to simulate a sunrise or sunset in the mornings and evenings. However, I do not necessarily want this to start at sunrise or sunset. So, could I use the time format (Hmm) to offset the automation so it would automatically run a certain amount of time before or after sunset or sunrise? And, can you repeat this within a single automation, so at a blank time before sunrise the brightness adjusts to a specific percent and changes to a specific color, and then at the blank time plus one more min the brightness adjusts again to a specific percent and the color again changes to create a gradual ramp up or down? Or would you need to write an automation for each step?
Hi Matt,
This sounds like a really cool automation.
It seems like the best way to trigger this, at least initially, is by creating a new automation using the event A time of Day Occurs. If you then choose Sunrise or Sunset, a little icon with an i in a circle will appear on the right side. If you press that, you’re able to offset the trigger.
If you want to contain it within a shortcut, remember that the max duration of a shortcut is just under 10 minutes (if you’re using an AppleTV or HomePod as the HomeKit hub, much less if an iPad is the hub). I have a feeling that this is something you want to happen under a much longer time period, and if so, you’ll need to create several automations.
Thanks for your blog, nice to read. Do not stop.
This is great – thanks for the big help. I had an automation up and running based on the sunset time. Then it just stopped working. The automation doesn’t seem to get the weather data anymore. I’ve checked all my location service settings on all my devices and everything seems ok, but it still doesn’t get weather data. The same commands as a shortcut on my phone works though. Any idea what the problem may be? Thanks!
Have you updated to iOS 16? How do you trigger the HomeKit Shortcut? There is a bug in iOS 16, but it’s actually not the weather function (sunset), but rather the triggering. Automations triggered by “An accessory is controlled” are mostly affected.
Thanks for the fast reply. Yes, updated to iOS 16. Automation is triggered by time (have a set up a shutter to close at a specific time if that time is at least 30 min after sunset). While trying to find the problem by running the automation in the home app, I can’t see the weather forecast from getting the daily forecast and then getting the sunset time. Doing the same thing in the shortcut app works. So I thought it might be a location services issue, but checked that and all is correct. Does seem like a bug, but am not finding anything online about others having that issue.
Yes, that seems to be another bug (that the data doesn’t show up at the bottom when test running the shortcut as you are creating it). I also noticed it only affected HomeKit shortcuts, not the normal shortcuts. This makes a bit harder to create shortcuts as you’re not getting the feedback of what the values of variables are. I also thought at first that there was a problem with the Weather function, but I noticed that if you go to the next page where you’re able to “Test This Automation”, it worked.
I’ve found threads about this on Reddit, but it seems the bugs are 100% consistent (which makes it even harder to debug and try to find ways around). I hope it will get fixed with the next update. I am a bit sad thought that there seems to be HomeKit automations problem with almost every major iOS release, which makes wonder if Apple engineers actually use HomeKit automations themselves 🙁
Hey Stefan, I’m so glad I found your website – thank you for creating this! I’m still trying to get the hang of HomeKit automations and struggling a little with understanding the nesting of If command. What I’m trying to do is have occupancy detected on three sensors vs just one (really large living room). Basically I want the automation to look for occupancy across all three sensors for a period of five minutes, and if occupancy is detected on either of them, light should remain on. However, if all detect no occupancy for five minutes, lights should go off.
So, the question is, do I need three separate “If” sets in the shortcut for this, or should it be all nested inside one. Any guidance would be much appreciated.
Hi Aatif,
I’m glad you found the website useful 😊
A warning related to the automation in this article: as of iOS 16, there is a bug which makes the “Get Current Weather” function, as well as the “Get Weather Forecast” function unusable. I’m hoping this will be fixed in the iOS 16.1 update.
When it comes to nesting if-statements, it’s really all about the execution flow of the shortcut. An if-statement nested within another will only run if the first (the outer) is true. So, for instance if you have an example with three nested if-statements (three level deep so to say):
(I’m using “>>” for indentation to make it easier to see which level they are at.)
If A is true
>> If B is true
>>>> If C is true
>>>>>> Turn the lights on
>>>> End If (this belongs to C)
>> End If (this belongs to B)
End If (this belongs to A)
In this example, “If B is true” will only be checked if A is true, and “If C is true” will only be checked if both A and B is true.
However, if we do another example:
If A is true
>> If B is true
>>>> Do X
>> End If (this belongs to B)
>> If C is true
>>>> Do Y
>> End If (this belongs to C)
End If (this belongs to A)
In this example, both B and C will be checked if A is true, but neither will be checked if A is false. The difference to the first example is that C will be checked whether B is true or not, as long as A is true.
If you have shortcuts with a lot of nested if-statements, and if-statements using the otherwise part, it might quickly get a bit difficult to overview. The indentation does help you see the different levels, but I usually do a quick sketch on a notepad or the computer as I’m figuring out how to build a new more advanced automation shortcut.
I actually answered a question a few days ago here about how to use two motion sensors to turn off the lights, but that one was using a slightly more advanced shortcut that dims the lights to 80% for 30 seconds before turning them off. You can check it out if you want to.
In your case, you will need to create three different automations which are the same, but are triggered by the three different motion sensors, when no motion is detected.
In each automation, you want to check all three motion sensors for motion, and they should not be nested within each other. However, they will be nested within another if-statement. I’ll do the whole automation below with comments.
(We start by checking if the light is on, since we only need the shortcut to run if the lights are on. This is the if-statement which the rest of the shortcut will be nested within.)
If Ceiling_light Is On
(We start the loop, which will run 100 times, each time waiting for 3 seconds… 100 x 3 is 300 seconds, which is 5 minutes. We want to keep checking each 3 seconds for movement, since we want to stop the automation if movement is detected.)
>> Repeat 100 times
>>>> Wait 3 seconds.
(At this point we’ll check if the lights have been turned off. In that case, we’ll stop the shortcut. The lights could have been turned off by the user or by another automation, and in that case, there is no use for this shortcut to continue running.)
>>>> If Ceiling_lighs Is Off
>>>>>> Stop this shortcut
>>>> End If
(We check if the first Motion sensor has detected motion, and if so, we stop this shortcut.)
>>>> If Motion_sensor_1 Motion Is Detected
>>>>>> Stop this shortcut
>>>> End If
(We check if the second Motion sensor has detected motion, and if so, we stop this shortcut.)
>>>> If Motion_sensor_2 Motion Is Detected
>>>>>> Stop this shortcut
>>>> End If
(We check if the third Motion sensor has detected motion, and if so, we stop this shortcut.)
>>>> If Motion_sensor_3 Motion Is Detected
>>>>>> Stop this shortcut
>>>> End If
>> End Repeat
(If we’ve come this far, it means that no motion has been detected by any of the three motion sensors for the 100 times the loop has run (for the last 5 minutes), and we can now turn of the lights.
>> Set all the lights in the room to off
End If (This belongs to the very first if-statement where we checked if the lamps are on.)
*********
This should work the way you described. Does it make sense? Do you have any questions? Just remember you need to create this three times, each triggered by a different motion sensor “when no motion is detected”. Let me know if you get it working 😊
Hi Stefan,
Thank you again for replying, this is quite a learning journey. So I built my shortcut by starting with a check for whether the light is already on or not, and the shortcut continues if the light is on. And then the logic that you described. The only difference I see is my final statement for “Set light to off” is outside of the whole If statement, so the final End If is above the Set all lights to off.
Now I’m dealing with a different aspect though I think I found the answer for that on your website in the article you wrote for Keith. Basically if I’m watching TV in the living room, I could be sitting long enough that the motion sensors won’t read any motion, and they’d turn the light off. I created a dummy switch in Homebridge and linked that switch to my TV state, so if TV is on, switch is on and if TV is off, switch is off. I’m planning to add a check for TV state before the shortcut starts executing, so if no motion is detected on one occupancy sensor (primary trigger), it should first look at that TV switch and if it’s on, shortcut should end. I think the logic should work.
Taking things up a notch, I also have time of day automations that keep my living room lights at a certain brightness level during certain times of the day. So the question is, say we’re entertaining guests and they’re staying over late. The simple automation is set to dim living room lights to 30% at 9PM if living room is still occupied; otherwise the “turn off” automation kicks in. Apart from disabling the dimming automation temporarily, could there be a way to override the automation and keep the state of lights as is? Maybe a personal automation since this will be very infrequent.
I have another rather complicated scenario in mind as well with multiple checks for multiple accessories, but I’m trying to work out the flow/logic for that. Instead of making this already long comment longer, I’ll put that in a separate comment.
Happy to help 😊
In this case, it doesn’t really matter if the “Set light to off” is outside the main if-statement. In your version, whether the lights are on or off, the shortcut will still try to turn them off if it has not been stopped inside the if-statement. The end result is the same in this case (in a different shortcut, it might give you an unexpected behaviour).
And you’re using Homebridge! That’s great! That will give you a lot of possibilities when it comes to using dummy switches to create “modes” or “states” (for instance “entertaining guests mode”). I use this a lot in our home, and I should really finish the post I’ve been writing about it. A suggestion I’ve got for you is to create a new room in the Home app called something like Automation, and then put all of these mode dummy switches in that room. It will make the Home app much tidier.
As it goes for your TV watching solution, your solution is perfect.
For overriding automations, I’ve gone the same way as you did with the TV. Create a new dummy switch in Homebridge called something like LivingRoomOverride. You could (and I would) make it resettable with a timer of perhaps 8 hours (28_800_000 milliseconds), which means that it will turn itself off after 8 hours in case you forget to do it. You should of course decide on what number makes sense in your case. You can then make the LivingRoomOverride dummy switch part of an Entertaining Scene which sets the lighting to “entertaining” levels and colors as well as turning the LivingRoomOverride on. I’m also using Hue Dimmer Switches in a few rooms to turn on and off Override dummy switches (which is something my wife has appreciated). Then you do as you did with TV case, and put an if-statement checking if the override dummy switch is on (and if so, stop the shortcut) in the automation shortcut that changes the lights at pre-set times.
Now I’m eager to see what other more complicated scenarios you’ve have thought about 😊
Hey Stefan,
Simpler version of my next question: is it possible to pull current time into a shortcut so that different actions take place depending on time?
Long version: In my bathroom, I have two different lights (one main and one shower) and a fan. There are two motion sensors, one in the main bathroom and the other in the shower. The logic that I’m trying to work with is a little complicated, and I’m wondering if this is even doable.
If Main Motion Sensor detects motion, then I want the automation to check time of day. If it’s between 9AM and 9PM, the main light and fan should come on. If it’s between 9AM and 9AM, then shower light and fan should come on instead of main light. However, I also want the automation to check if the shower motion sensor is detecting motion, and if it is, then the shower light should come on regardless of time.
Turning all these off would be simpler in a separate automation, I believe, but I want to add a second check for humidity in bathroom and leave the fan on if humidity is above a certain level (indicating that someone took a shower).
I’m struggling with establishing what the right flow for this automation will be, or if it can be done in one shortcut at all. Right now I have enabled this scenario but it’s through 5 different automations set up in Home+ app. It works fine, but I was wondering if there’s a way to declutter my automations (and it would open the way for some other cool experiments).
Thanks for being so helpful!
Yes, absolutely. You can use the Format Date function to get the current time. I’ve got a post about it here. You’re also able to set a time condition when creating the automation under Time, Specific times, and then restrict time with a start and end. However, in that case you need to make different automations for different time conditions.
You do want to have separate automations for the two motion sensors though. A small caveat when using the Format Date function method. It’s easy to check for times within the 24 hours of the day. But if you want to check for something that goes to the next day (like your second 9PM to 9AM), that would actually require two checks, from 9PM to a minute before midnight, and from midnight to 9AM. However, in this case we don’t need to be bothered by that, since we can check for 9AM to 9PM, and then use Otherwise for the rest.
This is how I would do it:
Automation 1 (main motion sensor detects motion)
Format Current Date (Custom: Hmm)
Get numbers from Formatted Date
If number is between 900 and 2100
>> Turn on Main_light
Otherwise
>> Turn on Shower_light
End If
Turn on Fan
Automation 2 (shower motion sensor detects motion) – doesn’t need to be a shortcut
Turn on Shower_light
One question. When you detect humidity in the bathroom, do you want the fan to run for a preset amount of time, or do you want to it to run until it’s under a threshold? Both are easy to do. I will assume that you want it to run for X amount of time. I would create a dummy switch named Bathroom_Fan_Delay that will turn itself off after X amount of time. I assume you want the lights to go turn off after 5 minutes?
Automation 3 (main motion sensor detects no motion)
If Main_lights are off
>> If Shower_lights are off
>>>> Stop this shortcut
>> End If
End If
Repeat 100 times
>> Wait 3 seconds
(We do another check to see if the other shortcut has already turned the lights off)
>> If Main_lights are off
>>>> If Shower_lights are off
>>>>>> Stop this shortcut
>>>> End If
>> End If
>> If Main_motion_sensor Motion Is Detected
>>>> Stop this shortcut
>> End If
>> If Shower_motion_sensor Motion Is Detected
>>>> Stop this shortcut
>> End If
End Repeat
If Bathroom humidity is too high
>> Turn on Batchroom_Fan_Delay
Otherwise
>> Turn off Fan
End If
Automation 4 (shower motion sensor detects no motion)
This will be the same as Automation 3
Automation 5 (Bathroom_Fan_Delay is turned off) – doesn’t need to be a shortcut
Turn off Fan
As you can see, this becomes 5 automations as well. So I guess the question is really if you want to have all your automations in the Home app, or rather have them scattered in several places 😊