Setting volume_level for google home - Message malformed

Good morning HA community, I’m trying to set a google home max volume with a potentiometer attached to an ESPHome device but am running into an issue probably with syntax on my end. HA is letting me know in the Automations tab that my yaml is not correct.

That led me to the developer section where HA doesn’t seem to have a problem with the same bit of code.

My automation yaml

description: ''
trigger:
  - platform: state
    entity_id: sensor.rotary_encoder_3
    id: sensor.rotary_encoder_3
condition: []
action:
  - service: media_player.volume_set
    target:
      device_id: 5a3838ba2c012cf630bbddda0b307ee8
    data:
      volume_level: '{{(((states('sensor.rotary_encoder_3')|float)*(('.01')|float))|string)|round(2)}}'
mode: parallel
max: 10

I’m reading the documentation and it’s stating that volume_level should be a Float, why is HA wanting a string? Thanks.

Because you are converting it to one.

And then attempting to round it.

Also there is no need to convert numbers.

Just write * 0.01

Also use double quotes outside your template if using single quotes inside.

volume_level: "{{ ( states('sensor.rotary_encoder_3')|float(0) * 0.01 )|round(2) }}"

Haha you’re right I was trying to round a string that’s hilarious. Thanks I’ll try this when I get back home.

[UPDATE] This works very well.

I’ve been looking for a good yaml data structure resource? You seem to have a good grasp of it, is that from experience or could you suggest some good resources? Thanks.

See here for the implementation.