Chromecast Rule : Auto turn on TV on cast

Hi,

simple need: automatic turn on TV and Set HDMI source when a casting to the chromecast start

but to avoid dummy turn on, i need to not turn on when CHANGE FROM UNDEF to Backdrop

rule "chromecast Salon"
when Item Chromecast_LG_App changed
then
    if(Chromecast_LG_App.previousState == NULL) return;
  
    if (Chromecast_LG_App.State.toString="Backdrop"){
                if( Chromecast_LG_App.previousState=UNDEF){return;}}

    LG_P_Power.sendCommand("ON")
    Thread::sleep(1000)       
    LG_TV0_Application.sendCommand("com.webos.app.hdmi2") 
end 

it seems like that it’s not possible to retrieve the previous state on change

Rule 'chromecast Salon': An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: chromecast.rules#|::0.2.1.2.0.1.0::0::/2) to 2018-08-08 16:00:17.990 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'chromecast Salon': An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: chromecast.rules#|::0.2.1.2.0.1.0::0::/2)

Please advise guys

You are confusing something…
There is a function called previousState() which returns a the previous state from persistence, but, for rules triggered by changed there is an implicit variable also called previousState.
Right now, you are calling the function and it should be Chromecast_LG_App.previousState().
So, you might want to try to change your if to:

..............
if(previousState == NULL) return;
..............

See the Docs

thanks for your answer

it look like it works

but i am trying

to return in this case

LG_AppStatus changed from UNDEF to 

i have tried this rule below

rule "chromecast Salon"
when Item Chromecast_LG_App changed
then
    if(Chromecast_LG_App.previousState === NULL) return;  
    if (Chromecast_LG_App==""){return;}

    if ((Chromecast_LG_App=="Backdrop") ||(Chromecast_LG_App===NULL )||(Chromecast_LG_App=="UNDEF" ) ){
             return;}

    LG_P_Power.sendCommand("ON")
    Thread::sleep(1000)       
    LG_TV0_Application.sendCommand("com.webos.app.hdmi2") 
end 

Your rule should be like this:

rule "chromecast Salon"
when Item Chromecast_LG_App changed
then
    if(previousState == NULL) return;  
    ...........
end

Notice the first if in the rule

Does your TV not support HDMI-CEC? When enabled devices like Chromecast will send a command to the TV when someone initiates a cast to turn on the TV and switch to it’s input automatically. No need for OH to be involved at all.

Not all TVs support this and it isn’t always enabled by default. And there might be cases where you wouldn’t want to enable it. For example, I have a Rule that sends my bedroom Roku to the home screen at midnight to stop all nigh streaming if we happened to fall asleep with the TV on. We set a timer on the TV to turn off. But when the command to go home on the Roku was received it turned the TV back on.

On my previous samsung, the TV was automatically turned on and on the right source

With my new LG, its not the case

Based on some quick googling, HDMI-CEC is not enabled by default on LG TVs but it is supported. You can enable it under the SIMPLINK menu in the settings.

I find it hard to believe that LG would make a TV with a network API but not implement HDMI-CEC, but I suppose anything is possible.

Really interesting

i have enabled this option and it well switch the source on the HDMI source but it do not turn on the TV

so, i still need a such rule