Change AVR source based on WebOS source

Hi
I wanted to make automation that would change my AVR (Yamaha) to HDMI2 for specific source on TV (LG CX) and to HDMI1 on any other source. I crafted automation that you see below but it doesn’t work - I’m getting errors in log…I’m clearly doing something wrong here but I’m not sure what exactly.

- id: 'avrsourceontv'
  alias: AVR Source from TV
  description: 'Change AVR source based on TV source'
  trigger:
    - platform: state
      entity_id: media_player.lg_webos_smart_tv_2
      attribute: source
  action:
    - service: media_player.select_source
      data:
        entity_id: media_player.yamaha_receiver
        source: >
          {% if is_state_attr('media_player.lg_webos_smart_tv_2', 'source', 'Panasonic') -%}
            hdmi2
          {%- else -%}
            hdmi1
          {%- endif %}

I think those two errors are relevant to this automation:

2021-02-10 20:00:29 ERROR (MainThread) [homeassistant.components.automation.avr_source_from_tv] AVR Source from TV: Error executing script. Unexpected error for call_service at pos 1:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 254, in _async_step
await getattr(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 464, in _async_call_service_step
await service_task
File "/usr/src/homeassistant/homeassistant/core.py", line 1455, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1490, in _execute_service
await handler.job.target(service_call)
File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
await self.hass.helpers.service.entity_service_call(
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 595, in entity_service_call
future.result() # pop exception if have
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 664, in async_request_call
await coro
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 632, in _handle_entity_call
await result
File "/usr/src/homeassistant/homeassistant/components/media_player/__init__.py", line 640, in async_select_source
await self.hass.async_add_executor_job(self.select_source, source)
File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/src/homeassistant/homeassistant/components/yamaha/media_player.py", line 357, in select_source
self.receiver.input = self._reverse_mapping.get(source, source)
File "/usr/local/lib/python3.8/site-packages/rxv/rxv.py", line 259, in input
assert input_name in self.inputs()
AssertionError
2021-02-10 20:00:29 ERROR (MainThread) [homeassistant.components.automation.avr_source_from_tv] While executing automation automation.avr_source_from_tv
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 404, in async_trigger
await self.action_script.async_run(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1033, in async_run
await asyncio.shield(run.async_run())
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 246, in async_run
await self._async_step(log_exceptions=False)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 254, in _async_step
await getattr(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 464, in _async_call_service_step
await service_task
File "/usr/src/homeassistant/homeassistant/core.py", line 1455, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1490, in _execute_service
await handler.job.target(service_call)
File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
await self.hass.helpers.service.entity_service_call(
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 595, in entity_service_call
future.result() # pop exception if have
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 664, in async_request_call
await coro
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 632, in _handle_entity_call
await result
File "/usr/src/homeassistant/homeassistant/components/media_player/__init__.py", line 640, in async_select_source
await self.hass.async_add_executor_job(self.select_source, source)
File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/src/homeassistant/homeassistant/components/yamaha/media_player.py", line 357, in select_source
self.receiver.input = self._reverse_mapping.get(source, source)
File "/usr/local/lib/python3.8/site-packages/rxv/rxv.py", line 259, in input
assert input_name in self.inputs()
AssertionError

show us then :roll_eyes:

whilst we wait,
if you want to use an attribute state change for the trigger, do so on the template result. Many ways to implement this, one might be:

- alias: whatever
  trigger:
    - platform: template
      value_template: "{{ is_state_attr('<entity>', '<attribute>', ''<required attr vale>) }}"
  action:
    ..

and,
try out the editor Automation editor - Home Assistant

Yeah, sorry for not bringing errors right away but I cleared the logs and only later realized that those are still present but hidden. :roll_eyes:

I was thinking about triggering this way but that would trigger on specific source and I want to trigger on any source change.

As for the editor I do use it to check some combinations and thing like that. I can even edit this automation. What I don’t do is save anything with UI because it tends to make weird things with strings and numbers in automation.yaml like removing quotes from some time triggers.

That looks like an error thrown within the yamaha media_player.py code. Is the source value resolving to a known valid source? Carriage return/LF/space issues?

Try adding the dashes at the front of the ‘if’ and on the last ‘endif’ as below

{%- if is_state_attr('media_player.lg_webos_smart_tv_2', 'source', 'Panasonic') -%}
            hdmi2
          {%- else -%}
            hdmi1
          {%- endif -%}

Use the template editor to check what value this is resolving to, is it a valid source on your TV?

Thanks for the tips. I checked additional dashes but it still didn’t work. But I did more test with Dev Tools to see are the states for all the devices on source change. What I’ve initially noticed was that media_player.lg_webos_smart_tv_2 wouldn’t report source properly. At the same time HomeKit version did (yeah I have two integration for TV)…to some degree - it didn’t change source state when launching any built-in app (Netflix for example). So I restarted whole server and then source state started to work again. Automation also started to run as it should. So I ended up with this:

- id: 'avrsourceontv'
  alias: AVR Source from TV
  description: 'Change AVR source based on TV source'
  trigger:
    - platform: state
      entity_id: media_player.lg_webos_smart_tv_2
      attribute: source
    - platform: state
      entity_id: media_player.yamaha_receiver
      to: 'on'
  condition:
    - condition: state
      entity_id: media_player.yamaha_receiver
      state: 'on'
  action:
    - service: media_player.select_source
      data:
        entity_id: media_player.yamaha_receiver
        source: >
          {%- if is_state_attr('media_player.lg_webos_smart_tv_2', 'source', 'Panasonic') -%}
            HDMI2
          {%- else -%}
            HDMI1
          {%- endif -%}

What I need to figure out now is how to prevent TV from changing source back to HDMI1 because it does that it seems…but that’s something outside of HA scope. :wink: