My understanding is that the runIn()
function allows me to execute the named function at a later time. This is the code that shows the problem. If needed I can paste in the whole App, but for now the problem is here:
def nightHandler(evt)
{
log.debug "nightHandler called"
setState()
runIn(5, "setState")
}
def setState()
{
String isNightStr = nighttime.currentValue("variable")
boolean isNight = isNightStr == "true"
log.debug "setState() isOn $state.isOn isNight $isNight"
// change the color of the bulb based on isOn and isNight
}
nightHandler(evt)
gets called twice a day, due to it being triggered by a change in a Hub Variable, using a Connector. The Hub Variable is changed by a couple of rules in Rule Machine, triggered at sunrise and sunset. All that logic works correctly, but I suspect that due to caching / lazy write of the variable the bulb doesn't always change state at sunrise and/or sunset. Thus the desire for a delay.
Except the logs show that the direct call to setState()
in nightHandler(evt)
works, but that the same call is not made five seconds later. I changed the rule to a repeated event that fires every 30 seconds, and uses an If / ELSE / ENDIF
condition to make the Hub Variable flip state every time. Using that, the logs show this:
[code]
app:109 2021-09-16 02:19:00.157 pm debug setState() isOn false isNight true
app:109 2021-09-16 02:19:00.150 pm debug nightHandler called
app:161 2021-09-16 02:19:00.134 pm info Setting Test to true
dev:196 2021-09-16 02:19:00.127 pm info Test variable is true
[/code[
Test
is the variable name, the third line is a log line from the Rule to verify it's activating. I'd expect the top line to be duplicated five seconds later but there's no sign of that call.