Help With a Rule Machine

I'm struggling trying to write this in rule machine. Pseudo code/logic is as follows

  1. At Sunset, set Dimmer A and B to 50% brightness. Keep this brightness until 9 pm and then turn off.

  2. From Sunset to Sunrise (so this would overlap #1 above for a few hours), if Motion is detected, then set dimmer A and B to 100% brightness.

  3. Wait for further motion for 3 minutes, then if no motion has been detected do one of the following:
    a) Set brightness back to 50% if the time is still under 9:00 pm
    b) Set brightness to 0% (off) if the time is between 9:01 pm and Sunrise. #2 would then run any time motion is detected between 9:01 pm and Sunrise.

I know how to write the steps for #1 and #2 separately, but I'm confused how to make them overlap for #3 to work.


If Time is Sunset to 9 pm 
{
	Set Dimmer A and B Brightness to 50%
		If motionDetected = true
		{
			Set Dimmer A and B Brightness to 100%
			Wait 3 min
			If motionDetected = false
			{
				Set Dimmer A and B Brightness to 50%
			}
		}
}
Else if Time is 9:01 pm to Sunrise
{
	Set Dimmer A and B Brightness to 0% (off)
	If motionDetected = true
		{
			Set Dimmer A and B Brightness to 100%
			Wait 3 min
			If motionDetected = false
			{
				Set Dimmer A and B Brightness to 0% (off)
			}
		}
}

}

Most of us are used to looking at a screen shot of the rule within the UI... but here's a suggestion.

For rule #3, use the trigger of the motion sensor going inactive, followed by a 3 minute wait, then a couple of conditionals to reset the brightness.

I think the setup below would work.

Rule #1

Trigger
certain time sunset

Action set light to 50%
wait for certain time 9:00
if light at 50% turn off. If it is at 100% there has been motion so the next rule will turn it off.

Rule #2

Required expression
sunset sunrise

Trigger
Motion active

Action
Set light 100%
wait for motion inactive duration 3 minutes
if time between sunset and 9:00
set 50%
else
turn off

1 Like

I think you can do this in one rule

Required Expression
Time between Sunset and Sunrise

Trigger
Time is Sunset or
Time is 9:00 PM or
Motion is active

Actions
IF (Motion is active) THEN
    Dim A, B 100%
ELSE-IF (Time is Sunset)
    Dim A, B 50%
ELSE
    Off A,B
END-IF
Wait for Expression: motion inactive
Wait for event: timeout --> 0:03:00
IF (Time between Sunset and 9:00 PM) THEN
    Dim A,B 50%
ELSE
    Off: A,B
END-IF
1 Like

I would have posted a screenshot if I had anything to post at that time

@pseudonym I think you are the closest to what I came up with after tinkering last night. I actually split it into two separate Rules because it was easier for me to compartmentalize it in my head at the time.

Rule 1 just turns on the lights to 25%/35% at Sunset, and then turns them Off (0%) at 9 PM

Rule 2 handles the motion activity from Sunset to Sunrise while also accounting for the desired dim level from Sunset to 9 pm

Need to test a bit more but seems to be working as intended. Took me a while to figure out the difference between the Wait for Events and Wait for Expression. To any other Newbs the Wait for Expression has ability to add the duration, which is what I wanted here without extra code. The Basic Rule app actually has better verbiage called "Motion has stopped and stayed inactive"...I wish the Rule Machine had sometime similar.

Also anyone looking for a motion sensor check out the Fibaro z-wave sensor. It's a great little device and has a ton of other sensors/features to play with besides just motion (luminance, temperature, humidity, vibration, tamper, etc)

Related question: Is there a quick way to "pause" a Rule using a scene or virtual button that would go from Hubitat to Homebridge and then to Homekit (so that my wife or I could pause it with a quick button press in the app)? Use case scenario would be I just want the lights to stay "on" via switch press rather than adjusting brightness or detecting motion for a given night.

Thanks all

@rwswan A few comments. An advantage to a single rule here is if motion is active at sunset, the two rules option will only turn on the lights at the lower levels and not 100%. And if motion is active at 9:00 then the lights will dim to the lower level and not stay at 100%. Probably not a big deal, just pointing it out. Your ELSE doesn't need an IF condition but if you want to use one then I'd make it and ELSE-IF not and ELSE with a nested IF. Again, not a big deal but I'd like it a little cleaner.

There is a gap in you conditions. As an example, Time between 5:00 and 9:00 is valid from 5:00.000 and 8:59.999. Right now you have a 1-minute gap where motion won't control your lights. Change your Time between to 9:00 PM and Sunrise or just use the ELSE.

I don't think the advantage is with one rule, it's just how you write it. I don't believe there is any advantage with a one rule vs a two rule system. If it makes it easier to understand then you can say that is an advantage.

To your point though, if motion is active at sunset then that is true. You could just add a if motion is active into first rule that sets it at 100%. I agree with your other points, just getting rid of the if inside the Else of the second rule. No need to check time again, with the required expression and the first if it can't be anything else.

I would also simplify the first rule. one trigger at sunset and then wait for event 9:00PM and turn it off.

2 Likes

Appreciate all of your tips and I'll try to simplify/clean up the code.

To clarify, at Sunset the automation should set the lights to 35/25% dim level. However, I always want any motion between Sunset and Sunrise to set brightness to 100% for at least 3 min, then back down to 35/25% if before 9 pm. I think my logic does this correctly.

I am doing something similar. I used 4 rules using Simple Rules:
Rule 1
Turn on exterior lights when mode transitions from day to evening.

Rule 2
Dim Exterior (Garage only) lights to 20% when mode transitions from evening to night.

Rule 3
Turn off Threshold light when mode transitions from evening to night.

Rule 4
Turn off all exterior lights when mode transitions from night to day.

In the future, I plan to add a rule that upon motion both turn on Threshold light and set exterior garage lights to 100%, wait a period time then return them to existing settings but limited to night time modes.

You could add the motion logic to the first rule but If motion doesn't go active again within the time frame then the second rule won't trigger and there will be nothing to set the light back to 35/25%. It'll be stuck at 100% until 9:00 so I claim there is an advantage to a single rule here.

This is definitely another way to to do it and might be easier to understand than doing it in Rule Machine. I haven't played around with the Modes yet. I guess I could define a "mode" to be from Sunset to 9 pm (or as granular as I want for the purpose)? That would be the key to making it work with the motion and going back and forth between brightness levels....otherwise you'd have to do it in Rule Machine like I did with the If/Else statements.

Here's a case to consider. If motion goes active 1 minute before Sunset and stays active for 5 minutes, the light will turn on at 35/25% and not 100% to meet your statement above,

My modes are pretty vanilla. Day, Evening, Night, Wake
Using Mode Manager --
Evening is sunset or 7:00PM whichever occurs first.
Night is 10:00PM
Wake is 0445 on Weekdays, and Sunrise + 30min on Weekends
Day is Sunrise +1hr or 0800 weekdays, and just 0800 Weekends

I find it easier to think of Modes as a general condition, kind of like a global variable that can be adjusted but fit with the lifestyle.

I have not coded since college. So, it kind of stretches my brain. I find that so much can be done with Simple Rules, Basic Rules (legacy) and the other apps that it is pretty easy to build out sets of less complex rules for simple tasks and save the heavy lifting for RM.

I'm not sure....if it's Active, then that implies there's new motion event(s), thereby triggering the second rule to set the lights to 100%

@rwswan If the motion active event happens before Sunset and the motion sensor state is active at and beyond Sunset then there isn't an event that meets the Required Expression and the light will not turn on at 100%.

Download the Hubitat app