Variable used in timer not working right in rules

hey all,

im having a small problem with with a small bit of this code. otherwise this works well but still needs more tweaking

the 1st small issue im having is

val Number distancesec = distance/30

im using this in a timer but the timer never runs as long as it is meant to. some times it will be close. others only a small amount of time passes.

2nd issue im had before i got rid of it until i made it stableish was a is device coming closer or is it going check but never worked out how to do it right.

thanks for the help in advanced :slight_smile:

rule "Jess's iPhone Home"
when
    Item icloud_device_30e37292_69c0eba5_location changed or
    Item network_pingdevice_192_168_0_37_online changed from ON
then
    val PointType phone_location = icloud_device_30e37292_69c0eba5_location.state as PointType
    var int distance = phone_location.distanceFrom(home_location).intValue()
    val Number epoch = (icloud_device_30e37292_69c0eba5_locationLastUpdate.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli
	val timenow = now.millis
//************************************************************************
    val Number distancesec = distance/30
    if (network_pingdevice_192_168_0_37_online.state != ON){
        if ((timenow - epoch) > 15000){
            logInfo("FILE", "time is over 15s (jess)")
            createTimer(now.plusSeconds(8))[
                logInfo("FILE", "over 15s refresh (jess)")
                icloud_device_30e37292_69c0eba5_location.sendCommand("REFRESH")
            ]
        }
        if ((timenow - epoch) < 15000){
            logInfo("FILE", "time is under 15s (jess)")
            if (distance >500 ) {
                logInfo("FILE", "timer started for jess for " + distancesec /60 + " minutes")
//**************************************************************************************************
                createTimer(now.plusSeconds(distancesec.intValue))[
                    logInfo("FILE", "distance refresh (jess)")
                    icloud_device_30e37292_69c0eba5_location.sendCommand("REFRESH")
                ]
            }
            else if ((distance > 100) && (distance < 500) && (network_pingdevice_192_168_0_37_online.state != ON)) {
                logInfo("FILE", "Jess's iPhone is opening garage.")
                dmx_dimmer_c60bb925_brightness.sendCommand("90")
            }
            else if ( distance < 100) {
                logInfo("FILE", "Jess's iPhone is at Home.")       
            }
            logInfo("FILE", "(jess) current distance " + distance)
        }
    }
end

rule "Jess's iphone in refresh"
when
     Item icloud_device_30e37292_69c0eba5_location received command "REFRESH"
then
val PointType phone_location = icloud_device_30e37292_69c0eba5_location.state as PointType
logInfo("FILE", "REFRESH received starting timer for 30secs (jess)")
createTimer(now.plusSeconds(30))[
    if (icloud_device_30e37292_69c0eba5_location.state as PointType == phone_location){
    	logInfo("FILE","timeout refresh (jess)")
		icloud_device_30e37292_69c0eba5_location.sendCommand("REFRESH")
    }
]
end

If it runs there there is nothing wrong with your code.
What I think is that your calculation is incorrect and you are not allowing enough time
Maybe try distance/20 or distance/15
Put some logInfo in your rule to get the actual value and monitor what is actually going on

Do you have code for that?
The problem with that sort of functionality is the rate of GPS refresh from the device and the noise removal

thanks Vincent for the the quick reply

i do have a logInfo in there to see it

logInfo("FILE", "timer started for jess for " + distancesec /60 + " minutes")

below says it should be 21 minutes but timer only goes for 16 minutes

018-12-01 19:41:44.088 [INFO ] [.eclipse.smarthome.model.script.FILE] - time is under 15s (jess)
2018-12-01 19:41:44.100 [INFO ] [.eclipse.smarthome.model.script.FILE] - timer started for jess for 21.03333333 minutes
2018-12-01 19:41:44.118 [INFO ] [.eclipse.smarthome.model.script.FILE] - (jess) current distance 37886
2018-12-01 19:58:06.564 [INFO ] [.eclipse.smarthome.model.script.FILE] - time is over 15s (jess)
2018-12-01 19:58:14.573 [INFO ] [.eclipse.smarthome.model.script.FILE] - over 15s refresh (jess)
2018-12-01 19:58:14.637 [INFO ] [.eclipse.smarthome.model.script.FILE] - REFRESH received starting timer for 30secs (jess)
2018-12-01 19:58:16.647 [INFO ] [.eclipse.smarthome.model.script.FILE] - time is under 15s (jess)
2018-12-01 19:58:16.659 [INFO ] [.eclipse.smarthome.model.script.FILE] - timer started for jess for 22.45000000 minutes
2018-12-01 19:58:16.671 [INFO ] [.eclipse.smarthome.model.script.FILE] - (jess) current distance 40431

unfortunately no, i removed that bit of code to get it out of the way as it wasnt working.
the code does try to get the location to within 15 second old

Try this:

val int distancesec = (distance / 30).intValue()