Hi all,
I’ve been away from OH for the past few months, lots of work things going on during the pandemic…
I’ve just popped back into some rules to add a couple of little bits and one is just blowing my mind. I’m sure it’s something simple but I just can’t spot it…
Premise:
I have presence detection from my iphone, I have set a few distances from home as HOME, LOCAL, AWAY. These are then used to do things within OH, ie, if I’m HOME set my presence to ON, and if I’m away trun down the thermostat etc.
I want to add in a quick IF to my case statement where if previous location was away and current is local then I will assume I am coming home so let’s get the thermostat on to warm the house up rather than wait until I’m actually home. Simple you’d think…
The rule itself runs so I’ll only post relevant snippets:
rule "GP iPhone Location detection"
when
Item GP_iPhone_Location changed
then
val PointType phone_location = GP_iPhone_Location.state as PointType
val int distance = phone_location.distanceFrom(home_location).intValue()
val int workdistance = phone_location.distanceFrom(GP_work_location).intValue()
// Calculate the current location for GP
var GPCurrLoc = "UNKNOWN"
switch distance {
case workdistance <= 300: GPCurrLoc = "WORK"
case distance <= 200: GPCurrLoc = "HOME"
case distance > 200 && distance <= 10000: GPCurrLoc = "LOCAL"
case distance > 10000: GPCurrLoc = "AWAY"
}
// Publish the current state when it changes
vGPCurrLoc.sendCommand(GPCurrLoc)
vGPPrevLoc.sendCommand(vGPCurrLoc.previousState(true, "influxdb").state.toString)
So this bit works a treat, it sets my current location and then updates the variable vGPCurrLoc and also set the previous location variable vGPPrevLoc from the influxdb I have set up for it.
This works as I broadcast the current and previous location to the logs:
2020-11-07 12:24:03.113 [INFO ] [ipse.smarthome.model.script.iCloudGP] - GP Current Location = HOME
2020-11-07 12:24:03.120 [INFO ] [ipse.smarthome.model.script.iCloudGP] - GP Previous Location = LOCAL
Perfect…
So, now for the actual rule:
//icloud GP iPhone rule
rule "GP iPhone Location detection - update based on vGPCurrLoc"
when
Item vGPCurrLoc changed
then
// Set Home Item based on vGPCurrLoc to set vPresent
if(vGPCurrLoc.state.toString == "HOME")
{
GP_iPhone_Home.postUpdate(ON)
logInfo(logName, "GP New location is HOME")
vThermostatCase.sendCommand(2) // ThermostatCase rule 2
sendBroadcastNotification("GP New location is HOME")
}
else
{
// I am away so change vPresent = OFF
GP_iPhone_Home.postUpdate(OFF)
// do some things depending on the state of vGPCurrLoc
switch vGPCurrLoc.state.toString {
case "LOCAL":{
logInfo(logName, "GP New location is LOCAL") // Post state to log
// Turn on Thermostat to warm up if coming home
if(vGPPrevLoc.state.toString == "AWAY")
{
vThermostatCase.sendCommand(2) // ThermostatCase rule 2
sendBroadcastNotification("GP Coming Home - Thermostat 2")
}
//if(vGPCurrLoc.state.toString != vGPPrevLoc.state.toString)
//{
sendBroadcastNotification("GP New location is LOCAL")
//}
}
case "AWAY":{
logInfo(logName, "GP New location is AWAY") // Post state to log
//if(vGPCurrLoc.state.toString != vGPPrevLoc.state.toString)
//{
sendBroadcastNotification("GP New location is AWAY")
//}
}
etc etc etc more cases here...
so, this whole rule works, when a location changes, ie I’ve moved, it runs the rule.
If I’m HOME then update my presence variable and turn the thermostat to rule 2 - works, tick
The LOCAL case is the issue, the first part works, so if I am LOCAL the logs show that I’m LOCAL, but the IF just doesn’t seem to run, at first I thougt maybe I’m never marked as AWAY so it wouldn’t run but I can see in the logs that I have been AWAY and then moved to LOCAL so it should trigger.
I can also hover over the variables in VSC and see the values of the vGPCurrLoc and vGPPrevLoc and they also show correctly…
I just don’t get it…
Any ideas / thoughts / pointers / you’re an idiot comments welcome please…