This is a JS library that implements TCP network control for LG TVs manufactured since 2018. It utilizes encryption rules based on a guide found on the internet. A non-encrypted mode is provided for older models, but hasn't been tested.
This is not provided by LG, and it is not a complete implementation for every TV model.
Requirements
- LG TV (tested on model OLED65B9PUA and OLED42C2PUA)
- Node 12+ (at least ES2017)
Installing
# Using NPM
npm install lgtv-ip-control
# Using Yarn
yarn add lgtv-ip-control
Before anything, you need to enable Network IP Control, which is very easy:
- Open the "All Settings" menu on the TV
- Using the remote arrows, navigate the focus to "Connection", do not enter it. For some TVs, this may say "Network" instead.
- Quickly, press
82888
using the remote numeric buttons - Note the MAC IP addresses for reference and library configuration
- Turn "Network IP Control" on
- Click "Generate Keycode", and take note of the 8 characters code displayed on the message for reference and library configuration. You can generate a new code at any time
- If you want to be able to turn the TV on, turn "Wake On LAN" on
Here's a very basic example of how to control the TV:
import { LGTV } from 'lgtv-ip-control';
const tv = new LGTV('192.168.1.100', '1a:2b:3c:4d:5e:6f', 'KEY1C0DE');
tv.connect()
.then(async () => {
console.log('Unmutting...');
await tv.setVolumeMute(false);
console.log('Setting volume to 15...');
await tv.setVolume(50);
console.log('Done!');
})
.catch(console.error);
To use import
, you need to make sure to save the file as .mjs
or to specify "type": "module"
on your package.json
. Learn more about Node.js' support for ESM.
Otherwise replace the first line with:
const { LGTV } = require('lgtv-ip-control');
Returns a new instance to control a TV.
const lgtv = new LGTV(
/**
* TV IP Address
*/
'192.168.1.100',
/**
* TV MAC Address for being able to turn the TV on remotely, `null` otherwise
*/
'1a:2b:3c:4d:5e:6f',
/**
* Encryption Keycode, as generated during "Setting Up the TV" above.
* If not provided, uses clear text, but is required by most models.
*/
'KEY1C0DE',
/**
* Additional options (optional)
*
* See src/constants/DefaultSettings.ts file.
*/
{
...DefaultSettings,
}
);
Connects to the TV using TCP.
Required before sending any commands.
await lgtv.connect();
Disconnects from the TV.
await lgtv.disconnect();
Gets the current app. May be one of the Apps
enum or an arbitrary string if
the app type is unknown.
const currentApp = await lgtv.getCurrentApp();
Gets the current volume as an integer from 0
to 100
.
const currentVolume = await lgtv.getCurrentVolume();
Gets the ip control state.
const ipControlState = await lgtv.getIpControlState();
Gets the MAC address by network interface.
const macAddress = await lgtv.getMacAddress('wired');
Gets the mute state.
const muteState = await lgtv.getMuteState();
Powers the TV off.
await lgtv.powerOff();
Powers the TV on, using Wake On Lan. Requires MAC address to be set when
creating the LGTV
instance.
lgtv.powerOn();
Sends a key
, as if it was pressed on the TV remote control.
await lgtv.sendKey(Keys.menu);
See Keys
for available keys.
Sets the current energy saving level. Note that screenOff
is known not to
work for some models.
await lgtv.setEnergySaving(EnergySavingLevels.maximum);
See EnergySavingLevels
for available levels.
Sets the current TV input.
await lgtv.setInput(Inputs.hdmi1);
See Inputs
for available inputs.
Sets the volume level as an integer from 0
to 100
.
await lgtv.setVolume(15);
Sets the volume mute state.
await lgtv.setVolumeMute(false);
Key | Brightness Level |
---|---|
auto | Automatic |
screenOff | Screen Off |
maximum | Low |
medium | Medium |
minimum | High |
off | Maximum |
Key | Input |
---|---|
dtv | Digital TV |
atv | Analog TV |
cadtv | Cable Digital TV |
catv | Cable TV |
av | AV Composite |
component | Component |
hdmi1 | HDMI 1 |
hdmi2 | HDMI 2 |
hdmi3 | HDMI 3 |
hdmi4 | HDMI 4 |
Key | Remote Button |
---|---|
arrowDown | Arrow Down |
arrowLeft | Arrow Left |
arrowRight | Arrow Right |
arrowUp | Arrow Up |
aspectRatio | Aspect Ratio Toggle |
audioMode | Audio Mode Toggle |
back | Back |
blueButton | Blue Button |
captionSubtitle | – |
channelDown | Channel Down |
channelList | Channel List |
channelUp | Channel Up |
deviceInput | Device Input Toggle |
energySaving | Energy Saving Toggle |
fastForward | – |
greenButton | Green Button |
home | Home |
info | Info |
liveTV | Live TV |
menu | Open Menu |
number0 | Number 0 |
number1 | Number 1 |
number2 | Number 2 |
number3 | Number 3 |
number4 | Number 4 |
number5 | Number 5 |
number6 | Number 6 |
number7 | Number 7 |
number8 | Number 8 |
number9 | Number 9 |
ok | Ok |
play | – |
previousChannel | Previous Channel |
programGuide | Show Program Guide |
record | – |
redButton | Red Button |
rewind | – |
sleepTimer | Sleep Timer Toggle |
userGuide | Open User Guide |
videoMode | Video Mode Toggle |
volumeDown | Volume Down |
volumeMute | Mute Toggle |
volumeUp | Volume Up |
yellowButton | Yellow Button |
This library uses wes-cli
, which simplifies configuration setup. Instead of
using yarn install
, you should use npx wes-cli install
, which will create
all configuration files and run yarn install
.