If you used a variable to store the state before movie starts, it would look something like this:
var String state_before_play = ""
rule "Lights off when Play Starts"
when
Item xbmc_state_1 changed
then
if (state.lowerCase == "play") {
// store the state when play was pressed
state_before_play = Lounge_Dimmer.state.toString()
sendCommand(Lounge_Dimmer, OFF)
}
// combining these states for simpler code
if (state.lowerCase == "pause"
|| state.lowerCase == "stop") {
// this if could be combined with the outer if. Keeping separate for clarity.
// If combined, it would be in the form: if(("pause" || "stop") && state_before_play)
if(state_before_play == "ON") {
sendCommand(Lounge_Dimmer,ON)
}
}
end
Alternatively, if you wanted to use historic state it would look similar with just a few tweaks
var AbstractInstant time_play_started
rule "Lights off when Play Starts"
when
Item xbmc_state_1 changed
then
if (state.lowerCase == "play") {
// store the state when play was pressed
time_play_started = now
sendCommand(Lounge_Dimmer, OFF)
}
// combining these states for simpler code
if (state.lowerCase == "pause"
|| state.lowerCase == "stop") {
// this if could be combined with the outer if. Keeping separate for clarity.
// If combined, it would be in the form: if(("pause" || "stop") && historic_state)
if(Lounge_Dimmer.historic_state(time_play_started) == "ON") {
sendCommand(Lounge_Dimmer,ON)
}
}
end
I don’t think either of these would survive an openHAB restart as the variable defined above the rule would be cleared.
To use the extensions as Chris was suggesting, you could use this extension:
.lastUpdate - Query for the last update timestamp of a given item.
So the embedded if in the pause/stop portion of the rule would look something like this:
var play_time = xbmc_state_1.lastUpdate
if(Lounge_Dimmer.historic_state(play_time) == "ON") {
sendCommand(Lounge_Dimmer,ON)
}
And if Lounge_Dimmer.historic_state(play_time) for some reason gives Lounge_Dimmer’s state AFTER the switch was changed and lights were turned off, you could get the state a second or two before. Something like this:
if(Lounge_Dimmer.historic_state(play_time.minusSeconds(1)) == “ON”) {
I haven’t tested any of the code in this post, but hopefully if anything it would just need syntax tweaks.
Hope this helps!