Toggling LG TV with webOS

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,

If you just use google to call them, I’d setup scripts to turn on for each of the sources, just tv on and tv off.
I’d also have a button card with buttons to call all the scripts
You’ll need to experiment with the delay, my tv needs ~9 seconds before its ready

1 Like

Yeah working, with 15 sec delay
Will do as you say use google and buttons to call the script

Have a look here, might be of some use:

1 Like

Cool! Didnt know of that one, thx !

I changed the scripts, cause it was sometimes putting on when off and vice versa
This is what i have so far but i get errors

##############################
## TV aanzetten
##############################
tv_aanzetten:
  alias: Activiteit TV kijken
  icon: "mdi:television"
  sequence:
    choose:
      - conditions:
          - condition: state
            entity_id: media_player.living_tv
            state: "off"
        sequence:
          - service: media_player.turn_on
            entity_id: media_player.living_tv
          - delay: 00:00:15
      - conditions:
          - condition: state
            entity_id: media_player.living_tv
            state: "on"
          - condition: template
            value_template: '{{is_state_attr("media_player.living_tv","source","HDMI2")}}'
        sequence:
          - service: media_player.turn_off
            entity_id: media_player.shield_tv 
      - service: media_player.select_source
        target:
          entity_id: media_player.living_tv
        data:
          source: TV VLAANDEREN
            
            
##############################
## Netflix aanzetten
##############################
netflix_aanzetten:
  alias: Activiteit Netflix kijken
  icon: "mdi:netflix"
  sequence:
    choose:
      - conditions:
          - condition: state
            entity_id: media_player.living_tv
            state: "off"
        sequence:
          - service: media_player.turn_on
            entity_id: media_player.living_tv
          - delay: 00:00:15
      - conditions:
          - condition: state
            entity_id: media_player.living_tv
            state: "on"
          - condition: template
            value_template: '{{is_state_attr("media_player.living_tv","source","HDMI2")}}'
        sequence:
          - service: media_player.turn_off
            entity_id: media_player.shield_tv      
      - service: media_player.select_source
        target:
          entity_id: media_player.living_tv
        data:
          source: Netflix
  

##############################
## Film aanzetten
##############################
film_aanzetten:
  alias: Activiteit Film kijken
  icon: "mdi:movie-open-outline"
  sequence:
    choose:
      - conditions:
          - condition: state
            entity_id: media_player.living_tv
            state: "off"
        sequence:
          - service: media_player.turn_on
            entity_id: media_player.living_tv
          - delay: 00:00:15
          - service: media_player.turn_on
            entity_id: media_player.shield_tv
      - conditions:
          - condition: state
            entity_id: media_player.living_tv
            state: "on"
          - condition: template
            value_template: '{{not is_state_attr("media_player.living_tv","source","HDMI2")}}'
        sequence:
          - service: media_player.turn_on
            entity_id: media_player.shield_tv
      - service: media_player.select_source
        target:
          entity_id: media_player.living_tv
        data:
          source: HDMI2
  
##############################
## TV uit
##############################  
tv_uit:
  alias: Activiteit Off
  icon: mdi:power
  sequence:
    choose:
      - conditions:
          - condition: template
            value_template: '{{is_state_attr("media_player.living_tv","source","HDMI2")}}'
        sequence:
          - service: media_player.turn_off
            entity_id: media_player.shield_tv  
      - service: media_player.turn_off
        entity_id: media_player.living_tv

The errors i get

  • Invalid config for [script]: [service] is an invalid option for [script]. Check: script->sequence->0->choose->2->service. (See /config/configuration.yaml, line 58).
  • Invalid config for [script]: [service] is an invalid option for [script]. Check: script->sequence->0->choose->1->service. (See /config/configuration.yaml, line 58).
1 Like

Think you need to add target:

Have a look at the scripts docs there’s examples there

Fixed it :slight_smile: