[Solved] Can't set MPD volume

I’m using MPD for local TTS and sounds through USB speakers attached to the Windows 10 computer where I have HA running on VirtualBox, and it works well, except I can’t seem to set the volume for it.

I call media_player.volume_set in my script, but it just gets ignored, and MPD always defaults to 85% when playing sound, and 0% when it is not.

Here is the script that I use. The sound and the message are are played properly, but even though the trace shows that the sound level should have been set it always is around 85%.

alias: announce
sequence:
  - service: media_player.volume_set
    target:
      entity_id: media_player.mpd
    data:
      volume_level: '{{ states(''input_number.volume'') | float }}'
  - service: media_player.play_media
    target:
      entity_id: media_player.mpd
    data:
      media_content_id: http://192.168.1.99:8123/local/sounds/{{type}}.mp3
      media_content_type: audio/mp3
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 750
  - service: tts.cloud_say
    data:
      entity_id: media_player.mpd
      language: en-US
      message: '{{ message }}'
      options:
        gender: female
mode: single
icon: mdi:bullhorn

And now the trace that shows it SHOULD be setting the volume:

Result:
params:
  domain: media_player
  service: volume_set
  service_data:
    volume_level: 0.1
    entity_id:
      - media_player.mpd
  target:
    entity_id:
      - media_player.mpd
running_script: false
limit: 10

Can anyone see what I am doing wrong?

For anyone else that finds this thread in the future I found out why this doesn’t work. Apparently, for no reason I can fathom, a sound must be playing for MPD’s volume to be adjusted. Moving the volume down in the script to the second place works. It does change it fast enough that you don’t get sound at whatever the old volume level was set at, but it still makes no sense to me (even though I decided to try it this way, and it does work).

So now my working script looks like this:

alias: announce
sequence:
  - service: media_player.play_media
    target:
      entity_id: media_player.mpd
    data:
      media_content_id: http://192.168.1.99:8123/local/sounds/{{type}}.mp3
      media_content_type: audio/mp3
  - service: media_player.volume_set
    data_template:
      entity_id: media_player.mpd
      volume_level: '{{ states(''input_number.volume'') | float }}'
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 750
  - service: tts.cloud_say
    data:
      entity_id: media_player.mpd
      language: en-US
      message: '{{ message }}'
      options:
        gender: female
mode: single
icon: mdi:bullhorn

Moving the volume setting to the second service called (after the mp3) fixes it. Doesn’t make sense to start the sound, then adjust volume, but that is the only way it seems to work.

Leaving this here in case anyone else runs into this in the future.