Welcome Me Home Lights Rule

Hi All

I am trying to implement a rule that would switch on the outside lights when I get near home.

The rule currently consist of:

rule "Welcome Home Lights"
when
    Item iPhone_DistanceFromHome changed
then
    if(iPhone_DistanceFromHome.state <= 800)
    if((hour >= 20)  || (hour <= 5))
    if(Light_Outside_ESPurna_10.state == OFF){
        sendCommand(Light_Outside_ESPurna_10, ON)
 end

Problem with above is that the lights would now to turn on every time that iPhone_DistanceFromHome changes and I am near/at home. How would I get this rule to only fire once when I enter the 800m radius and then re-activate when I leave the 800m radius again to be triggered the next time I arrive home?

Please use code fences How To Ask For Assistance

I would add another proxy item like a switch that will serve as a flag

Switch isHome
rule "Welcome Home Lights"
when
    Item iPhone_DistanceFromHome changed
then
    if(iPhone_DistanceFromHome.state <= 800 && isHome.state != ON) {
        if((hour >= 20) || (hour <= 5))
            if(Light_Outside_ESPurna_10.state == OFF){
                sendCommand(Light_Outside_ESPurna_10, ON)
                postUpdate(isHome, ON)
    }
end

As an exercise, I’d let you come up with the logic to turn isHome OFF

1 Like

@luckymallari, Thanks so much for the guidance. I have now learned the use and appreciate proxy items.

I have ended up creating a proxy item for each family member to indicate whether they are at home or not and also to indicate whether the sun is up or not:

Items File:

Switch JWIsHome "JW Is Home [%s]"
Switch SunIsUp "The Sun Is Up [%s]"		

Rules File:

rule "JW Is Home"
when
    Item iPhone_DistanceFromHomeJW changed
then
    if(iPhone_DistanceFromHomeJW.state <= 900)
    postUpdate(JWIsHome, ON)
end
    
    
rule "JW Is Away"
when
    Item iPhone_DistanceFromHomeJW changed
then
	if(iPhone_DistanceFromHomeJW.state >= 901)
    postUpdate(JWIsHome, OFF)

end

rule "Sunrise Event Occured"
when
    Channel 'astro:sun:home:rise#event' triggered START 
then
        sendTelegram("bot1", "Sunrise Event Occured")
        postUpdate(SunIsUp, ON)
        
end

rule "Sunset Event Occured"
when
    Channel 'astro:sun:home:set#event' triggered START 
then
        sendTelegram("bot1", "Sunset Event Occured")
        postUpdate(SunIsUp, OFF)

end


    rule "Welcome Home Lights For JW"
when
    Item JWIsHome changed to ON
then
    if(SunIsUp.state==OFF){
    sendCommand(CarPortLigtsTimer, ON)
    }
end

rule "Welcome Home Lights Timer"
when
    Item CarPortLigtsTimer changed to ON
then
    if(timer == null || timer.hasTerminated) {
        Switch_ESPurna_28.sendCommand(ON)
        timer = createTimer(now.plusMinutes(10), [ |
          Switch_ESPurna_28.sendCommand(OFF)
          timer = null
        ])
}
else {
    timer.reschedule(now.plusMinutes(10))
}
end

With this I can probably make use of these proxy items for other tasks in future as well…

1 Like

Awesome work!!
Don’t forget to add some logging :slight_smile:

I dont’t know which unit “DistanceFromHome” is, but I would use a hysteresis. If the values are varying a little bit and you are at Distance 900 and the values toggle between 900 and 901, then you have a toggling IsHome state.

I would suggest a hysteresis which is about double the value variation on any the distance of interest. For this just stay at the distance and record the values for DistanceFromHome. If the value is varying with 10 Units (e.g. from 895…905) I would suggest a hysteresis of 20 Units:
DistanceFromHome < 900: IsHome=ON
DistanceFromHome > 920: IsHome=OFF

In your case you’re just getting a lot of updates of the items, but your rule is only triggering once (changed to ON). If you want to turn the lights off when you are leaving, then you are running in troubles…

Andreas