I’m using the Universal Media Player to automate a multi-zone amp that I’ve exposed over MQTT with a server I built. I have it working fairly well, but the media_player
entity does not have a volume_level
attribute and the volume slider in the UI is not reflecting the true volume. I can change the volume successfully with the volume slider, but it always defaults to zero and changes to the number entity I use to control the volume over MQTT do not reflect in the slider. What’s required to get the volume_level
attribute so that it will sync with the slider in the UI? Here is my setup:
- platform: universal
name: Office Zone
children:
- media_player.whole_house_speaker
commands:
turn_on:
service: switch.turn_on
data:
entity_id: switch.office_speakers
turn_off:
service: switch.turn_off
data:
entity_id: switch.office_speakers
volume_up:
service: number.set_value
data_template:
entity_id: number.office_volume
value: '{{ (states.number.office_volume.state | int) + 2 | round(0) }}'
volume_down:
service: number.set_value
data_template:
entity_id: number.office_volume
value: '{{ (states.number.office_volume.state | int) - 2 | round(0) }}'
volume_set:
service: number.set_value
target:
entity_id: number.office_volume
data:
value: '{{ ((0 - (-80)) * (volume_level - 0) / (1 - 0)) + -80 }}'
select_source:
service: mqtt.publish
data:
topic: devices/audio-zone/14/input/set
payload_template: '{{ source }}'
attributes:
state: switch.office_speakers
#Scale this to a percentage 0-1
volume_level: >
{{ (((1 - 0) * (states('number.office_volume')|default(-80)|int - (-80)) / (0 - (-80))) + 0) | float }}
source_list: input_select.audio_sources|options
source: sensor.office_speakers_input
Note that the MQTT server accepts the volume as dB gain from -80 to 0 so I have templates to scale the volume from 0…1.
Here’s the entity state:
source_list:
- '00'
- '01'
- '02'
- '03'
- '04'
- '05'
- '06'
- '07'
- '08'
- '09'
is_volume_muted: false
source: '09'
friendly_name: Office Zone
supported_features: 3460
And I’ve tested the template I’m using for volume_level and it’s working, returning the right value from 0…1.