Timers for Outside lights

Hi, I’ve just replaced my porch light with a smart bulb, and want to make it “smarter”. So the current rule I have is this:

  • Between sunset and 11pm, don’t do any shut off timers. However, during this time, make it sensitive to motion so it comes on when motion is detected, or if the door bell is pressed. Probably also when the front door is opened.
  • at 11pm, if it’s still on, turn it off
  • Between 11pm and sunrise, automatically turn off lights in 10 minutes. Automatically turn on when front door is opened
  • Between sunrise and sunset, turn off the light after 3 seconds (just so people can see that it turns on, and the bulb is not broken.

Any feedback / suggestions / a better idea to automate the porch light?

This rule is far from complete, I’m aware of that, but FWIW, here it is below. I am mainly just wanting to get feedback / high level ideas and not seeking help with the actual coding of it at this point.

import java.util.Map

val Map<String, Timer> timers = newHashMap


// Between 11pm - 6am, turn off lights after 10 minutes
// During the day (between sunrise - sunset), turn off lights after 3 seconds 
// (just so people can see that the light did come on, and not wonder if it's blown)
// Between sunset - 11pm, do not automatically turn off lights
rule "Automatically turn off external lights"
when
    Member of gOutsideLights received update ON
then
    val tSunrise = new DateTime(Sunrise_Time.state.toString)
    val tSunset = new DateTime(Sunset_Time.state.toString)
    val hour = now.getHourOfDay
    var timeoutSecs = 0
    if (hour >= 23 || (hour <= 6 && now.isBefore(tSunrise))) {
        timeoutSecs = 10*60
    } else if (now.isAfter(tSunrise) && now.isBefore(tSunset)) {
        timeoutSecs = 3
    } else {
        return
    }
    
    if (timers.get(triggeringItem.name) === null) {
        timers.put(triggeringItem.name, createTimer(now.plusSeconds(timeoutSecs), [
            if (triggeringItem.state == ON) {
                triggeringItem.sendCommand(OFF) 
            }
            timers.put(triggeringItem.name, null)
        ]))
    } else {
        timers.get(triggeringItem.name).reschedule(now.plusSeconds(timeoutSecs))
    }
end

Can you post your items and rules? That gives us a better view of the situation, we could learn from it and give you better feedback.

My current rule is not yet complete, since I haven’t got a door sensor / motion sensor yet. In any case I’ll update my original post with my current rule

Why don’t you want it to turn off the light? Ex in winter the sunset can be at
5pm and at 1 minut after 5pm some rings the bell, the light will be on for almost 6 hours.

I know that your not seeking help with coding, but I can be done with a little bit less coding (e.g. now.isBefore(tSunrise) can be replaced with (now.isBefore(Sunrise_Time.state).

That’s OK, but can you post your items and you can use a dummy item for the door sensor and motion sensor. Because if you want to trigger the rule based on motion or door than “Member of gExternalLights received command ON” this won’t work :wink:

Usually we like keeping the lights on if there’s a visitor or if we’re expecting visitors, as a way of welcoming them. Once they’ve left, we can turn it off manually. Probably have some sort of “Visitors Expected” toggle button somewhere near the entry door - I haven’t thought of this yet. With this, when “Visitors Expected” is active, the light will stay on, but when it’s not active, it will automatically turn off after X minutes like usual.

Thank you for the tip! I didn’t want to ask for coding help since I know the code is still changing at this point, but your tip was very helpful, please keep them coming.

Another great idea about dummy items I hadn’t thought of. I was originally going to wait until my sensors arrive before tinkering with the rules. Now I can have a head start! :slight_smile:

@ljsquare I tried this:

rule "test"
when
    System started
then
    if (now.isBefore(Sunrise_Time.state)) {
        logInfo("test", "****** Before")
    } else {
        logInfo("test", "****** After")

    }
end

And I got this error:

Error during the execution of startup rule 'test': Could not invoke method: org.joda.time.base.AbstractInstant.isBefore(org.joda.time.ReadableInstant) on instance: 2019-06-08T22:44:28.091+10:100:

So it seems that this can’t be done: now.isBefore(Sunrise_Time.state)

I made this rule for turning the lights on when my cars returns after midnight. The lights will turn of after x minutes.

rule:

var Timer myTimer_homelink = null

rule "auto homelink schakeling voorterrein uit na x minuten"
when
    Item Light_Outdoor_voortuin_timer received command ON
then
    var Number hour = now.getHourOfDay
    if (Sun_Elevation.state < 0 && hour >= 0 && hour <= 9 && Light_Outdoor_voortuin_timer.state==OFF) {
       myTimer_homelink_voor.cancel
       myTimer_homelink_voor = null
    }
    else if (Sun_Elevation.state < 0 && hour >= 0 && hour <= 9 && Light_Outdoor_voortuin_timer.state==ON) {
        if (myTimer_homelink_voor!==null) {
           myTimer_homelink_voor.cancel
           myTimer_homelink_voor = null
        }
        myTimer_homelink_voor = createTimer(now.plusMinutes((Minuten_auto_homelink.state as DecimalType).intValue)) [|
            Light_Outdoor_voortuin_timer.sendCommand(OFF)
        ]
      } 
end

sitemap:

Setpoint item=Minuten_auto_homelink		 label="homelink minuten[%d]"				step=1 minValue=1 maxValue=15

item:

Number Minuten_auto_homelink
Switch Light_Outdoor_voortuin_timer	                                         	    	{ channel="knx:device:bridge:generic:Light_Outdoor_voortuin",autoupdate="true"  }

Thanks. You’ve given me several ideas:

  • Have the timeout value stored in a Number item
  • Use Sun_Elevation instead of checking sunrise/sunset

How do you tell/detect when your cars return after midnight? I’ve been thinking about having an ESP8266 inside the car that connects to home wifi when in range, as a means of car presence detection.

Sorry my mistake, I wrote it without testing and between things :wink:
Your first attempt was correct, only I don’t use an extra val.

I have homelink in the car this sends a signal when I am near (approx 20 meters) home on a 433mhz frequency. You can also use a remote control. Got the reciever connected to a KNX input. This functions like a switch.

If you search for homelink 433mhz reciever you will find serveral devices.

1 Like

Thanks again! I didn’t know about Homelink, and we don’t have it here in Australia. However it gave me some ideas, maybe I can install an esp8266 in the car and hook it up to a button somewhere in the car so that I won’t have to fumble around looking for the garage opener. Also a great idea to activate / deactivate the home alarm system and activate lights etc. I’m excited about the possibilities.

If you want to deactivate your alarmsystem please take care of an encrypted transmission. I didn’t go for the alarm because the 433mhz is not protected and anyone can transmit on that frequency.

I’m planning to do it over wifi, so it’s protected by wpa2-psk. Yep it’s no longer secure nowadays, but it’s better than nothing, and certainly much better than 433mhz. But, we’ll see how it goes when it comes to implementing it.