Kodi / light rule

You probably want:

sendCommand(Sw_EG_Lampe_Sofa,OFF)

OK, will try that too. But as I wrote, switching off the lamp works correctly. The part that is not working is if(kodi_player_type.state == "episode")

Ah, try this then:

if(kodi_player_type.state.toString == "episode")

Any luck?

Not at first, I thought I was going crazy :smiley:

If I did anything before my if condition, it worked correcty!? And I literally mean “anything” like logging a message.

It turns out, that the rule runs “too fast”. When kodi_player_state changes to Play, it takes a short while for the kodi_player_type to be updated! So if I directly have the if condition, kodi_player_type is still empty and the condition is false. If I did anything before that, the type has enough time to update.

The condition itself works correctly :grinning:

I will insert an additional check to make sure that kodi_player_state is set before I continue with the rule.

Thanks a lot for your help.

Here is how I do it:

rule "Adjust Living lighting when XBMC starts/stops"
when
    Item XbmcLiving_Player changed
then
    var String state = XbmcLiving_Player.state.toString()
    
    logInfo("XBMC State:", state.toString)
    switch (state.lowerCase) {
	 	case "play" :  {
	 		   		sendCommand(Light_GF_Living_Floor, OFF)
	 		   		sendCommand(Light_GF_Living_Iris, OFF)
	 		   		sendCommand(Light_GF_Living_Strip, OFF)
	 				 }
	 	case "pause" : {
	 				sendCommand(Light_GF_Living_Floor, "80,1,20")
	 				sendCommand(Light_GF_Living_Iris, "80,1,20")
	 		   		sendCommand(Light_GF_Living_Floor, ON)
	 		   		sendCommand(Light_GF_Living_Iris, ON)
	 		   		sendCommand(Light_GF_Living_Strip, ON)
	 	}
	 	case "stop"  : {
	 				sendCommand(Light_GF_Living_Floor, "80,1,20")
	 				sendCommand(Light_GF_Living_Iris, "80,1,20")
	 		   		sendCommand(Light_GF_Living_Floor, ON)
	 		   		sendCommand(Light_GF_Living_Iris, ON)
	 		   		sendCommand(Light_GF_Living_Strip, ON)
	 	}
 	}
end

Best regards,
.

Thanks for your example. Good idea with the switch statement, will use that in my rules too now.

in your case, you don’t distinguish between different types? In our case, I don’t want to switch off all lights when we start music :wink: That’s why I want to check the type for “episode” or “movie” first.
Also, once everything works, I will extend it with some Astro binding rules so these light rules only trigger once it’s dark outside.

@pgruetter - Got you regarding types. Personally I use my Onkyo receiver binding for playing audio, and only use KODI for music when the house is in party mode for playing music/visualizations…So haven’t gotten around to wiring player type handling yet, but imagine something like this would work before the switch (untested!):

Thread::sleep(500)
if (kodi_player_type.state.toString != ("episode" | "movie")) {
  return;
}

‘sleep’ is there for a half a second to address the state timing issue you mentioned above. Interested to see what you come with! :slight_smile:

The following now works for me, so I’m very close to your solution:

while(kodi_player_type.state.toString == “”) {
Thread::sleep(500)
}
if(kodi_player_type.state.toString == “episode” || kodi_player_type.state.toString == “movie”)

I’m still learning the rule syntax so I’m always interested in other ways to write my rules.

Also got an Onkyo since yesterday :grin: Although I deactivated the network access because I realized that standby power consumption is VERY high. Got a master slave multiple socket outlet and even on the highest setting, all slaves keep activating / deactivating because the Onkyo uses too much power… bummer :rage:

My Onkyo also stays kind of warm when on standby - have the TX-NR838. According to this, it only uses 5W of power during standby, but I’ve never bothered to measure… There are some options mentioned here you might want to try disabling to see if power consumption during standby can be reduced.

What model do you have, and how many Watts did you measure during standby?

Got a TX-NR609. Already tried disabling all services. The socket outlet can be adjusted between 8W and 80W, although I’m not sure that’s really true. Anyway, I read in another blog post that someone measured around 38Watts, only because of active network setting. I don’t think it’s really 80W on mine, but even 40W is too much to keep it running 24/7.

1 Like

Thanks for sharing! Will have to measure mine soon to see for myself… :sunglasses:

Let me know if you do, interested to see if it’s really that high

Do you guys actually get player.type to update? I have player.state, player.title etc. Updating when I press play, but never type even after a few minutes.

Yes, check my first post how I defined kodi_player_type. This updates about 1s after the state has changed to Play and then contains “episode” or “movie” for example.
Maybe check your logs? I had a syntax error in my items file yesterday and got a nice error message telling me that.

hmm. I see where I went wrong - for some reason it didn’t like hostname being a name. Worked when I changed it to the IP Address

Not sure if this is of any use to anyone but I have a great rule that does all that’s here as well as recording the state the lights were in BEFORE kodi starts playing, has different light optiosn for pause and then resumes lighting once stopped to the state it was in before play started.

Happy to post items and rule if anyone is interested

I know its been a while since you offered to share your rules Andrew, but I’m a fairly new user and I’d like to see what you’ve done. I plan on trying to implement this behavior in the next few days and examples are helpful, especially at this stage for me.

Hi Daniel,

maybe you are interested in my example.

Have Fun.

Hi @divinebovine here is my script in its completeness - it is still running off the old xbmc binding am planning to move it over now we have pause and stop identifiable. Not sure if the includes are needed wither any more:

import org.eclipse.smarthome.core.library.types.DecimalType
import org.eclipse.smarthome.core.library.types.HSBType
import org.eclipse.smarthome.core.library.types.PercentType
import org.joda.time.DateTime

var DateTime time_play_started 

rule "Lights off when Play Starts"
when
	Item kodi_player_state changed 
then
	logInfo("RULES", "Kodi State in lowercase is: " + kodi_player_state.state.toString())
	logInfo("RULES", "kodi player type is: " + kodi_player_type.state.toString())
	var String Kodistate = kodi_player_state.state.toString()
	
	if(Sunset_Event.state == ON && kodi_no_smart_lights.state == OFF) {
logInfo("RULES", "its after sunset")
logInfo("RULES", "kodi state: " + Kodistate.toLowerCase)
		if (Kodistate.toLowerCase == "stop") {
   		logInfo("RULES", "we have stopped")
    		//reset the proxy switch as we have stopped not paused
    		kodi_is_playing.sendCommand(OFF)
    
    		//look into historicstate for all the light values when the first play started
    		var Number Lounge1_DimmerState = Lounge_3_Dimmer.historicState(time_play_started).state
    		var Number Lounge2_DimmerState = Lounge_6_Dimmer.historicState(time_play_started).state
    		var Number Kitchen1_DimmerState = Kitchen1_Dimmer.historicState(time_play_started).state
    		var Number Kitchen2_DimmerState = Kitchen2_Dimmer.historicState(time_play_started).state
			var HSBType LIFX_LoungeState = Lifx_Lounge.historicState(time_play_started).state
			logInfo("RULES", "historic state done")
    		//turn on all the lights back to the value they were when play started				
    		if(Lounge1_DimmerState > 0) {
    			Lounge_3_Dimmer.sendCommand(Lounge1_DimmerState)
    		}
    		if(Lounge2_DimmerState > 0) {
    			Lounge_6_Dimmer.sendCommand(Lounge2_DimmerState)
    		}
    		if(Kitchen1_DimmerState > 0) {
   			Kitchen1_Dimmer.sendCommand(Kitchen1_DimmerState)
    		}
    		if(Kitchen2_DimmerState > 0) {
    			Kitchen2_Dimmer.sendCommand(Kitchen2_DimmerState)
    		}
    		//LIFX bulbs
    		Lifx_Lounge.sendCommand(LIFX_LoungeState) 
    
    		logInfo("RULES", "kodi is stopped so lights should be on as they were")
    		logInfo("RULES", "kodi is playing:" + kodi_is_playing.state.toString())
		}
			
			
		//sleep loop until player type is updated (without this there are null errors)
		while(kodi_player_type.state.toString == "") {
			Thread::sleep(500)
		}	
		//check for TV, Movies, or other such as youtube, TenPlay ABC Iview etc)
		var String KodiPlayerState = kodi_player_type.state.toString()
		if(KodiPlayerState.toLowerCase == "episode" || KodiPlayerState.toLowerCase == "movie" || KodiPlayerState.toLowerCase == "unknown") {
			
			
			logInfo("RULES", "Kodi State: " + Kodistate)
			logInfo("RULES", "Kodi is playing: " + kodi_is_playing.state.toString())
			switch (Kodistate.toLowerCase) {
		    	case "play" :  {
					//check if this is the first time play pressed with a proxy switch
					if (kodi_is_playing.state == OFF) {
						//record the time to use in historicState
						time_play_started = now
						//set the proxy switch as this is the first time
						kodi_is_playing.sendCommand(ON)

						logInfo("RULES", "kodi is playing set to on so this is the first time")
			    	}
					//turn off all the desired lights
					//Lounge_Lamp_Dimmer.sendCommand(OFF)
					Lounge_3_Dimmer.sendCommand(OFF)
					Lounge_6_Dimmer.sendCommand(OFF)
					Kitchen1_Dimmer.sendCommand(OFF)
					Kitchen2_Dimmer.sendCommand(OFF)
					
					
					
					
					//dim LIFX based on scene
					var hue = new DecimalType(242.9993133544921875)
					var sat = new PercentType(100)
					var bri = new PercentType(23)
					var HSBType kodiplay = new HSBType(hue,sat,bri)
					Lifx_Lounge.sendCommand(kodiplay) 	
					logInfo("RULES", "all lights off")	
					 
				}
    			case "pause" : {
    				//look into historicstate for all the light values when the first play started
    				//var Number LoungeLamp_DimmerState = Lounge_Lamp_Dimmer.historicState(time_play_started).state
    				var Number Lounge1_DimmerState = Lounge_3_Dimmer.historicState(time_play_started).state
    				var Number Lounge2_DimmerState = Lounge_6_Dimmer.historicState(time_play_started).state
    				var Number Kitchen1_DimmerState = Kitchen1_Dimmer.historicState(time_play_started).state
    				var Number Kitchen2_DimmerState = Kitchen2_Dimmer.historicState(time_play_started).state
    				//turn on specific lights to a low light only if they were already on when play started
				var hue = new DecimalType(242.9993133544921875)
				var sat = new PercentType(100)
				var bri = new PercentType(100)
				var HSBType kodipause = new HSBType(hue,sat,bri)
				Lifx_Lounge.sendCommand(kodipause) 
    				
    				logInfo("RULES", "kodi is paused so lights should be on dim")
    				//kodi_notification.postUpdate("lights should be dim")
    			}
    
			}
		}
	}
	
	
end





3 Likes