Skip to content

Instantly share code, notes, and snippets.

@bjoernh
Last active December 7, 2025 17:02
Show Gist options
  • Select an option

  • Save bjoernh/28d81390bbf5510072c7a7a5cc499d45 to your computer and use it in GitHub Desktop.

Select an option

Save bjoernh/28d81390bbf5510072c7a7a5cc499d45 to your computer and use it in GitHub Desktop.
Control Rooted LG WebOS TV Energy Saving via Home Assistant (SSH)

Control Rooted LG WebOS TV Energy Saving via Home Assistant (SSH)

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.

The Problem

You want to change energy saving settings programmatically (e.g., turn the screen off while keeping audio running) using luna-send commands over SSH.

  1. 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\"}}'"
  2. 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 Cause

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.

The Solution

To fix this, we need to:

  1. Use a wrapper script to handle the logic cleanly.
  2. Force SSH to allocate a pseudo-terminal using the -tt flag.

Step 1: Prepare SSH Keys

Ensure your Home Assistant instance can SSH into your TV without a password.

  1. Generate keys in HA: ssh-keygen -t rsa -f /config/ssh_keys/id_rsa_lg
  2. Copy public key to TV: ssh-copy-id -i /config/ssh_keys/id_rsa_lg root@YOUR_TV_IP

Step 2: Create the Wrapper Script

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 $?

Step 3: Configure Home Assistant

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 }}"

Step 4: Create Dashboard UI

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"

Valid 'mode' values

Dependent on TV Model/WebOS version. Common values are:

  • off (Energy Saving disabled)
  • min
  • med
  • max
  • auto
  • screen_off (Panel off, audio remains on)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment