What wrong with this rule?

Hi I’m getting Error during the execution of rule ‘Lights off when Play Starts’
java.lang.NullPointerException: null

Can anyone tell me what is wrong with this rule?


import org.openhab.core.library.types.DecimalType
import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import java.lang.Math
import org.joda.time.*

var AbstractInstant time_play_started

rule “Lights off when Play Starts”

when

Item xbmc_state_1 changed
then

var String state = xbmc_state_1.state.toString()

if (state.lowerCase == “play”) {
// store the state when play was pressed
time_play_started = now
sendCommand(Lounge_Dimmer, OFF)
}
if (state.lowerCase == “pause”
|| state.lowerCase == “stop”) {
var play_time = xbmc_state_1.lastUpdate
if(Lounge_Dimmer.historicState(play_time) == “ON”) {
sendCommand(Lounge_Dimmer,ON)
}
}

I think your instances of .lowerCase should be .toLowerCase.

This is the rule I use, works fine here.

rule “Lights dimm play”
when
Item Status_Kodi changed to Play
then
sendCommand(Lys_stue_venstre_dimm, “20”)
sendCommand(Lys_stue_hoyre_dimm, “20”)
end

rule “Lights dimm pause/stopp”
when
//Item Status_Kodi changed from Play to Pause or
Item Status_Kodi changed from Play to Stop
then
sendCommand(Lys_stue_hoyre_dimm, “100”)
sendCommand(Lys_stue_venstre_dimm, “100”)
end

Thanks for the reply - I had a working script like yours working fine - however the workflow with that script is a but of a problem as if the light is not on when Kodi plays, I want it to not be on when kodi stops (for example in the middle of the day). With this script the light comes on regardlesss of it’s previous state.
That was where I was trying to get persistance to evaluate the state prior to the play event but there is something either not initialised or there is some syntax problem. I will change tolowercase and see if thats causing it

nup not just the lowercase

is it that I need to initialise some values - how do I do this if so?

OK some more testing its definately this line:

xbmc_state_1.lastUpdate

I have persistance using db4o catching all changes

this is the item type
String xbmc_state_1 “Player State [%s]” (gLounge) { xbmc="<[#livingRoom|Player.State]" }

can you have persistance on a string?

Yes, string Items are persistable, however, it depends on the persistence service, but db4o should do it.

I have one question about your Code:
You defined time_play_started to save the time, when playback started, but you never make use of this variable.
Instead you make use of .lastUpdate, which should be fine.
Now .lastUpdate should deliver Date, historicState expects AbstractInstant. I’m not very familiar with Types, so… is that correct?
Maybe you have to make a TypeCasting like xbmc_state_1.lastUpdate as AbstractInstant or so…

Hi thanks for the reply, yes I am finding all sorts of logic issues with the code, however I am down to exactly what you have described.

The problematic line is this if condition:
if(Lounge_Dimmer.historicState(play_time) == 100)

I am defining
var play_time = xbmc_state_1.lastUpdate

I have tried typing play_time as an abstractinstant but this seems to not make the variable accept lastupdate at all