Changing channels by voice on LG Webos

Changing channels by voice on webos

I’m a beginner on HA and a non-programmer. With the help of ChatGPT, I’ve managed to do some simple things. My TV is already turning on by voice and changing channels through the dashboard, but I want to make it change channels by voice, either by channel name or channel number. ChatGPT has suggested several alternatives, but none of them worked. At least the last one, the voice assistant recognizes and responds that the request was answered, but nothing happens. Could some kind soul help me fix it?

scripts.yaml:

mudar_canal_por_numero:
  alias: Mudar canal por número
  description: Troca o canal da TV com base no número
  fields:
    canal_numero:
      description: Número do canal
      example: 1
  sequence:
    - variables:
        canal_nome: >
          {% set canais = {
            '1': 'Das Erste HD',
            '2': 'SWR RP HD',
            '3': 'WDR HD Köln'
          } %}
          {{ canais[canal_numero|string] if canal_numero|string in canais else '' }}
    - condition: template
      value_template: "{{ canal_nome != '' }}"
    - service: input_select.select_option
      target:
        entity_id: input_select.tv_channel
      data:
        option: "{{ canal_nome }}"

configuration.yaml:

  tv_channel:
    name: Canal da TV
    options:
      - "Das Erste HD"
      - "SWR RP HD"
      - "WDR HD Köln"
    icon: mdi:television

automations.yaml - for changing chanels voice control:

- id: 'trocar_canal_por_voz'
  alias: Mudar canal por voz
  trigger:
    - platform: conversation
      command:
        - mudar pro canal 1
        - mudar pro canal 2
        - mudar pro canal 3
        - trocar pro o canal 1
        - trocar pro o canal 2
        - trocar pro o canal 3
        - canal 1
        - canal 2
        - canal 3
  action:
    - variables:
        numero: >
          {% if '1' in trigger.command %} 1
          {% elif '2' in trigger.command %} 2
          {% elif '3' in trigger.command %} 3
          {% else %} 0
          {% endif %}
    - service: script.mudar_canal_por_numero
      data:
        canal_numero: "{{ numero }}"
    - service: conversation.process
      data:
        text: Canal {{ numero }} selecionado
  mode: single

automations.yaml - Where the channel changing throug Dashoard already works:

- alias: Trocar canal da TV
  id: trocar_canal_tv
  trigger:
  - platform: state
    entity_id: input_select.tv_channel
  action:
  - service: media_player.play_media
    target:
      entity_id: media_player.lg_webos_tv_oled55b9dla
    data:
      media_content_id: '{{ states(''input_select.tv_channel'') }}'
      media_content_type: channel
  mode: restart

- id: 'trocar_canal_por_voz'
  alias: Mudar canal por voz
  trigger:
    - platform: conversation
      command:
        - mudar pro canal {numero}
        - [trocar pro o ]canal {numero}
  action:
    - service: script.mudar_canal_por_numero
      data:
        canal_numero: "{{ trigger.slots.numero }}"
    - service: conversation.process
      data:
        text: Canal {{ trigger.slots.numero }} selecionado
  mode: single
1 Like

Thanks, but still didn´t work. I changed to english for better understanding.

scripts.yaml


change_number_channel:
  alias: Changing channel by number
  fields:
    channel_number:
  sequence:
    - variables:
        channel_name: >
          {% set channels = {
            '1': 'Das Erste HD',
            '2': 'SWR RP HD',
            '3': 'WDR HD Köln'
            '27': 'Nitro'
          } %}
          {{ channels[channel_number|string] if channel_number|string in channels else '' }}
    - condition: template
      value_template: "{{ channel_name != '' }}"
    - service: input_select.select_option
      target:
        entity_id: input_select.tv_channel
      data:
        option: "{{ channel_name }}"

automations.yaml

- id: 'voice_channel_change'
  alias: Voice channel change
  trigger:
    - platform: conversation
      command:
        - change to {number}
        - change to channel {number}
  action:
    - service: script.change_number_channel
      data:
        channel_number: "{{ trigger.slots.number }}"
    - service: conversation.process
      data:
        text: Channel {{ trigger.slots.number }} selected
  mode: single

configuration.yaml

  tv_channel:
    name: TV channel
    options:
      - "Das Erste HD"
      - "SWR RP HD"
      - "WDR HD Koln"
      - "Nitro"
    icon: mdi:television

The entity “input_select.tv_channel” is working fine on dashboard.
Any help would be apreciated.

You missed a comma.

I would recommend that you start by simplifying your automation construct to a single input_select.select_option action.
You can get the value from the dictionary locally without adding extra variables.

    - service: input_select.select_option
      target:
        entity_id: input_select.tv_channel
      data:
        option: >
          {% set channels = {
            '1': 'Das Erste HD',
            '2': 'SWR RP HD',
            '3': 'WDR HD Köln',
            '27': 'Nitro'
          } %}
          {{ channels[trigger.slots.number]  }}

For tests, you should use the DevTools - Template section and experiment with the solutions from llm.
Start with the simple things, then add the required checks.

{% set trigger = {"slots": {"number": "1"}} %}
  {% set channels = {
    '1': 'Das Erste HD',
    '2': 'SWR RP HD',
    '3': 'WDR HD Köln',
    '27': 'Nitro'
  } %}
{{ channels[trigger.slots.number]  }}

When you are happy with the code, bring it into the automations