Default value in number selector not working, results in "expected float" or "UndefinedError"

Hi,

I’m using a number selector in my script with a default value of 0.5 to set the media player volume before sending a TTS message. It shows the default value when I add the script to an automation in the UI but it’s not actually working. If I leave it on default value HA will tell me:

expected float for dictionary value @ data['volume_level']. Got None

When I change the slider it works as expected.

This is the input field:

    tts_volume_level:
      name: Message volume
      description: Volume level at which to play the TTS message.
      required: true
      default: 0.5
      selector:
        number:
          min: 0.1
          max: 1
          step: 0.1

This is where I use the data:

  - service: media_player.volume_set
    data_template:
      entity_id: "{{ entity_id }}"
      volume_level: "{{ tts_volume_level }}"

It looks like media_player.volume_set expects the volume_level to be a number/float while I’m delivering a string. But I’m stumped at why it does work when changing the value with the slider?

I also tried:

  - service: media_player.volume_set
    data_template:
      entity_id: "{{ entity_id }}"
      volume_level: "{{ tts_volume_level | float }}"

But then HA tells me:

Error rendering data template: UndefinedError: 'volume_level' is undefined

Can anyone tell me where my logic is failing?

I am far from an expert, but this is a part of a script I use. “input_number.speech_volume.state” is the helper I use to set the volume. This script is called by another script that passes a value for “where”

service: media_player.volume_set
data_template:
  entity_id: "{{ 'media_player.' ~ where }}"
  volume_level: >-
    {%- set vol = states.input_number.speech_volume.state | int -%} {%- set
    adjvol = vol / 100 -%} {%- set newvolume = adjvol | float -%} {{newvolume}}

Not sure how you are setting entity_id though.

I’m using another selector for the entity_id


    entity_id:
      name: Mediaplayer entity
      description: Choose targeted media player entity id
      required: true
      selector:
        entity:

My script sets the volume correctly as long as I’ve actually changed the value by clicking the slider. It just doesn’t work when I leave it on the default value. Changing it to 0.6 and then back to 0.5 also works. So it’s not the value itself that’s problematic, it’s the way the default value is passed to the volume_set service.

Could it be that using entity_id as the variable is the issue? Maybe change to something else and see?

Other than that. I am out of my depth here. Sorry.

Tried that already, made no difference. Seeing as it does set the volume correctly when it’s a non-default value it would have been a surprise :wink:

It seems the default value for the slider is just for show and is not actually passed along. I created a workaround by using an if/else statement in the template to set the default value if nothing is set.

  - service: media_player.volume_set
    data_template:
      entity_id: "{{ entity_id }}"
      volume_level: >-
        {% if volume_level is defined %}
        {{ volume_level }}
        {% else %}
        0.5
        {% endif %}