LG TV Binding

This binding supports LG TV models with Netcast 3.0 and Netcast 4.0 (Model years 2012 & 2013), and with LG TVs which support the UDAP 2.0 protocol over Ethernet. See "UDAP Specifications (For Second Screen TV and Companion Apps)".

Author: Martin Fluch 25.1.2014

Binding Configuration

This binding can be configured in the file services/lgtv.cfg.

PropertyDefaultRequiredDescription
<lgtvId1>.hostYeshost name or IP address of the LG TV to control
<lgtvId1>.port60128NoTCP port number of the LG TV to control
<lgtvId1>.serverportYesTCP port address of the openHAB system to receive LG TV status messages. Only the first occurance is used for all TVs and is usually 8080.
<lgtvId1>.xmldatafileslocation to put xml files with the information of availiable channels and apps
<lgtvId1>.checkalivecheck if TV is availiable every in seconds. Common value is 60
<lgtvId1>.pairkeyYesif it's wrong the device shows the right pairkey at every connection attempt
<lgtvId2>.hostYeshost name or IP address of the LG TV to control
<lgtvId2>.port60128NoTCP port number of the LG TV to control
<lgtvId2>.xmldatafileslocation to put xml files with the information of availiable channels and apps
<lgtvId2>.checkalivecheck if TV is availiable every in seconds. Common value is 60
<lgtvId2>.pairkeyYesif it's wrong the device shows the right pairkey at every connection attempt

Where <lgtvIdN> is a name you can use to reference the device from your item configuration (see below).

Example

In services/mgtv.cfg:

wohnzimmer.host=192.168.77.15
wohnzimmer.port=8080
wohnzimmer.pairkey=
wohnzimmer.serverport=8080
wohnzimmer.xmldatafiles=./
wohnzimmer.checkalive=60

Item Configuration

The syntax of the binding configuration strings accepted is the following:

lgtv="<openHAB-command>:<device-id>:<device-command>[,<openHAB-command>:<device-id>:<device-command>][,...]"

where parts in brackets signify an optional information.

  • The <openHAB-command> corresponds to the OpenHAB command

  • The <device-id> corresponds device which is introduced in services/lgtv.cfg

  • The <device-command> corresponds to LG TV commands. See complete list below.

Predefined Commands

CommandDescription
POWERPOWER
N0Number 0
N1Number 1
N2Number 2
N3Number 3
N4Number 4
N5Number 5
N6Number 6
N7Number 7
N8Number 8
N9Number 9
KEY_UPUP key among remote Controller’s 4 direction keys
KEY_DOWNDOWN key among remote Controller’s 4 direction keys
KEY_LEFTLEFT key among remote Controller’s 4 direction keys
KEY_RIGHTRIGHT key among remote Controller’s 4 direction keys
KEY_OKOK
KEY_HOMEHome menu
KEY_MENUMenu key (same with Home menu key)
KEY_BACKPrevious key (Back)
VOLUME_UPVolume up
VOLUME_DOWNVolume down
KEY_MUTEMute (toggle)
CHANNEL_UPChannel UP (+)
CHANNEL_DOWNChannel DOWN (-)
KEY_BLUEBlue key of data broadcast
KEY_GREENGreen key of data broadcast
KEY_REDRed key of data broadcast
KEY_YELLOWYellow key of data broadcast
KEY_PLAYPlay
KEY_PAUSEPause
KEY_STOPStop
KEY_FFFast forward (FF)
KEY_REWRewind (REW)
KEY_SFSkip Forward
KEY_SBSkip Backward
KEY_RECORDRecord
KEY_RECORDLISTRecording list
KEY_REPEATRepeat
KEY_LIVETVLive TV
KEY_EPGEPG
KEY_CURRENTPROGCurrent program information
KEY_ASPECTAspect ratio
KEY_EXTERNALINPUTExternal input
KEY_PIPPIP secondary video
KEY_SUBTITLEShow / Change subtitle
KEY_PROGRAMLISTProgram list
KEY_TELETEXTTele Text
KEY_MARKMark
KEY_3DVIDEO3D Video
KEY_3DLR3D L/R
KEY_DASHDash (-)
KEY_PREVCHANNELPrevious channel (Flash back)
KEY_FAVORITEFavorite channel
KEY_QUICKMENUQuick menu
KEY_TEXTOPTIONText Option
KEY_AUDIODESCRAudio Description
KEY_NETCASTNetCast key (same with Home menu)
KEY_ENGERGYSAVEEnergy saving
KEY_AVMODEA/V mode
KEY_SIMPLINKSIMPLINK
KEY_EXITExit
KEY_RESERVATReservation programs list
PIP_CHANNEL_UPPIP channel UP
PIP_CHANNEL_DOWNPIP channel DOWN
KEY_SWITCHPSECSwitching between primary/secondary video
KEY_MYAPPSMy Apps
REQUESTPAIRKEYMF: Request Pair Key
SENDPAIRKEYMF: Send Pair Key
CHANNEL_SETMF: Set Channel
VOLUME_CURRENTMF: Get Current Volume
VOLUME_ISMUTEDMF: Get Is Muted
CHANNEL_CURRENTNUMBERMF: Get Current Channel Number
CHANNEL_CURRENTNAMEMF: Get Current Channel Name
CHANNEL_CURRENTPROGMF: Get Current Program Name
GET_CHANNELSMF: Get all Channels
GET_APPSMF: Get all Apps
APP_EXECUTEMF: Execute Application
APP_TERMINATEMF: Terminate Application
CONNECTION_STATUSMF: Get Binding Connection Status

For those who try to POWER ON - LgTV shuts down the network port so only power off is availiable 😭

Examples

items:

Switch LgTvPower                        "Power Command"                 (GF_Living)     {lgtv="ON:wohnzimmer:POWER"}
Switch LgTvMute                         "Mute"                          (GF_Living)     {lgtv="ON:wohnzimmer:KEY_MUTE, OFF:wohnzimmer:KEY_MUTE"}
Number LgTvVolume                       "lgVolume [%.1f]"               (GF_Living)     {lgtv="INIT:wohnzimmer:VOLUME_CURRENT, INCREASE:wohnzimmer:VOLUME_UP, DECREASE:wohnzimmer:VOLUME_DOWN, *:wohnzimmer:VOLUME_SET"}
Number LgTvChannel                      "Channel [%.1f]"                (GF_Living)     {lgtv="INIT:wohnzimmer:CHANNEL_CURRENTNUMBER, INCREASE:wohnzimmer:CHANNEL_UP, DECREASE:wohnzimmer:CHANNEL_DOWN, *:wohnzimmer:CHANNEL_SET" }
String LgTvChannelName                  "Channelname [%s]"              (GF_Living)     {lgtv="INIT:wohnzimmer:CHANNEL_CURRENTNAME"}
String LgTvGetChannels                  "getchannels [%s]"              (GF_Living)     {lgtv="INIT:wohnzimmer:GET_CHANNELS, ON:wohnzimmer:GET_CHANNELS"}
String LgTvGetApps                      "getapps [%s]"                  (GF_Living)     {lgtv="INIT:wohnzimmer:GET_APPS, ON:wohnzimmer:GET_APPS"}
String LgTvAppExecute                   "excuteapp"                     (GF_Living)     {lgtv="*:wohnzimmer:APP_EXECUTE"}
String LgTvAppTerminate                 "terminateapp"                  (GF_Living)     {lgtv="*:wohnzimmer:APP_TERMINATE"}
Switch LgTvConnStatus                   "connstatus "                   (GF_Living)     {lgtv="*:wohnzimmer:CONNECTION_STATUS"} 

sitemap:

Frame label="Tv"
    {
        Switch item=LgTvConnStatus
        Switch item=LgTvPower mappings=[ON="Power Off"]
        Setpoint item=LgTvChannel minValue=0 maxValue=300 step=1.0
        Setpoint item=LgTvVolume minValue=0 maxValue=100 step=1.0
        Switch item=LgTvMute mappings=[ON="Mute", OFF="Mute"]
        Text item=LgTvChannelName
        Selection item=LgTvChannel mappings=[1="ORF1",2="ORF2"]
        Switch item=LgTvAppExecute label="AppExecute" mappings=[Skype=Skype, Spotify=Spotify]
        Switch item=LgTvAppTerminate label="AppTerminate" mappings=[Skype=Skype, Spotify=Spotify]
    }

BETA FUNCTION: Usage of LG TV as openHAB "screen"

The initial idea behind the binding was the following usecase: In the morning (office days) a start of the TV should automatically start a browser showing latest news, traffic information and the status of the openhab sites (eg heating, water and power consumtion of the house ...) - controlled via an openhab rule.

As the binding is able to start an internet browser on the tv a functionality to automaticly change websites in the screen browser is embedded.

the functionality is realized via a small embedded webserver in the binding which shows a minimalistic (not visible) website (a frame). this website shows the given url described in an item and refreshes it if the configured item changes the value to a new website.

to automatically enable the feature you should set the bindings location as default page in the browser - the location is:

http://<ipadressofopenhab>:<portofopenhab>/udap/api/event?devicename=<deviceid>

example: http://opnehabserver:8080/udap/api/event?devicename=wohnzimmer

Try the sample by creating the following items in lgtv.items in the appropriate folder

extend the item file eg. lgtv.items

String LgTvAppExecute                   "excuteapp"                     (GF_Living)     {lgtv="*:wohnzimmer:APP_EXECUTE"}
String LgTvBrowserRemote                "wohnzimmer url"                (GF_Living)     {lgtv="*:wohnzimmer:BROWSER_URL"}
Number LgTvInfoMode                     "infomode [%.1f]"               (GF_Living)

Within a rules file you can then open a browser and set a location

LgTvAppExecute.sendCommand("Internet")
postUpdate(LgTvBrowserRemote,"http://www.profil.at")

With the following example rule you can automate this process and changes websites based on a timeshedule.

create a rules file lginfoscreen.rules

import java.lang.Math

var Number lgruninsec=0
var String site=""
var Number currentpage=0

rule "infosystemstart"
when
 System started
then
        println ("infosystemstart")
        lgruninsec = 0
        currentpage = 0
end

rule "infostart"
when
        Item LgTvInfoMode received update
then

        if (LgTvInfoMode.state!=0)
        {
                if (lgruninsec==0)
                {
                        LgTvAppExecute.sendCommand("Internet")
                        lgruninsec = lgruninsec+1
                        currentpage=1
                }
                currentpage=LgTvInfoMode.state
                if (currentpage==1) site="http://www.google.com"
                if (currentpage==2) site="http://www.diepresse.at"
                if (currentpage==3) site="http://www.spiegel.de"
                if (currentpage==4) site="http://openhabserver:8080/openhab.app?sitemap=demo"
                postUpdate(LgTvBrowserRemote,site)
        }else
        {
                LgTvAppTerminate.sendCommand("Internet")
                lgruninsec=0
        }
end

rule "infolaeuft"
when
        Time cron "0,15,30,45 * * * * ?"
then
        var int s1=0
        var int s2=0

        if (LgTvInfoMode.state>0)
        {
                lgruninsec = lgruninsec + 1
                currentpage=LgTvInfoMode.state
                if (lgruninsec > 4)
                {
                        currentpage = currentpage+1
                        lgruninsec=1
                }
                logInfo("lginfomode","infolauft: lgruninsec="+lgruninsec+" currentpage="+currentpage)
                if (currentpage>4) currentpage=1
                postUpdate(LgTvInfoMode,currentpage)
        }
end