Availability_template - does not work?

There is a sensor:

        value_template: >-
          {{ (states('sensor.rpi_1_netdata_cpu_softirq') | float +
              states('sensor.rpi_1_netdata_cpu_user') | float +
              states('sensor.rpi_1_netdata_cpu_system') | float +
              states('sensor.rpi_1_netdata_cpu_iowait') | float +
              states('sensor.rpi_1_netdata_cpu_irq') | float +
              states('sensor.rpi_1_netdata_cpu_nice') | float +
              states('sensor.rpi_1_netdata_cpu_steal') | float) | round(1) }}

        availability_template: >-
          {% set SOURCE_SENSOR_MASK = "sensor.rpi_1_netdata_cpu_" -%}
          {{ not states(SOURCE_SENSOR_MASK + "softirq") in ["unavailable","unknown"] and
             not states(SOURCE_SENSOR_MASK + "user") in ["unavailable","unknown"] and
             not states(SOURCE_SENSOR_MASK + "system") in ["unavailable","unknown"] and
             not states(SOURCE_SENSOR_MASK + "iowait") in ["unavailable","unknown"] and
             not states(SOURCE_SENSOR_MASK + "irq") in ["unavailable","unknown"] and
             not states(SOURCE_SENSOR_MASK + "nice") in ["unavailable","unknown"] and
             not states(SOURCE_SENSOR_MASK + "steal") in ["unavailable","unknown"] }}

It returns a total CPU load retrieved from Netdata.

When Netdata node is unavailable, then there are warnings in Log:

2021-11-27 01:43:35 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unavailable' when rendering template '{{ (states('sensor.rpi_1_netdata_cpu_softirq') | float +
states('sensor.rpi_1_netdata_cpu_user') | float +
states('sensor.rpi_1_netdata_cpu_system') | float +
states('sensor.rpi_1_netdata_cpu_iowait') | float +
states('sensor.rpi_1_netdata_cpu_irq') | float +
states('sensor.rpi_1_netdata_cpu_nice') | float +
states('sensor.rpi_1_netdata_cpu_steal') | float) | round(1) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1

But I provided availability_template to avoid this!
Why do these errors occur?

1 Like

Templates resolve no matter what. Next version the availability will actually stop the state from resolving.

Thank you Petro,

Does that mean:

  1. Now I should provide a check with using “is_number()” in “value_template”.
  2. Starting from 2021.12 this check will not be necessary since “availability_template” will be executed first and if it returns FALSE then “value_template” will not be executed.

?

It’s just a warning until 2021.12, so you might as well live with it until then, where it shouldn’t be an issue anymore.

Not clear what you mean.
After 2012.12 it will be not a warning - it will be an error.
Any warning means “look at this code with attention”.

It won’t execute the value_template, so there’s nothing to worry about. Ignore it.

Will it happen at 2021.12 ?

I feel were going in circles and I’m saying the same thing. In 2021.12 value_template will not resolve when availability is unavailable.

You said “next version” - and I am clarifying “is it 2021.12”.

Unrelated to the original question but might be useful to you:

Create a group containing the 7 sensors. It will allow you to simplify the templates.

expand('group.net_data') | map(attribute='state') | map('float', 0) | sum | round(1) }}
expand('group.net_data') | selectattr('state', 'in', ['unavailable', 'unknown']) | list | count == 0 }}
1 Like

Great, Taras!))