Retrieving the volume from a Chromecast Audio device

Hi there!

I’m trying to add two buttons to my lovelace UI to volume up and down my Sound Bar. I almost did manage to make it. I tried with volume_up service and it works, but as there’s no way to set the step (it is 0.1 ), it goes too much for my device. Then, I went with a service that is calling volume_level . The logic is pretty simple: just get the current volume and subtract or add a step.

The problem is I can’t get the current value with state_attr('media_player.sound_bar', 'volume_level') . Also states('media_player.sound_bar') is returning off . I’ve noticed it only returns on and the actual volume if home assistant is casting to the sound bar. I can actually set the volume even if not casting, but not get the actual volume.

Have you seen anything like this?

1 Like

You seem to have it figured out. Why not use an input number / slider in your UI? Making do with what is possible.

I believe any media_player entity will only return volume when it is on.

Because it only works one-way. Since I cannot get the current value, I can’t update the slider.

That’s what’s funny! The device is on and I can even set its volume. What happens is that Home Assistant isn’t casting to it – the selected input is my TV. When I start casting to it, everything works and I can get the current volume information.

I have a similar slider, works great. It updates when the player is on. Why would you want to update the slider when nothing is playing? Anyway, my point is just accept that it isn’t perfect and make something workable :slight_smile:

So sorry I wasn’t clear. The fact is my TV is constantly connected to the sound bar and I basically use it for this purpose only. I just want to have a way to update the volume when the TV is on, sending audio to the sound bar.

These are 2 automations I used for a volume slider to control my stereo. This was pre-Lovelace, now I’m just using the Mini Media Player card for controlling my Chromecast Audios. There are 2 automations because 2-way control. This is because the volume could not only be changed from the HA-interface, but also from within the Spotify app.

In your situation, perhaps you can simply use the first automation to push the desired volume from the slider to the soundbar? You can even remove the condition, it would be a simple automation?

In automations.yaml:

- alias: Adjust volume stereo after slider
  trigger:
    platform: state
    entity_id: input_number.stereo_volume
  condition:
    condition: template
    value_template: >    # for preventing log error at boot time when the state is unavailable at first
      {% if not states('media_player.livingroom_audio') in ['unavailable','off'] %}
        {{ (state_attr('media_player.livingroom_audio', 'volume_level') | float * 100) | round(0) != (states('input_number.stereo_volume') | float) | round(0) }}
      {% else %}
        False
      {% endif %}
  action:
    # - service: automation.turn_off
    #   entity_id: automation.bijwerken_slider_na_volume_stereo
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.livingroom_audio
        volume_level: "{{ states('input_number.stereo_volume') | float / 100 }}"
    # - service: automation.turn_on
    #   entity_id: automation.bijwerken_slider_na_volume_stereo

- alias: Adjust slider after volume stereo
  trigger:
    platform: state
    entity_id: media_player.livingroom_audio
  condition:
    condition: template
    value_template: >   # preventing error in log at startup because of unknown state
      {% if not states('media_player.livingroom_audio') in ['unavailable','off'] %}
        {{ (state_attr('media_player.livingroom_audio', 'volume_level') | float * 100) | round(0) != (states('input_number.stereo_volume') | float) | round(0) }}
      {% else %}
        False
      {% endif %}
  action:
    # - service: automation.turn_off
    #   entity_id: automation.bijwerken_volume_stereo_na_slider
    - service: input_number.set_value
      data_template:
        entity_id: input_number.stereo_volume
        value: '{{ (states.media_player.livingroom_audio.attributes.volume_level | float * 100) | round(0) }}'
    # - service: automation.turn_on
    #   entity_id: automation.bijwerken_volume_stereo_na_slider

In configuration.yaml:

input_number:
  stereo_volume:
    name: Stereo
    min: 0
    max: 100
    step: 1
    unit_of_measurement: '%'
    icon: mdi:volume-medium
1 Like

Hm… it’s actually a good idea. Not ideal, of course, but I already managed to make it one way. Why not use just that for now?

Thank you for this hint!

What exactly is this device?

It’s a LG SK6.

Hi Roelof,

Ik ga er voor het gemak even vanuit dat je Nederlands bent, (staat in je profiel) Ik ben nu een paar dagen bezig met home assistent maar begrijp er nog niet heel veel van en hoop dat jij mij kan helpen.
Ik wil graag een knop maken op mijn dashboard die het volume omhoog of omlaag kan doen.
Ik heb jouw code gekopieerd en in de desbetreffende bestanden gezet maar ik weet niet zo goed hoe ik nu verder moet.
Bij voorbaat dank voor je hulp!
groet,

English please.

did you ever get this resolved? I’m having the same problem with a polk soundbar. It is just showing as off when I’m streaming through HDMI, even though I can still control the volume via the service calls. Definitely seems like there should be a way to override the off state value allowing volume slider to be displayed.

i’ve had to use shortcut/buttons

type: 'custom:mini-media-player'
entity: media_player.playroom_firetv
toggle_power: false
group: true
hide:
  volume: true
shortcuts:
  columns: 2
  buttons:
    - icon: 'mdi:volume-medium'
      type: service
      id: media_player.volume_down
      data:
        entity_id: media_player.polk_magnifi_max_6470
    - icon: 'mdi:volume-high'
      type: service
      id: media_player.volume_up
      data:
        entity_id: media_player.polk_magnifi_max_6470