Generic rules and scripts to control lights based on time, daylight and presence

Hi

I am quite new to OpenHAB but i have now played and tried to setup my own environment for some time and has succeed quite well i think but i still have some trouble to design some rules.

I mainly want to use OpenHAB (for now) to automatically control lights in my house. The turning on and off should be based on:

  • time of the day
  • when i am home
  • when the sun is down
    I already have one switch that is ON when someone is at home and OFF when nobody is home. Another switch that is ON during daytime and OFF during night. This is based on example code i have found on this site + some modifications.

Now to my problem, how do i control my lights/group of lights in a simple way? This is how I want it to work:
Weekdays turn on the light between 7 AM and 10PM if I/someone is at home and the sun is down but on weekends (and holidays?) it should be between 9AM and 23PM.
I want the rule to be generic so i can have different time for different groups of lights in a simple way.

Is this possible to do in a simple way in OpenHAB using rules and scripts? Currently i am using SwitchKing and this is basically what that tools is doing but now i want to migrate to OpenHAB.

Is there anyone that has a some code like this or could help me design the code? If possible please provide some example code or link to examples.

I hope i can get some help here and that its not to hard to solve.

I am using OpenHAB 1.7.1 on a Pi2 with tellstick Duo but will probably expand to Z-wave also when i get satisfied with my solution.

/Fredrik

Doing what you are asking is not as simple as you might think. Doing so in a single rule would not be simple at all. I think you will find that if you break it up it will be easier to code, understand, debug, and expand.

See this thread for how I set up my lighting (post 3) and how I manage time of day events (post 11).

Also see this post for an alternative approach to generating time of day events if you have more than just a few.

At a high level:

  1. Create groups for each behavior your want in your lights. For example, one group for lights that come on in the evening, another for those that come on at sundown, one for lights that turn off at 11, etc).

  2. Create switches for each of the time of days you care about. Some could be triggered by the Astro binding, others by cron triggered rule or by timers.

  3. Create rules to turn on and off the time of day switches at the right time.

  4. Create rules that trigger on the time of day swtiches that turn on and/or off lights based on that time period

  5. Add if statements to your rule to check for special cases (e.g. don’t turn on the lights if no one is home).

  6. See this example for how to calculate public holidays

Finally, consider using the CalDAV binding and put your on/off times as entries in a calendar instead of doing number 3 above.

1 Like

Thanks for the tips how to design the rules. I can notice that it will be quite a lot of rules to handle my system but i guess this is the best way to handle it.
If anyone else has some ides or can tell me about your solution i would be very happy but i think i should be able to do it with the tips from rikoshak.

/Fredrk

The rules can be simplified depending on the triggering events and preconditions for your specific scenario. For example, if your only triggering events are “present” and “not present”, then you may not need all the items and rules for times of day. Those are just preconditions to check given a presence trigger. My understanding is that @rlkoshak is also using time of day as a triggering condition (independent of presence) so he needs extra items and rules to support that behavior.

I have lighting rule for lights controlled by a PIR motion detector with a light sensor. When motion is detected (triggering event) and it’s dark (precondition) then I turn on the light and create (or update) a timer to turn off the light. I also use time of day as a fallback precondition if I don’t have a light sensor reading for some reason. It’s a relatively simple rule that only uses a few items, but it works well for my requirements.

His original requirements in the first posting were:

Which is why I sent him the examples I did.

But I completely agree, if you don’t care about time of day you can make the rules WAY simpler. Stupid time. :wink:

I saw the list, but he also said …

which could possibly imply that “when i am at home” (arrive/depart) is the triggering event and “time of the day” and “when the sun is down” are preconditions. I wasn’t criticizing your suggestion but I wanted to emphasize the distinction between triggering events and preconditions as a way to reduce the number of required items and rules in some cases.

This is different than not caring about time of day. However, if time of day is a precondition rather than a trigger it can be checked “on demand” when the rule is triggered.

Very good point.

Thanks for the suggestions. One of the things I will try to do is to make many small and simple rules instead of one big, but i think i need some thinking before i do it since this is rather complex for hobby programmer like me.

I can also see that i can solve some of my issues in different ways and need to find out what is best. I also think i need several solutions not one as I requested in my first post.