Sensor code help please

What am I doing wrong in this sensor yaml.

I am trying to:

  1. Create a sensor named: Daily_Propane_Usage_371_with_test_to_more_than_0
  2. On the condition that ((midnight - current) + refill) is greater than 0, the sensor will get the value of ((midnight - current) + refill)
  3. Else, the sensor retains the last value
  - sensor:
      - name: "Daily Propane Usage 371 with test to more than 0"
        unit_of_measurement: "gal"
        state: >
          {% set midnight = states('input_number.midnight_propane_level_371_claude') | float %}
          {% set current = states('sensor.propane_tank_neevo_371_gallons') | float %}
          {% set refill = states('input_number.propane_refill_amount_371_claude') | float %}

          {% if (((midnight - current) + refill) | round(2) > 0) %}		  

            {% if is_number(midnight) and is_number(current) and is_number(refill) %}
              {{ ((midnight - current) + refill) | round(2) }}
            {% else %}
              {{ 0 }}
            {% endif %}

          {% else %}
            {{states('sensor.last_Daily_Propane_Usage_371_with_test_to_more_than_0') }}
          {% endif %}

        state_class: measurement
        device_class: volume_storage

Thank you.

Hello

shouldn’t include the default float(0)

{% set midnight = states('input_number.midnight_propane_level_371_claude') | float(0) %}
          {% set current = states('sensor.propane_tank_neevo_371_gallons') | float(0) %}
          {% set refill = states('input_number.propane_refill_amount_371_claude') | float(0) %}

As noted above, you need to handle the issue of one or more of your entities’ being unavailable… but, since using a default value could cause inaccurate calculations if only one of the entities isn’t working correctly, it needs to be handled before you apply the float filters:

- sensor:
    - name: "Daily Propane Usage 371 with test to more than 0"
      unit_of_measurement: "gal"
      state_class: measurement
      device_class: volume_storage
      state: >
        {% set midnight = states('input_number.midnight_propane_level_371_claude') %}
        {% set current = states('sensor.propane_tank_neevo_371_gallons') %}
        {% set refill = states('input_number.propane_refill_amount_371_claude') %}
        {% set n_list = [midnight, current, refill] %}
        {% if n_list | count != n_list | reject('in', ['unknown','unavailable', none]) | list | count %}
          {{ states('sensor.last_Daily_Propane_Usage_371_with_test_to_more_than_0') | default(0, 1) }}
        {% else %}
          {{ [((midnight | float - current | float) + refill | float) | round(2), 0] | max }}
        {% endif %}

Wow!

I did not know the difference between “float” and “float(0)”.

A quick google results in this:

https://community.home-assistant.io/t/need-a-quick-breakdown-of-meanings-of-float-int-and-how-to-use-them/664532

in which tom_i provides the very clear explanation:

" The number in the brackets … is the default value that will be substituted if the string can not be converted to a number. e.g.

"1.23456"|float(2)1.23456

"foobar"|float(2)2"

What an elegant parameter to be available!

Thank you.

Thank you very much for explaining what is necessary and for editing the code.

Unfortunately, while I get the gist of what the code does, the specific mechanics of the code is way above my level (using the “|”, “count”, “reject”, “list”, and “default”, for example).