[SOLVED] Item state not casted to String correctly when UNDEF

Hi Everyone!

I have a rule:

rule "Turn on Chromecast"
    Item ChromecastRemote changed or
    Item ChromecastAppName changed
    logInfo(filename, "Chromecast state: " + ChromecastRemote.state + " App: " + ChromecastAppName.state)
    if(ChromecastRemote.state == PLAY && ChromecastAppName.state.toString != "Backdrop") {

However I have some problems with it. It seems to execute the send command also when the control state is PLAY and the App name is UNDEF. Why is that? Why it can’t compare UNDEF (as a String) to the String shown there? How can I solve this?

Also anyone has better solution for checking Chromecast state? It also seems that somehow it wouldn’t report instantly it’s state.

I would have expected that to work but UNDEF is a special type. Maybe because it is an enum type it doesn’t have a toString, though I thought everything except for primitives have a toString.

Anyway, UNDEF is a special state meaning the state is undefined. You should probably test for UNDEF and NULL (which means uninitialized) separately in the Rule anyway.

I don’t use this binding so I don’t have any suggestions about the rest.

Thanks I thought the same way! However I just wanted to get some response that I might be missing something, that’s why it is not working as it should. I thought that these states are Java Objects so it should have toString, but I might be wrong.

This should do the job, right?

(ChromecastAppName.state.toString != "Backdrop" || ChromecastAppName.state != UNDEF || ChromecastAppName.state == NULL)

You might want to change the order ; at the moment, you test the state.toString before testing if it is NULL/UNDEF (which apparently throw an error with toString)

1 Like

Thanks I have changed that!