This guide explains how to control the Energy Saving modes (Minimum, Medium, Maximum, Screen Off) of a rooted LG WebOS TV using Home Assistant shell_commands.
You want to change energy saving settings programmatically (e.g., turn the screen off while keeping audio running) using luna-send commands over SSH.
- Manual Execution Works: Running the SSH command manually in the Home Assistant terminal works perfectly:
ssh root@192.168.1.X "luna-send -n 1 -f 'luna://com.webos.settingsservice/setSystemSettings' '{\"category\":\"picture\",\"settings\":{\"energySaving\":\"screen_off\",\"energySavingModified\":\"true\"}}'" - Home Assistant Automation Fails: When running the exact same command via a Home Assistant
shell_command, nothing happens on the TV.- The exit code is often
0(Success). - Debug logs show the connection is established.
- However: The TV returns no response (no JSON output), and the setting does not change.
- The exit code is often
Home Assistant executes shell commands in a non-interactive environment.
No TTY: The luna-send service on WebOS often requires a pseudo-terminal (TTY) to function correctly. Without it, the process starts but hangs or exits silently without executing the action.
To fix this, we need to:
- Use a wrapper script to handle the logic cleanly.
- Force SSH to allocate a pseudo-terminal using the
-ttflag.
Ensure your Home Assistant instance can SSH into your TV without a password.
- Generate keys in HA:
ssh-keygen -t rsa -f /config/ssh_keys/id_rsa_lg - Copy public key to TV:
ssh-copy-id -i /config/ssh_keys/id_rsa_lg root@YOUR_TV_IP
Create a file named /config/set_tv_energy.sh and make it executable (chmod +x /config/set_tv_energy.sh).
Note: Check where luna-send is located on your TV by running which luna-send via SSH first. It is usually in /usr/bin/ or /bin/.
#!/bin/bash
# --- Configuration ---
# Path to your private key inside Home Assistant
KEY_PATH="/config/ssh_keys/id_rsa_homeassistant"
# IP Address of your LG TV
TV_IP="192.168.1.X"
# Remote command path (Check on TV with 'which luna-send')
TV_COMMAND="/usr/bin/luna-send"
# Local SSH path (usually /usr/bin/ssh)
SSH_BIN="/usr/bin/ssh"
# --- Execution ---
# $1 takes the mode argument (min, med, max, screen_off, auto, off)
# -tt : Forces pseudo-tty allocation (CRITICAL FIX)
# -o BatchMode=yes : Prevents hanging if connection fails
$SSH_BIN -tt -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i $KEY_PATH root@$TV_IP \
"$TV_COMMAND -n 1 -f 'luna://com.webos.settingsservice/setSystemSettings' '{\"category\":\"picture\",\"settings\":{\"energySaving\":\"$1\",\"energySavingModified\":\"true\"}}'" > /dev/null 2>&1
# Return exit code
exit $?Add the shell command to your configuration.yaml. We use a template to pass the mode dynamically.
shell_command:
set_lg_energy: "/config/set_tv_energy.sh {{ mode }}"You can now use this service in your Lovelace dashboard. Here is an example Grid card:
type: grid
square: false
columns: 3
cards:
- type: button
name: Screen Off
icon: mdi:television-off
tap_action:
action: call-service
service: shell_command.set_lg_energy
data:
mode: "screen_off"
- type: button
name: Medium Eco
icon: mdi:leaf
tap_action:
action: call-service
service: shell_command.set_lg_energy
data:
mode: "med"
- type: button
name: Normal (Off)
icon: mdi:brightness-7
tap_action:
action: call-service
service: shell_command.set_lg_energy
data:
mode: "off"Dependent on TV Model/WebOS version. Common values are:
off(Energy Saving disabled)minmedmaxautoscreen_off(Panel off, audio remains on)