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
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 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}}
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.
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 %}