Float needs default value

I updated to 2021.10.0 and now getting a warning that I need a default value for my template. Basically there is a bug I find with my Ecobee that it keeps changing to a super low volume. So whenever it drops below 80% volume I force it back up. Use the below:

- alias: "Media: Monitor and set ecobee volume to 8"
  trigger:
    - platform: template
      value_template: "{{ state_attr('media_player.ecobee','volume_level') | float < 0.8 }}"
  action:
    - service: media_player.volume_set
      entity_id: media_player.ecobee
      data_template:
        volume_level: 0.8

What would I need to set a default of?

Basically you just need to tell it a number to use if the volume level returns something non-numeric. If you want it to try to set the volume if it gets an unusual volume level like “unknown”, set it below your threshold so it will default to triggering the action you have set up. If you want to kill the automation if the volume level is unusual, set the default above your 0.8 threshold.

{{ state_attr('media_player.ecobee','volume_level') | float(default=0) < 0.8 }} }}
1 Like

Whatever value you want. It represent the value float will report in the event it is unable to convert the value of volume_level to a floating-point number.

For example, what do you think float will do if this is unavailable or unknown?

state_attr('media_player.ecobee','volume_level')

In previous versions, anything it could not convert was reported as 0. If you didn’t want 0 you could specify your own default value as shown here. It will report -1 because it cannot convert the string 'foo' to a number.

{{ 'foo' | float(-1) }}

The difference in 2021.10.0 is that you must specify a default value. If you don’t, you get a warning message. In 2021,12.0 you will get an error message.

So if you want float to report 0 for things it can’t convert, explicitly indicate it:

{{ 'foo' | float(0) }}

Likely, here, the issue is that media_player.ecobee is not available, for whatever reason (startup?).
So the default should be above the threshold, to not execute the action, that will very likely fail.