Timer rule problem - Rule cannot invoke method public abstract boolean org.eclipse.smarthome.model.script.actions.Timer.cancel() on null

Hi,
I have a problem with some rules.
What I’m trying to achieve:
Rule1
-when kodi playback is stopped or paused, switch off TV after 15min. before switching off, make sure it is still stopped. if playback changed to PLAY, cancel the timer.
Rule2

  • when kodi is playing music, switch off TV after 15min. before switching off, make sure it is still playing music (and not videos). If media type is changed to something else than song, cancel the timer.

here are my rules, please let me know where is the mistake,and/or if there is a smarter way of doing this.

var Timer KodiTimer = null

rule "KODI Stop"
when
    Item myKodi_control changed
then
if(KodiTimer !== null) {
KodiTimer.cancel()}
else if(myKodi_control.state.toString == "PAUSE")
{KodiTimer = createTimer(now.plusMinutes(15)) [
 LgTvPower.sendCommand(if(myKodi_control.state.toString == "PAUSE") ON)]}
else if(myKodi_control.state.toString == "PLAY")
{KodiTimer.cancel()}
end

rule "KODI Music"
when
    Item myKodi_mediatype changed
then
if(KodiTimer !== null) {
KodiTimer.cancel()}
else if(myKodi_mediatype.state.toString == "song")
{KodiTimer = createTimer(now.plusMinutes(15)) [
 LgTvPower.sendCommand(if(myKodi_mediatype.state.toString == "song") ON)]}
else if(myKodi_mediatype.state.toString !== "song")
{KodiTimer.cancel()}
end

thanks

I think i figured it out. I am missing if(KodiTimer !== null) on the end of the rule, so he is trying to cancel the timer that is not active (null)

edit: nope, still same boolean error. I give up, I will remove timers from my rules and use expire binding as someone suggested

1 Like

The problem is in your final else if, where the object is null, but you’re still calling cancel on it.

if(KodiTimer !== null) {
    KodiTimer.cancel()
}
else if(myKodi_control.state.toString == "PAUSE") {
    KodiTimer = createTimer(now.plusMinutes(15)) [
       LgTvPower.sendCommand(if(myKodi_control.state.toString == "PAUSE") ON)
    ]
}
else if(myKodi_control.state.toString == "PLAY")
{
    KodiTimer.cancel()
}
end