Set media player volume before playing?

Hey Everyone!

I have a Chromecast Audio and I am trying to set the media player volume before the media player starts playing. I can pull up the device in the native Google Home app, view the current volume level, adjust the volume to whatever I want, connect to it with a media source (Pandora, Spotify, whatever), and then start playing music.

I would like to do the same thing using Home Assistant, but I run into an error when attempting to view and set the volume using media_player.volume_set: “media player has no attribute ‘volume_level’”

It appears that if the media player is not playing, the media player has no idea what volume it is set to since it is in the state of “off” and the attribute ‘volume_level’ does not exist when “off”. The native Google Home app does not have this limitation; it can view and adjust the volume when no media is playing. Everything in my automation script works fine in Home Assistant once the media player starts playing since the ‘volume_level’ attribute is then available.

Is there a way to know the current volume level and set a media player’s volume before the media connects and starts playing (like the Google Home app does)?

Thanks again for being awesome!

1 Like

I’m also using CCA’s and this works for me.

service: media_player.volume_set
data:
  volume_level: 0.15
target:
  device_id:
    - 668965c149bc9c7c3d890660f4a1417f
    - 281bb8b2099a0c167aaf55ed64389679
    - 0af83191d902d42c1ff268c0884bdc83
    - 17ba68ecae723f55f77c1e50a41dc286

EDIT:
I don’t think you can ‘view’ the level before setting or playing, as I think the attribute is created when setting or playing.

Interesting, I will try that.

How would I get the media player’s current volume before playing? I would like to set the volume only if it is louder or quieter than certain thresholds.

Thanks again!!

I set at 0, wait a wait a few seconds the move to the desired volume. Crude, but I’ve not found another way.

I would like to set the volume to .15 if it is currently <.15 and set to .25 if it is >.25 before playing. Is there a reason the attributes are available only when playing and not before? The Google Home app has access to them before playing.

I have no idea why that seems to be the case.

  1. Go to Developer Tools > States and locate your speaker.
  2. Ensure it is off.
  3. It has no volume attribute when it’s off.
  4. Without a volume attribute, you can’t check the speaker’s current volume level.

If you call media_player.turn_on, the volume attribute will be available. In other words, as long as the state is something other than off, you can access the attribute.

1 Like

Ahh, so I would have to add an extra step:

  1. Turn on media player
  2. Get volume level
  3. Adjust if needed
  4. Connect media source
  5. Start playing media

I guess this is a workaround. It’s still odd that the values aren’t available for Home Assistant but are available for Google Home.

Thanks again!!

Step 0 can be a test to determine if the speaker is off before proceeding to turn it on. Similarly, step 2.5 can be a test to determine if the current volume is not the desired volume. These tests can be implemented with choose.

Well dang it, that didn’t work. I manually set the state to various not “off” values, but none of them would reveal any other details of the media player, specifically volume_level; the attributes were exactly the same as “off” when the state was manually set to not “off”.

Should this be a feature request? The values are available to not Home Assistant (Google Home), so there should be a way to make them available to Home Assistant.

Thanks again for your help!

It might be a characteristic of the Chromecast Audio or how Home Assistant interacts with it.

What I had described above was tested with an off-brand device (battery powered weatherproof speaker supporting Chromecast). For all intents and purposes, it’s a portable Google Mini. Turning it on sets its state to idle and causes the volume attribute to appear (and a few others).

With the newest. Version of HA 22.6

 - service: media_player.play_media
    target:
      entity_id: media_player.nestmini5944
    data:
      media_content_id: media-source://radio_browser/ba572a84-0c64-48e5-ba57-18ad22614d70
      media_content_type: audio/mpeg
    metadata:
      title: A MISSISSIPPI BLUES
      thumbnail: >-
        https://d1ujqdpfgkvqfi.cloudfront.net/favicon-generator/htdocs/favicons/2022-03-06/20e1e13b9560245cced8a320508ace70.ico.png
      media_class: music
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://radio_browser
        - media_content_type: music
          media_content_id: media-source://radio_browser/tag
        - media_content_type: music
          media_content_id: media-source://radio_browser/tag/blues
  - service: media_player.volume_set
    data:
      volume_level: 0.24
    target:
      device_id: 0af84fce0987264fd057f7e7638b9b7e
  - delay:
      hours: 1
      minutes: 0
      seconds: 0
      milliseconds: 0
  - service: media_player.media_stop
    data: {}
    target:
      device_id: 0af84fce0987264fd057f7e7638b9b7e
mode: restart

2 Likes

Yes, I ended up doing this for my media players. Hopefully this will help someone who stumbles upon this thread.

alias: Kitchen Chromecast Audio set default volume when started
trigger:
  - platform: state
    entity_id:
      - media_player.kitchen
    from: 'off'
condition: []
action:
  - service: media_player.volume_set
    data:
      volume_level: 0.33
    target:
      entity_id: media_player.kitchen
mode: single

7 Likes

So this is exactly what I was looking for thanks! Question- for my application I need the speaker to bump up the volume to 8 but after the automation runs I would like it to go back to where it was, 50% for example, I am sure it is not that hard but I am struggling to figure it out- Any idea how to do this?

If you want your automaton to dynamically adjust the volume, you’d need the automaton to read and store the volume value in a text helper, bump up the volume, do the thing, then reset the volume to the stored value from the helper when the thing is done. (I do something similar with storing other state values for future related automaton uses.)

I hope this helps!

1 Like

Thanks. Exactly what i was searching for.

1 Like

When I wake up with a sound system loud, windows almost break, because mediaplayer started with volume on ‘1’, yes thanks, my wife honoured you now, :crazy_face: