Input helper number not a float?

Hi All,

I’m trying to store the current volume level for my google home devices before I broadcast a message to restore it to the previous level afterwards. But somehow this doesn’t work. I get the message the volume set expects a float.

Could it be that the input helper for number is not a float?

The error message I get is :

2021-01-21 19:56:19 ERROR (MainThread) [homeassistant.components.script.var_test] var test: Error executing script. Invalid data for call_service at pos 4: expected float for dictionary value @ data['volume_level']
2021-01-21 19:56:19 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [139884269493984] expected float for dictionary value @ data['volume_level']

Here’s my code. Any help welcome.

alias: var test
mode: single

sequence:
  - service: input_number.set_value
    entity_id: input_number.google_home_volume
    data:
      value: "{{ state_attr('media_player.google_home_study', 'volume_level') | float }}"
  - service: media_player.volume_set
    data:
      volume_level: 0.3
    entity_id: media_player.google_home_study
    
# tts message goes here    

  - delay:
      seconds: 2
  - service: media_player.volume_set
    data:
      volume_level: input_number.google_home_volume
    entity_id: media_player.google_home_study

Paste this into the template editor of Developer Tools to check the output.

Thanx!

I know better than not the check that. But after checking it…

Result type is a number… which is puzzling.

Try using data_template instead of data in your first sequence.

Good one!

… but that didn’t help either.

This is the erroneous line, correct? Maybe addressing an input-helper is not supported.

Try

volume_level: {{ states('input_number.google_home_volume') }}

Yes, that is the line that gives me trouble. You’re suggestion is very creative. Tried it, but still a no go.

I’m puzzled by this, should be so easy.

The error message seems to be complaining about this line

data:
      value: "{{ state_attr('media_player.google_home_study', 'volume_level') | float }}"

As you stated the key is a float, but by putting double quotes around the template you turned it into a string I believe. Remove the double quotes and try that FWIW. Also, some key:value pairs will only accept a float, not a template, so not sure this will fix it. You could also try service_data: then your template.

HA stores all values as strings, you then have to convert them
Recently HA has been trying to ‘interpret’ dimensions from the values stored and convert them back but there are sometimes issues
You should always test in the template editor.

The question I have here from the above is how did you set up the input number and what it’s step value is compared to the values displayed in the template edit (you say you always test to) ie does it show 3.00 or just 3.0.?

data_template was deprecated in favor of data several versions ago.
data now serves the same function as data_template did.

No, in fact the double-quotes are required otherwise, without them, the entire line will be reported as an error.


The error message is complaining about volume_level expecting a float value but it failed to receive one. That’s obvious from here:

  - service: media_player.volume_set
    data:
      volume_level: input_number.google_home_volume. <---- This
    entity_id: media_player.google_home_study

m0wlheld had the right idea, just overlooked to wrap the template in double-quotes.

  - service: media_player.volume_set
    data:
      volume_level: "{{ states('input_number.google_home_volume') }}"
    entity_id: media_player.google_home_study
1 Like

Thanx for all the input and help. Much appreciated. Got it working with all the input you guys gave me.

Final working code :

var_test:
  alias: var test
  sequence:
  - service: input_number.set_value
    data:
      entity_id: input_number.google_home_volume
      value: "{{ state_attr( 'media_player.google_home_study', 'volume_level' ) | default(0) | float }}"
  - service: media_player.volume_set
    data:
      volume_level: 0.3
    entity_id: media_player.google_home_study
  - delay: 00:00:02
  - service: media_player.volume_set
    data:
      entity_id: media_player.google_home_study
      volume_level: "{{ states( 'input_number.google_home_volume' ) }}"
  mode: single

Thanx for your elaborate reply. Very much appreciated!

1 Like

You’re welcome!

You should know that marking your own post as the Solution is reserved for the situation where you actually solved it yourself.

It’s the custom of this community to mark the first post that offers, or leads to, a solution to the problem. Many people offered assistance and at least one of their responses provided a solution. That’s the post that should be marked with the Solution tag.

3 Likes

Oke, didn’t know that. I’ll keep that in mind.

So move the solution !

Oke, now that is a bit blunt.

I’m trying to find my way in this forum. If there is common decency on how to tag a solution I’ll oblige. But for now, I don’t even know what : ‘So move the solution !’ even means…

Taras already explained.
Each topic can have only one solution.
You can tick any post in the thread as the solution.
You did none of the work but ticked your own post as the solution.
Think about that.
If everyone ticked their own posts as solutions then why are the rest of us here ?
Besides it’s just polite to acknowledge someone’s efforts
Find the post that helped you most and tick that.
ie Move the ‘solution’

I really think you are too much after that solution tick. Let them do what they like, who cares. It’s not a lie that that post leads to the final solution for someone who doesn’t want to read the entire thread.

No, I didn’t even know I was in the running, Taras gave the full explanation and gave the most guidance. I have no problem with him or anyone else for that matter taking the win.

Let me actually read the thread again.

Back in a moment.

Edit: No, I definitely do not deserve the solution, but outline for me why you think, anyone but Taras, does ?

For me it’s not about winning, it’s about somebody coming to this thread and finding the solution as fast as possible. If you summarize in a post the solution and mark it, it’s ok to me. It’s not a matter of who deserves it or who doesn’t, it’s about putting things as simple as possible for other people who haven’t necessarily followed the whole thread or aren’t willing to.

it’s just as good if you mark Taras’ or yours

1 Like