Availability is_number not working with more that one sensor

When using the the Availability check with more than one sensor the first one will not work of the value is not a number, it even says that its “true” if i use a sensor that not even exists.

Is this a bug or did I do something wrong?

  # Power Consumption Grid Netto
  - name: "Power Consumption Grid"
    unique_id: "template_power_consumption_grid"
    unit_of_measurement: "W"
    device_class: power
    state_class: measurement
    icon: "mdi:transmission-tower"
    state: >
      {% set PowerConsumption = states('sensor.electricity_meter_energieverbruik_w_2') | float %}
      {% set SolarPower = states('sensor.solaredge_huidig_vermogen') | float %}

      {{ ((PowerConsumption - SolarPower)) | round(0, default=0) }}
    availability: "{{ is_number(states('sensor.nonexistingsensor') and states('sensor.solaredge_huidig_vermogen')) }}"

It does ‘work’ if I give a fake sensor for the second sensor to check.

  # Power Consumption Grid Netto
  - name: "Power Consumption Grid"
    unique_id: "template_power_consumption_grid"
    unit_of_measurement: "W"
    device_class: power
    state_class: measurement
    icon: "mdi:transmission-tower"
    state: >
      {% set PowerConsumption = states('sensor.electricity_meter_energieverbruik_w_2') | float %}
      {% set SolarPower = states('sensor.solaredge_huidig_vermogen') | float %}

      {{ ((PowerConsumption - SolarPower)) | round(0, default=0) }}
    availability: "{{ is_number(states('sensor.electricity_meter_energieverbruik_w_2') and states('sensor.nonexistingsensor')) }}"

Hi Ricks88,

Change that to an is_state to get a boolean answer…

If I do this, it will work:

availability: "{{ is_number(states('sensor.electricity_meter_energieverbruik_w_2')) and is_number(states('sensor.nonexistingsensor')) }}"

But this is confusing (at least for me haha) because of the first is_number is opened with a ( and closed after the second sensor ).
example
is_number(<open states(sensor1) AND states(sensor2) close>)
Is it not that all sensors wrapped in the (<open close>) count as the is_number when using AND or OR?

Do you understand what i mean? haha

And do i still need the default=0 if i use the availability check?

Thanks!

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

these both work, but i am confused with the use of (( ))

Well I noticed that your first one was and-ing a bool with a something that wasn’t a bool. That is now fixed.
I’m not sure where your confusion is, but those now look correct to me. Those are both valid uses of that filter now.

1 Like

Hi @Sir_Goodenough ,

My confusion is in the use of the ( ) symbols.

availability:
"{{ is_number ( states(‘sensor.nonexistingsensor’) and states(‘sensor.solaredge_huidig_vermogen’) )

“is_number” is in front of the first bold ( symbol and both the sensors are in this 2 bold ( )

So the confusion is why is_number does not work for all sensors inside those 2 ( ) why do we need to repeat is_number for every entity that is already inside the wrap of those 2 ( ) where is_number is in front.

If i use the if else as en example of what i neam:

    state: >
      {% if**(** is_state('update.home_assistant_core_update1', 'on')
      or is_state('update.home_assistant_core_update2', 'on') 
      or is_state('update.home_assistant_core_update3', 'on') **)** %}

I do not need to put the "IF(is_stare) before every entity that is between the first and last ( ). just once.

Sorry i can not explain it better than this in my english :stuck_out_tongue:

Another example of the confusing with using () is this:

sensor:
  - name: "Aircos MaxiCool Cooling - Last Year Energy (kWh)"
    unique_id: "template_aircos_maxicool_cooling_last_year_energy_kwh"
    unit_of_measurement: "kWh"
    device_class: energy
    state_class: total
    icon: "mdi:lightning-bolt"
    state: "{{ (state_attr('sensor.aircos_maxicool_cooling_yearly_energy_kwh','last_period') | float)  | round(3, default=0) }}"
    availability: "{{ state_attr('sensor.aircos_maxicool_cooling_yearly_energy_kwh','last_period') | is_number }}"

This works:

{{ (state_attr('sensor.aircos_maxicool_cooling_yearly_energy_kwh','last_period') | float)  | round(3, default=0) }}"

and this works:

{{ state_attr('sensor.aircos_maxicool_cooling_yearly_energy_kwh','last_period') | float  | round(3, default=0) }}"

And also this:

{{ (state_attr('sensor.aircos_maxicool_cooling_yearly_energy_kwh','last_period')) | float  | round(3, default=0) }}"

I do not know anymore what is the “correct” way of using it… :see_no_evil:
They work, but do all of these variants have the same effect and results? I can put the end ) before the float, or after the float. It works but i do not know if the results or calculation is different.

Returns a boolean:

Returns the state of the sensor:

are separate operands.
The “{{ is the start of the template and the }}” is the end of the template.

The bold paren just made it check if what the and-ing of 2 states were a number or something weird.
I don’t know what happens when you ‘and’ 2 sensor states that are not bool…

1 Like