LG webOS change picture setting mode with scripts

I wrote these scripts to change LG smart tv picture setteing via remote button key presses (i.e Living colors, HDR effect, expert light room)

I did not manage to control the picture settings using luna-send (Please share if you have a solution using the API)

scripts.yaml :

tv_pic_menu_enter:
  sequence:
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: MENU
  - delay: 1
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: UP
  - delay: 1
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: ENTER
  - delay: 2
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: RIGHT
  - delay: 1
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: ENTER
  - delay: 1.5
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: ENTER
  - delay: 1
tv_pic_menu_choose_back:
  sequence:
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: ENTER
  - delay: 1
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: BACK
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: BACK
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: BACK
tv_down_button:
  sequence:
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: DOWN
  - delay: 0.2
tv_light_expert:
  alias: TV Expert Light
  sequence:
  - service: script.tv_pic_menu_enter
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_pic_menu_choose_back
    data: {}
  mode: single
  icon: mdi:television-ambient-light
tv_dark_expert:
  alias: TV Expert Dark
  sequence:
  - service: script.tv_pic_menu_enter
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_down_button
    data: {}
  - service: script.tv_pic_menu_choose_back
    data: {}
  mode: single
  icon: hass:sleep
tv_living_colors:
  alias: TV Living Colors
  sequence:
  - service: script.tv_pic_menu_enter
    data: {}
  - service: script.tv_pic_menu_choose_back
    data: {}
  mode: single
  icon: hass:flower

if you’d like to add the rest just copy paste tv_light_expert and remove one DOWN(- service: script.tv_down_button) command line
to choose a previous setting(-1 = HDR effect, -2 = Game etc.)

Also if your only using HASS to change the setting you might use the smaller quick menu by using left and right commands (HDR -> Game = tv_smenu_right; HDR <- Game = tv_smenu_left)

tv_smenu_right:
  sequence:
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: MENU
  - delay: 1
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: DOWN
  - delay: 1
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: ENTER
  - delay: 1
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: RIGHT
  - delay: 1
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: BACK
tv_smenu_left:
  sequence:
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: MENU
  - delay: 1
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: DOWN
  - delay: 1
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: ENTER
  - delay: 1
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: LEFT
  - delay: 1
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: BACK

some tv loads the menus faster in that case you can lower the delay to speed up the command:

tv_pic_menu_enter:
  sequence:
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: MENU
  - delay: 0.5
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: UP
  - delay: 0.5
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: ENTER
  - delay: 1.5
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: RIGHT
  - delay: 0.5
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: ENTER
  - delay: 1.5
  - service: webostv.button
    data:
      entity_id: media_player.lg_webos_smart_tv_2
      button: ENTER
  - delay: 0.5

Hi,

since I desperately wanted to find a way to organize scenes in which I can automatically set the Picture Mode and its parameters (e.g. when the shutters also close automatically and I need to reduce brightness) I’ve eventually found a tool which is capable of doing this. Now I can do this from the command line with this tool:

With this tool you can even change OLED brightness setting when your LG OLED TV is set to energy saving mode in Picture Settings.

From what I could understand from the source code I believe also the LGTV Home Assistant interface should be able to integrate picture mode settings and other settings. Also npm tools like LGTV2 should be able to run the necessary commands.

I installed aiopylgtv in MacOS Terminal:
pip install aiopylgtv

Put this code in a text file, e.g. picure_settings.py

import asyncio
from aiopylgtv import WebOsClient

async def runloop():
    client = await WebOsClient.create('192.168.0.60')
    await client.disconnect()    
    await client.connect()
    await client.start_calibration(picMode="game") 
    await client.set_oled_light(picMode="game", value=100)
    await client.set_contrast(picMode="game", value=70)
    await client.set_brightness(picMode="game", value=60)
    await client.end_calibration(picMode="game")
    test = await client.get_picture_settings()
    print(test)
    await client.disconnect()

asyncio.run(runloop())

Then run:
python picure_settings.py

I use Python 3.9.4

Regards
Helge

5 Likes

I also left feedback at Additional endpoint examples · Issue #17685 · home-assistant/home-assistant.io · GitHub regarding the documentation of webostv integration in Home Assistant.

I’ve manage to do this through telnet:

Check post #24 for updated code.

DEPRECATED - Here's the code I've used
switch:
  - platform: command_line
    switches:
      lgtv_picture_eco:
        unique_id: "lgtv_pict_eco"
        command_on: "echo \"luna-send -n 1 luna://com.webos.settingsservice/setSystemSettings '{\\\"category\\\": \\\"picture\\\", \\\"settings\\\": {\\\"pictureMode\\\": \\\"eco\\\"}}' & exit\" | nc 192.168.0.103 23"
        command_off: "echo \"luna-send -n 1 luna://com.webos.settingsservice/setSystemSettings '{\\\"category\\\": \\\"picture\\\", \\\"settings\\\": {\\\"pictureMode\\\": \\\"normal\\\"}}' & exit\" | nc 192.168.0.103 23"
        command_state: "echo \"luna-send -n 1 luna://com.webos.settingsservice/getSystemSettings '{\\\"category\\\": \\\"picture\\\", \\\"keys\\\": [\\\"pictureMode\\\"]}' & exit\" | nc 192.168.0.103 23 | grep -c eco"
        friendly_name: "LG Picture ECO"
      lgtv_picture_cinema:
        unique_id: "lgtv_pict_cinema"
        command_on: "echo \"luna-send -n 1 luna://com.webos.settingsservice/setSystemSettings '{\\\"category\\\": \\\"picture\\\", \\\"settings\\\": {\\\"pictureMode\\\": \\\"cinema\\\"}}' & exit\" | nc 192.168.0.103 23"
        command_off: "echo \"luna-send -n 1 luna://com.webos.settingsservice/setSystemSettings '{\\\"category\\\": \\\"picture\\\", \\\"settings\\\": {\\\"pictureMode\\\": \\\"normal\\\"}}' & exit\" | nc 192.168.0.103 23"
        command_state: "echo \"luna-send -n 1 luna://com.webos.settingsservice/getSystemSettings '{\\\"category\\\": \\\"picture\\\", \\\"keys\\\": [\\\"pictureMode\\\"]}' & exit\" | nc 192.168.0.103 23 | grep -c cinema"
        friendly_name: "LG Picture Cinema"

For nc command you need to install netcat, e.g.: sudo apt-get install netcat .

Your LG TV should be capable of accepting Telnet connection. You might need to root your tv.

You can apply this to change any settings you want on LG TV, instantly, which give user much more control, and also get instant feedback about status. Here are WebOS API references.

Example automation for automatic switch profile between X hours
alias: "Picture mode change WebOS: Eco"
description: ""
trigger:
  - platform: time
    at: "01:00:00"
  - platform: device
    device_id: 4c58e3068c393b95070e6c5800a35647
    domain: media_player
    entity_id: media_player.lg_webos_tv_uj620v
    type: turned_on
condition:
  - condition: device
    device_id: 4c58e3068c393b95070e6c5800a35647
    domain: media_player
    entity_id: media_player.lg_webos_tv_uj620v
    type: is_on
  - condition: time
    before: "04:00:00"
    after: "01:00:00"
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.lgtv_picture_eco
mode: single
alias: "Picture mode change WebOS: Cinema"
description: ""
trigger:
  - platform: time
    at: "20:00:00"
  - platform: device
    device_id: 4c58e3068c393b95070e6c5800a35647
    domain: media_player
    entity_id: media_player.lg_webos_tv_uj620v
    type: turned_on
condition:
  - condition: device
    device_id: 4c58e3068c393b95070e6c5800a35647
    domain: media_player
    entity_id: media_player.lg_webos_tv_uj620v
    type: is_on
  - condition: time
    before: "01:00:00"
    after: "20:00:00"
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.lgtv_picture_cinema
mode: single
2 Likes

Hi all,

i would like to share my solution. After digging deeper into GitHub - merdok/homebridge-webos-tv: Homebridge plugin for LG webOS TVs i found a way to set specific picture mode using pure HA services.
It’s a little bit tricky because it opens allert popup and then after click fires luna action :slight_smile:

This is my script:

living_room_lg_tv_picture_mode:
  sequence:
    - service: webostv.command
      data:
        entity_id: media_player.living_room_lg_tv
        command: system.notifications/createAlert
        payload:
          message: >-
            {{ picture_mode }}
          modal: false
          buttons:
            - label: "ok"
              focus: true
              buttonType: "ok"
              onClick: "luna://com.webos.settingsservice/setSystemSettings"
              params:
                category: "picture"
                settings:
                  pictureMode: >-
                    {{ picture_mode }}
          type: "confirm"
          isSysReq: true
    - delay: 0.6
    - service: webostv.button
      data:
        entity_id: media_player.living_room_lg_tv
        button: ENTER

as picture_mode you can pass supported picture mode.
Available picture modes (not all are available on all TVs):
cinema, eco, expert1, expert2, game, normal, photo, sports, technicolor,
vivid, hdrEffect, hdrCinema, hdrCinemaBright, hdrExternal, hdrGame,
hdrStandard, hdrTechnicolor, hdrVivid, dolbyHdrCinema,dolbyHdrCinemaBright,
dolbyHdrDarkAmazon, dolbyHdrGame, dolbyHdrStandard, dolbyHdrVivid, dolbyStandard

You can also play with time of delay.

And also worth to note that this approach can be used to modify other settings eg. energySaving

13 Likes

Hello, this script looks very interesting !
I’m a bit new to Hassio.
How to you pass on the parameter: {{ picture_mode }} ? Is it a variable in your script?
Should you configure an input_select variable with all the different choices to then execute the script?
Thanks in advance for the complementary info !

Hi,

just like that:

service: script.living_room_lg_tv_picture_mode
data:
  picture_mode: eco

picture_mode is only variable named in my script

2 Likes

Hello, thanks a lot ! I managed to get the scrip working :slight_smile: The question is: do you know which LG TV would support pictureModes?
When I execute the script, I do see a quick widget appearing on my TV but it doesn’t change picture mode. Might there be other name for: eco, normal, cinema etc…

Hello again, I have started digging in the API.
There might be an error? It seems that pictureMode is in the object dimension and not settings ?

I still can’t manage to pass the parameter to my TV :confused:

I have OLED65C9 and this script is working without problems (i can set eco, normal, 'cinema), also i am able to change energySavingmode. As far as i know there are differeces between models, so maybe there is different name for preset or maybe your model (os version) doesn't support this command. I thinkpictureModenested indimensioncan be used to set mode per eachinput`

1 Like

Could you also share the script for this mode?
Thanks !

Sure :slight_smile:

living_room_lg_tv_picture_energy_saving:
  sequence:
    - service: webostv.command
      data:
        entity_id: media_player.living_room_lg_tv
        command: system.notifications/createAlert
        payload:
          title: "Energy saving"
          message: >-
            {{ energy_saving }}
          modal: false
          buttons:
            - label: "ok"
              focus: true
              buttonType: "ok"
              onClick: "luna://com.webos.settingsservice/setSystemSettings"
              params:
                category: "picture"
                settings:
                  energySaving: >-
                    {{ energy_saving }}
                  energySavingModified: "true"
          type: "confirm"
          isSysReq: true
    - delay:
        milliseconds: 40
    - service: webostv.button
      data:
        entity_id: media_player.living_room_lg_tv
        button: ENTER
3 Likes

Is it necessary to have developer mode activated to be able to send such scripts to webos?

I don’t have this app. Only Mobile Apps anabled and HA paired with my TV.

Thanks a lot, the script works well on my LG CineBeam HU810P projector.

But I have one question: the projector also has an additional picture mode “Brightest”. How can I get the name of the mode to use in the script? hdrBrightest doesn’t work for that

Sorry, I have no idea and no way to check. The trial and error method remains. I hope you succeed :slight_smile:

Sorry if this is a very basic question, as relatively new to HA. Where does the script go, and how can it be triggered - for example in an automation? (I’ve tried adding to scripts.yaml but can’t find a way to trigger it). Thanks.

Edit: Ah I think all this code can be put into the automation itself when formatted correctly.

You can define scipts in scripts.yaml (thank you have to reaload script or restar HA).
Than you will be able to call the script as a service eg:

- service: script.living_room_lg_tv_picture_energy_saving
  data:
     energy_saving: 'max'

Or include whole script in automation but you won’t be able to reuse it.

2 Likes

I have root telnet enabled. I can connect via Putty, and send commands such as below which successfully changes picture mode.

luna-send -n 1 luna://com.webos.settingsservice/setSystemSettings '{"category": "picture", "settings": {"pictureMode": "expert2"}}' & exit

However I’m unable to get the command_line switches code here to work. The switch simply resets to off. The logs show no error.


Edit:

Finally managed to get it working with below…

switch:
  - platform: command_line
    switches:
      lgtv_picture_bright_cl:
        unique_id: "lgtv_pict_bright_cl"
        command_on: >
            echo "luna-send -n 1 luna://com.webos.settingsservice/setSystemSettings '{\"category\": \"picture\", \"settings\": {\"pictureMode\": \"expert1\"}}' & exit" | curl --raw --max-time 1 telnet://192.168.1.4:23
        command_off: >
            echo "luna-send -n 1 luna://com.webos.settingsservice/setSystemSettings '{\"category\": \"picture\", \"settings\": {\"pictureMode\": \"expert2\"}}' & exit" | curl --raw --max-time 1 telnet://192.168.1.4:23
        command_state: > 
            echo "luna-send -n 1 luna://com.webos.settingsservice/getSystemSettings '{\"category\": \"picture\", \"keys\": [\"pictureMode\"]}' & exit" | curl --raw --max-time 1 telnet://192.168.1.4:23 | grep -c expert1
        friendly_name: "LG Picture Bright"
4 Likes