An Insight on detecting TV power state reliably

  • Platform information:
    • Hardware: Raspberry Pi 4
    • openHAB version: 2.5

Hi all, I just wanted to share some insight I had regarding the use of the LG WebOS binding.
Specifically, how to figure out if the TV is ON or OFF.

You need to use to different methods to turn the TV ON and OFF.
To turn ON the TV you need to send a WOL packet via the WOL binding.
After the TV is ON you can use the power channel of the LG WebOS binding to turn it OFF.
However this power channel doesn’t seem to reliably represent the correct state of the TV. For example when I turned the TV OFF from the remote control, this item stayed in an ON state.

The way I decided to resolve this is to detect the state of the TV from the App ID channel (Application channel). It’s a string channel that contains the id of the app the TV is currently running. I noticed that when the TV is OFF the string gets cleared out.
So I used this item to detect whether the TV is ON or OFF and updated a virtual switch to represent the TV state. This switch is what I exposed to the UI and when the user interacts with it the rules activate the WOL and WebOS binding behind the scenes.

my items configuration:

Switch LR_TV_PowerWOL "Living Room TV WoL"                  <switch>                {wol="255.255.255.255#??-??-??-??-??-??" }       // replace ?? with the relevant mac address of the TV
Switch LR_TV_PowerBinding "Living Room TV OFF from binding" <switch>                {channel="lgwebos:WebOSTV:e529500c-e871-8022-3bb1-d8cb828f5da1:power"}
Switch LR_TV_PowerMain "TV"                                 <screen>                {ga="Switch"}
String LR_TV_App "Living Room TV App Launcher"                                      {channel="lgwebos:WebOSTV:e529500c-e871-8022-3bb1-d8cb828f5da1:appLauncher"}

my rules:

val String filename = "devices.rules"

rule "Turn ON TV from openhab UI"
when
    Item LR_TV_PowerMain changed from OFF to ON
then
    logInfo(filename, "Turned ON TV from openHAB UI")
    if (LR_TV_PowerWOL.state != OFF) { LR_TV_PowerWOL.sendCommand(OFF) }
   	LR_TV_PowerWOL.sendCommand(ON)
end

rule "Turn OFF TV from openhab UI"
when
    Item LR_TV_PowerMain changed from ON to OFF
then
    logInfo(filename, "Turned OFF TV from openHAB UI")
    if (LR_TV_PowerBinding.state != ON) { LR_TV_PowerBinding.sendCommand(ON) }
    LR_TV_PowerBinding.sendCommand(OFF)
end

rule "Detect TV State from App ID"
when
    Item LR_TV_App changed
then
    logInfo(filename, "LR_TV_App changed to " + LR_TV_App.state)
   	if (LR_TV_App.state == "") {
           logInfo(filename, "TV is OFF")       // no App ID means the TV is OFF
           LR_TV_PowerMain.sendCommand(OFF)     // update UI power switch
    } else {
        logInfo(filename, "TV is ON")          // any App ID means the TV is ON
        LR_TV_PowerMain.sendCommand(ON)        // update UI power switch
    }
end

and that’s it, simple as that.

Hope it helps, and I’d be happy to here any responses, remarks or suggestions.

Thank you,
Chenta

2 Likes

Hi there,
While continuing playing with the binding and the TV, I noticed that the App Launcher channel can also be used to directly turn ON and OFF the TV.

Remember it’s a string item-
by sending it and empty string ("") I managed to turn the TV off,
and while the TV is OFF- if I would send it a name of an app the TV would turn ON and launch the app.

It means that I don’t longer need the WOL binding to turn the TV ON.

That’s it for now.
Chenta