Error in source sync (TV)

Got a working code, but still a few errors to iron out.
I’ve got an input_select for source select TV, which I then sync.
But every once and awhile i get the following error;

Invalid option: (possible options: PC, TV, Wii)

How can i get around this?
Feel like i’m missing something obvious.

media_player:

  ################################################################################
  ## LG WebOS
  ################################################################################

  - platform: webostv
    host: !secret tv_ip
    name: TV
    timeout: 5
    filename: webostv.conf
    customize:
      sources:
        - PC
        - TV
        - Wii

################################################################################
## SELECT SOURCE
################################################################################

input_select:
  tv_source:
    name: 'Source:'
    options:
     - PC
     - TV
     - Wii
    icon: mdi:login-variant

################################################################################
## SENSORS
################################################################################

sensor:

  - platform: template
    sensors:
      tv_source:
        friendly_name: "TV Source"
        value_template: "{{ states.media_player.tv.attributes.source 

automation:
  ################################################################################
  ## SOURCE SYNC
  ################################################################################

  - alias: 'TV Source (Sync)'
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sensor.tv_source
    action:
      service: input_select.select_option
      entity_id: input_select.tv_source
      data_template:
        option: '{{  states.sensor.tv_source.state  }}'

Template sensors will often have a state of ‘unknown’ for a short time when HA starts. Try adding a condition to your automation that checks if the value of the sensor is valid for the input_select.

1 Like

Thanks for your reply, and putting me on the right track.
Still a newbie, so much trial and error, but beginning to understand it.

I’ve added a condition to the automation.
That the TV state must be ‘playing’.

And changed the sensor to an is_state.
As described in: https://www.home-assistant.io/components/sensor.template/ - considerations

As far as i’ve tested it works!
Again, thanks very much!

sensor:

  - platform: template
    sensors:
      tv_source:
        friendly_name: "TV Source"
        value_template: "{{ is_state('media_player.tv.attributes.source', 'on') }}"

  ################################################################################
  ## SOURCE SYNC
  ################################################################################

  - alias: 'TV Source (Sync)'
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sensor.tv_source
    condition:
      - condition: template
        value_template: "{{ is_state('media_player.tv', 'playing') }}"
    action:
      service: input_select.select_option
      entity_id: input_select.tv_source
      data_template:
        option: '{{  states.sensor.tv_source.state  }}'

I can see a few things in your latest code that you might want to consider changing.

First, is_state returns true or false. I don’t think you were looking to have sensor.tv_source evaluate to either true or false. (Although, if you did, you should probably make it a binary_sensor instead of a sensor.) Assuming the source attribute of media_player.tv is indeed one of PC, TV or Wii, then I think the way you had sensor.tv_source in the first place makes more sense. Although I would use the state_attr function:

sensor:
  - platform: template
    sensors:
      tv_source:
        friendly_name: TV Source
        value_template: "{{ state_attr('media_player.tv', 'source') }}"

Second, for the condition, I was thinking more along the lines of checking the current state of sensor.tv_source. Something like this:

automation:
  ################################################################################
  ## SOURCE SYNC
  ################################################################################

  - alias: 'TV Source (Sync)'
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sensor.tv_source
    condition:
      condition: template
      value_template: >
        {{ states('sensor.tv_source') in
             state_attr('input_select.tv_source', 'options') }}
    action:
      service: input_select.select_option
      entity_id: input_select.tv_source
      data_template:
        option: "{{  states('sensor.tv_source')  }}"
1 Like

That does it!
I was a bit too quick to mark it as resolved.

Also had another problem:
When tv wasn’t playing, selecting the source would give an error.
This is also resolved with your code.

I get the first part, makes more sense than calling the is_state.
The condition for the automation is really great.

One question though, in action:

Why do you use
option: “{{ states(‘sensor.tv_source’) }}”

Instead of
option: ‘{{ states.sensor.tv_source.state }}’

Is it just a difference in formatting?
Thanks for helping me with this. Really appreciate it.

No, normally they will result in exactly the same value. However, the former will not cause an error if the entity doesn’t exist yet; it will just return ‘unknown’ in that case. The latter will cause an error in the same situation. The former is slightly shorter and less to type as well. :wink:

1 Like

Less to typ, cleaner code! :smiley:

Maybe you could / want to help me with similar error.
Besides source select, i’ve got volume control for the TV and spotify.
It works 2-way. Can change volume on TV and HA, and it get’s synced.
But both of them give the same error.

Error executing service <ServiceCall media_player.volume_set: entity_id=[‘media_player.tv’], volume_level=0.0>
Error executing service <ServiceCall media_player.volume_set: entity_id=[‘media_player.spotify’], volume_level=0.0>

It errors out on the ‘volume.set’ attribute.
Could it be that when tv is off, ‘0 / 100’ would return an error?

Is it correct to resolve this, by adding this condition to the automation:

condition:

  • condition: state
    entity_id: media_player.tv
    state: ‘playing’

making it:

      ################################################################################
      ## VOLUME SET
      ################################################################################

      - alias: 'TV Volume (Set)'
        trigger:
          platform: state
          entity_id: input_number.tv_volume
        condition:
          - condition: state
            entity_id: media_player.tv
            state: 'playing'
        action:
          service: media_player.volume_set
          data_template:
            entity_id: media_player.tv
            volume_level: '{{  states.input_number.tv_volume.state | int / 100  }}'

This is the code for TV:

################################################################################
## VOLUME
################################################################################

input_number:
  tv_volume:
    name: Volume
    icon: mdi:volume-high
    initial: 20
    min: 0
    max: 40
    step: 1

################################################################################
## SENSORS
################################################################################

sensor:

  - platform: template
    sensors:
      tv_volume:
        friendly_name: "TV Volume"
        value_template: "{{ state_attr('media_player.tv', 'volume_level') }}"

################################################################################
## AUTOMATION
################################################################################

automation:

  ################################################################################
  ## VOLUME SET
  ################################################################################

  - alias: 'TV Volume (Set)'
    trigger:
      platform: state
      entity_id: input_number.tv_volume
    action:
      service: media_player.volume_set
      data_template:
        entity_id: media_player.tv
        volume_level: '{{  states.input_number.tv_volume.state | int / 100  }}'

  ################################################################################
  ## VOLUME SYNC
  ################################################################################

  - alias: 'TV Volume (Sync)'
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sensor.tv_volume
    action:
      - delay:
          seconds: 5
      - service: input_number.set_value
        data_template:
          entity_id: input_number.tv_volume
          value: '{{  states.sensor.tv_volume.state | float | round(2) * 100  }}'

Sorry, I don’t see any obvious reason why you’re getting those errors. Is there any additional, useful information in the HA log?

Although I don’t think this has anything to do with the errors, for input_number.tv_volume you have max set to 40. Shouldn’t that be 100?

Yeah there is:

Error executing service <ServiceCall media_player.volume_set: entity_id=[‘media_player.tv’], volume_level=0.0>
Traceback (most recent call last):
File “/usr/local/lib/python3.6/site-packages/homeassistant/core.py”, line 1038, in _event_to_service_call
await service_handler.func(service_call)
File “/usr/local/lib/python3.6/site-packages/homeassistant/components/media_player/init.py”, line 441, in async_service_handler
await getattr(player, method[‘method’])(**params)
File “/usr/local/lib/python3.6/concurrent/futures/thread.py”, line 56, in run
result = self.fn(*self.args, **self.kwargs)
File “/usr/local/lib/python3.6/site-packages/homeassistant/components/media_player/webostv.py”, line 317, in set_volume_level
self._client.set_volume(tv_volume)
File “/usr/local/lib/python3.6/site-packages/pylgtv/webos_client.py”, line 312, in set_volume
‘volume’: volume
File “/usr/local/lib/python3.6/site-packages/pylgtv/webos_client.py”, line 199, in request
self.command(‘request’, uri, payload)
File “/usr/local/lib/python3.6/site-packages/pylgtv/webos_client.py”, line 193, in command
loop.run_until_complete(asyncio.wait_for(self._command(message), self.timeout_connect, loop=loop))
File “uvloop/loop.pyx”, line 1448, in uvloop.loop.Loop.run_until_complete
File “/usr/local/lib/python3.6/asyncio/tasks.py”, line 362, in wait_for
raise futures.TimeoutError()
concurrent.futures._base.TimeoutError

I deliberately did that.
TV Volume goes to 100, but 40 is already really loud, so i maxed the setting at 40 :hear_no_evil::wink:

Well, the error certainly is a timeout error which appears to happen when trying to send the command to the TV. So, yes, I’d say you shouldn’t try setting the volume when the TV won’t respond (i.e., when it’s off I guess.) Since this is specific to a media_player platform I don’t use (webostv), you’ll just have to figure out under what conditions the media_player.volume_set service will work, and come up with an appropriate condition for your automation.

I suppose that prevents someone from changing the input_number to a value above 40 via the HA interface, but what if the volume is turned up beyond that at the TV itself - i.e., when your automation will try to set the input_number accordingly?

That is correct.
Thanks, going to find that out.

Got a point.
But, I have this TV 2 years and not once set the volume higher than 30.
So the convenience of not being able to give the slider a yank / accidentally (read:the wife) putting it to 100. That outweighs the possible error i could get when manually setting volume to 45, and HA not being able to sync it’s value.

Understood. But you could let the input_number go all the way to 100, then in your automation that sets the TV volume, cap the value that is actually sent to 0.4. It could even immediately set the input_number back to 40, too.

One thing you have to be careful about with these automations is feedback. I.e., the input_number gets changed, so it changes the TV volume. The TV volume changes accordingly, then that causes the input_number to get updated, too. But if there’s any difference, this could go on forever. (E.g., input_number goes to 27, which changes TV to 0.27, but TV actually changes to 0.26, which causes input_number to get changed to 26, which causes TV to be set to 0.26, but…) I think you’ve already considered this, especially given the delay you’ve included. But I just thought I’d point it out in case.

I can confirm that adding the condition that the tv / spotify must be playing, resolves the issue of volume_set!

  ################################################################################
  ## VOLUME SET
  ################################################################################

  - alias: 'TV Volume (Set)'
    trigger:
      platform: state
      entity_id: input_number.tv_volume
    condition:
      - condition: state
        entity_id: media_player.tv
        state: 'playing'
    action:
      service: media_player.volume_set
      data_template:
        entity_id: media_player.tv
        volume_level: '{{  states.input_number.tv_volume.state | int / 100  }}'

Ok, that’s an option. I can try it out.
Thanks for pointing that out, I’ve played around with different values of delay, to get this right.