Toggling LG TV with webOS

Hi,
I’m trying to create a script that will toggle my LG TV power state.
So far this is what I configured:
configuration.yaml:

##########################################################
# swtich for WOL LG TV
##########################################################
switch:
  platform: wake_on_lan
  name: lg
  mac: !secret lg_mac_address
##########################################################
# LG smart TV
##########################################################
webostv:
  host: !secret lg_host
  name: Living Room TV
  #other settings
  turn_on_action:
    service: switch.turn_on
    entity_id: switch.lg
  customize:
    sources:
      - Mi Box
      - youtube
      - netflix
      - PS4
      - makoTV

script.yaml:

lg_toggle_power:
  sequence:
    - service: switch.toggle
      data_template:
        entity_id: switch.lg

But it only powers the TV on and not powering it off.
The goal is to create one button to do both on and off functions. Something like this:

 denon_toggle_power:
  alias: "toggle power denon"
  sequence:
    - service: media_player.toggle
      data_template:
        entity_id: media_player.denon_2

Thanks in advance!

Make a template switch.

- platform: template
  switches:               
        lg_tv:
            friendly_name: LG TV
            value_template: "{{ is_state('media_player.living_room_tv', 'on') }}"
            turn_on:
              service: media_player.turn_on
              data:
                  entity_id: media_player.living_room_tv
            turn_off:
              service: media_player.turn_off
              data:
                  entity_id: media_player.living_room_tv

I think this shouldn’t be in the esphome section.

Moved

thanks for the answer.
once I define this template, how do I use it?

Can’t you just use media_player.toggle ? Don’t need a switch ?

Youre trying to toggle your wake_on_lan switch

Your script just needs to be:

lg_toggle_power:
  sequence:
    - service: media_player.toggle
      entity_id: media_player.living_room_tv

You can also remove the WOL switch and add it directly to the webos config, make sure to add the broadcast address that suits you LAN (not the IP of your TV)

webostv:
  host: !secret lg_host
  name: Living Room TV
  turn_on_action:
    service: wake_on_lan.send_magic_packet
    data:
      "mac": !secret lg_mac_address
      "broadcast_address": "192.168.1.255"
      "broadcast_port": 7
  customize:
    sources:
      - Mi Box
      - youtube
      - netflix
      - PS4
      - makoTV
1 Like

You were correct, I used the media_player service and it worked.
THANKS!

Hi all, complete novice here. Trying to make my first script. I’m trying this one however am getting an error when attempting to save.

I’ve tried removing the first line and then I get the following error:
Error executing script. Invalid data for call_service at pos 2: required key not provided @ data[‘command’]

Might need an alias ?

lg_toggle_power:
  alias: LG Toggle Power
  sequence:
    - service: media_player.toggle
      entity_id: media_player.living_room_tv

Same error when attempting to save the script.
Message malformed: extra keys not allowed @ data[‘lg_toggle_power’]

I must be doing something really basic (and obvious) wrong. Is it where I’m editing the script?

Doing it from the UI, Configuration, Scripts, New Script, Edit in YAML, Save Script.

alias: lg_tv
sequence:
  - service: media_player.toggle
    target:
      entity_id: media_player.living_room_tv
mode: single

or

alias: lg_tv
sequence:
  - service: media_player.toggle
    data:
      entity_id: media_player.living_room_tv
mode: single

and when you want to call the service directly from lovelace (without a script), for example from a button

it would be

tap_action:
  action: call-service
  service: media_player.toggle
  service_data:
     entity_id: media_player.living_room_tv

Thanks everyone. Issue ended up being related to a corrupt configuration. scripts weren’t saving correctly.

Hi @Holdestmade
Thx for this! Works like a charm!

Now i want to put it a set further
I want 3 scripts:

  • turning that tv on/off but starting at source: tvvlaanderen (its an app)
  • turning that tv on/off starting at source netlix
  • turning that tv on/off + starting my nvidia shield on/off

Is that possible with webos or not?

Thx for your help

Yes that should be possible

For the first 2, in your scripts, first turn on the TV, then you’ll probably need a delay to wait for the TV to power up and be ready, then call the media_player.play_media service as described here:

For the 3rd, do the same but just turn on your shield

1 Like

ok but what do i do then with media_player.play_media
media_content_id: “netflix”
media_content_type: “source”

Something like that?

Just tried this, this works:

service: media_player.select_source
target:
  entity_id: media_player.monitor
data:
  source: Netflix

Make sure your sources are setup and in the source_list (viewable in dev tools states)

1 Like

Cool thx !

One more question
Now it looks like this, the script

##############################
## Netflix aanzetten
##############################
netflix_aanzetten:
  alias: Netflix aanzetten
  icon: "mdi:netflix"
  sequence:
    - service: media_player.toggle
      entity_id: media_player.living_tv
    - delay: 00:00:02
    - service: media_player.select_source
      target:
        entity_id: media_player.living_tv
      data:
        source: Netflix

Before i added the select source, i could use the script to turn on / off
How will that react now?
Cause i added the select source, or doesnt it do any harm?

If its state is off now, it will turn on and select source netflix
if its state is on now, it will turn off and select the source too ?

It should be OK, depending on whether the TV turns back on when the select source is called ?
Problem is if you run this and netflix is playing, then you run another script to watch another source, it will turn the TV off

You may need a turn and and off script.

You could either have separate buttons to call them or you could set up a template switch to run the on and off actions

Thats true.
When tv is on, and i run script to watch netflix it will turn off instead of switching sources
However, if the tv is on (source tv or netflix) we will probablhy dont use the script to change sources, once the tv is on
i’m not sure yet

For now i’m still struggling with the source he doesnt select, propably the delay is too short

So how would you do it? A turn on/off sciprt, or having separate buttons or with a template switch
Whats the best route to go

Turning on/off we will mostly use google to call the script,