[solved] Problems with TV and Web OS Integration

Good evening,
I am using the latest version of Home Assistant in combination with an LG Web OS TV (2020) and a Sony Soundbar.
I would like to achieve two things:
1.
I want a button with which I can switch the TV on and off (like a normal switch).
The colour of the icon should also change, like when I switch a Shelly Switch, for example.
The Sony soundbar should also be switched on at the same time as the TV.
2.
I want to create buttons for the services Netflix & Co. After clicking on them, the TV starts (if required) and then switches directly to Netflix. If the TV is already on, it only switches to Netflix.

I thought these two things shouldn’t be so difficult, but I’m failing on many levels.
The following problems have occurred:
1.
the TV is switched off via a command, but switched on via WOL.
I can do this individually, but I can’t put it on a button.
I have created the following automation:

- id: '1649289532235'
  alias: TV einschalten
  description: ''
  trigger:
  - platform: homeassistant
    event: start
  condition: []
  action:
  - choose:
    - conditions:
      - condition: device
        device_id: a132aca714d8ccd256452643fbc3125f
        domain: media_player
        entity_id: media_player.lg_webos_tv_un74007lb
        type: is_off
      sequence:
      - service: wake_on_lan.send_magic_packet
        data:
          mac: 58:FD:B1:BC:FB:03
    - conditions:
      - condition: device
        device_id: a132aca714d8ccd256452643fbc3125f
        domain: media_player
        entity_id: media_player.lg_webos_tv_un74007lb
        type: is_on
      sequence:
      - service: webostv.command
        data:
          entity_id: media_player.lg_webos_tv_un74007lb
          command: system/turnOff
    default: []
  mode: single

When I test this directly, it also works.
But as I said, it doesn’t work when I put it on a button and want to trigger it.
And I don’t understand how it works with the icon colour.
2.
the Sony soundbar switches off automatically when the TV goes off, but unfortunately it doesn’t switch on automatically with the TV (I’ve tried everything).
Therefore I need an automation that recognises when the TV switches on and then also switches on the soundbar via WOL.
I have the following automation:

- id: '1649293010914'
  alias: automatisch Soundbar starten
  description: ''
  trigger:
  - platform: device
    type: changed_states
    device_id: a132aca714d8ccd256452643fbc3125f
    entity_id: media_player.lg_webos_tv_un74007lb
    domain: media_player
  condition:
  - condition: device
    device_id: a132aca714d8ccd256452643fbc3125f
    domain: media_player
    entity_id: media_player.lg_webos_tv_un74007lb
    type: is_on
  action:
  - service: wake_on_lan.send_magic_packet
    data:
      mac: 10:4F:A8:AA:AC:72
  mode: single

The problem is that the automation sometimes takes 30 seconds to recognise that the TV has been switched on.
3.
When I switch on the Soundbar via HA & WOL, it is on the TV channel, but there is no sound.
If I switch the Soundbar on normally using the remote control, the channel is also set to TV, but sound comes out of the speakers.
I wonder what the difference is?
In any case, after switching on the Soundbar, I would have to change the channel once to HDMI and then back to TV, then it works.

No one had any idea?

I have now made a lot of progress and have created this template:

switch:
  - platform: template
    switches:
      switch_tv_wohnzimmer:
        value_template: "{{ is_state('media_player.lg_webos_tv_un74007lb', 'on') }}"
        turn_on:
          service: wake_on_lan.send_magic_packet
          data:
            mac: 58:FD:B1:BC:FB:03
        turn_off:
          service: webostv.command
          data:
            entity_id: media_player.lg_webos_tv_un74007lb
            command: system/turnOff

I then placed the template on a button.
The icon colour already changes according to the state of the TV, which works.
Only when I click on the button nothing happens.
It doesn’t matter whether the state of the TV is on or off, nothing happens.
Neither the turn_on nor the turn_off action is executed.

I really need your help.

Found the error in the frontend, not in the template

Code for switching tv on/off

switch:
  - platform: template
    switches:
      switch_tv_wohnzimmer:
        value_template: "{{ is_state('media_player.lg_webos_tv_un74007lb', 'on') }}"
        turn_on:
          - service: wake_on_lan.send_magic_packet
            data:
              mac: '58:FD:B1:BC:FB:03'
        turn_off:
          - service: webostv.command
            data:
              entity_id: media_player.lg_webos_tv_un74007lb
              command: 'system/turnOff'

and Code for switching sony soundbar on when tv is switching on:

- id: '1649546489619'
  alias: Soundbar_anschalten
  description: ''
  trigger:
  - platform: state
    entity_id: media_player.lg_webos_tv_un74007lb
    from: 'off'
    to: 'on'
  condition: []
  action:
  - service: wake_on_lan.send_magic_packet
    data:
      mac: 10:4F:A8:AA:AC:72
  - service: media_player.select_source
    data:
      source: HDMI1
    target:
      device_id: 6feedba24bd837d1f5769c8b8c75cdc6
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: media_player.select_source
    data:
      source: TV
    target:
      device_id: 6feedba24bd837d1f5769c8b8c75cdc6
  mode: single
1 Like