Hi guys,
I’m trying to use a workaround for a wifi speaker’s volume control in HomeKit using a template light.
I have the actual input change for change volume working using the brightness, my issue is that the ‘light’ power status and ‘brightness’ status doesn’t sync up with the media player attributes.
If I tell Siri to ‘set Sony speaker to 20%’, then the volume will be changed. But I want home app to know the speaker is already on and its current level. I had an unfortunate error at 3am experimenting with code, I turned the light on in the home app, it sprung to 100% volume. Very uncool in an apartment
Seems these error is preventing:
Update for light.sony_speaker fails
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 221, in async_update_ha_state
await self.async_device_update()
File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 347, in async_device_update
await self.async_update()
File "/usr/local/lib/python3.6/site-packages/homeassistant/components/light/template.py", line 257, in async_update
if 0 <= int(brightness) <= 255:
UnboundLocalError: local variable 'brightness' referenced before assignment
&
UndefinedError: 'None' has no attribute 'attributes'
Here is my code, tweaked from the template light component docs which also doesn’t seem to work out of the box with the entity ids changed.
- platform: template
lights:
sony_speaker:
friendly_name: "Sony Speaker"
value_template: "{{ is_state('switch.sony_speaker', 'on') }}"
level_template: "{{ states.media_player.sony_speaker.attributes.volume_level|float * 255)|int }}"
turn_on:
service: media_player.turn_on
data:
entity_id: media_player.sony_speaker
turn_off:
service: media_player.turn_off
data:
entity_id: media_player.sony_speaker
set_level:
service: media_player.volume_set
data_template:
entity_id: media_player.sony_speaker
volume_level: "{{ (brightness / 100 * 100)|int / 100 }}"
level_template: >-
{% if is_state('media_player.sony_speaker', 'on') %}
{{ (states.media_player.sony_speaker.attributes.volume_level|float * 100)|int }}
{% else %}
0
{% endif %}
If I remove the volume part, then the power syncs up as expected
Should I be creating a sensor for the volume as set by the speaker to reference initially?
Any help would be appreciated
Thank you
Linton