Availability template - is number and greater than 0

Hi,

Is it possible to define an availability template that checks if the sensor is a number and is also greater than zero?

          availability:
            "{{ (states('sensor.emporiavue2_circuit_10_power')|is_number )}}"

Would something like this be correct?

          availability:
            "{{ (states('sensor.emporiavue2_circuit_10_power')|is_number and states('sensor.emporiavue2_circuit_10_power')|float > 0) }}"

The Jinja2 template is fine but the YAML needs minor corrections.

Like this:

availability: >
  {{ states('sensor.emporiavue2_circuit_10_power')|is_number and states('sensor.emporiavue2_circuit_10_power')|float > 0 }}

Or like this:

availability: "{{ states('sensor.emporiavue2_circuit_10_power')|is_number and states('sensor.emporiavue2_circuit_10_power')|float > 0 }}"

Reference:

Important template rules


NOTE

For improved legibility, you may wish to consider doing this:

availability: >
  {% set ecp = states('sensor.emporiavue2_circuit_10_power') %}
  {{ ecp|is_number and ecp|float > 0 }}
1 Like

You don’t need the is_number test. Just use the float filter with a default of 0. If it is not a number the value will be replaced with zero:

availability:  "{{ states('sensor.emporiavue2_circuit_10_power')|float(0) > 0 }}"
2 Likes

Actually I want the sensor to become unavailable if the number is not greater than 0. Wouldn’t it be better to prevent some errors?

That’s what my template does.

If the source sensor is unavailable the value is set to zero → template = false → unavailable.

If the source sensor is < 0 → template = false → unavailable.

It prevents all errors you asked for.

1 Like

Ok, great info. Thanks!