Display the last value that is greater than zero and not unknown

I am getting crazy about this, I have read several posts about this but still can’t get it to work. Maybe because they are all answered with the legacy template style?
I have also read the docs about templates, and I have tried to use the availability option but it does not work the way I want.

It should be a fairly simple operation, I have sensor and I only want to display the last value that is greater than zero (and no ‘Not available’ or ‘ unknown’).

The sensor that has the value is ‘sensor.laddbox_session_energy’
It has the value 0 when it not in use.

I have tried this. It work when the sensor has a value but when it’s zero it displays unknown.

template:
  - sensor:
      - unique_id: laddbox_session_energy2
        unit_of_measurement: "kWh"
        state: >-
          {% if states('sensor.laddbox_session_energy') > '0' %}
             {{ states('sensor.laddbox_session_energy')|int / 1000  }}
          {% else %}
             {{ states('sensor.laddbox_session_energy2')  }}
          {% endif %}

#displays Unknown
            
  - sensor:
      - unique_id: laddbox_session_energy3
        unit_of_measurement: "kWh"
        state: >-
          {% set data_source = states('sensor.laddbox_session_energy') %}
          {% if data_source in ['unavailable', 'unknown', '0'] %} 
            {{states('sensor.laddbox_session_energy3')}}
          {% else %}
            {{data_source}}
          {% endif %}

#displays Unknown

  - sensor:
      - unique_id: laddbox_session_energy5
        unit_of_measurement: kWh
        state: "{{ states('sensor.laddbox_session_energy')|int(-1) / 1000  }}"
        availability: "{{ states('sensor.laddbox_session_energy')|int(-1) > 0 }}"

 #displays  Not available

  - sensor:
      - unique_id: laddbox_session_energy6
        unit_of_measurement: "kWh"
        state: >-
          {% if states('sensor.laddbox_session_energy') not in ['unknown', 'unavailable' , "0", "0.0"] %}
            {{ states('sensor.laddbox_session_energy')|int / 1000 }}
          {% else %}
            {{ states('sensor.laddbox_session_energy6') }}
          {% endif %}

#displays Unknown

Thanks

It’s doing what it’s told, but you’re thinking you can do numeric comparisons on strings. Look:

That’s because ‘u’ is later in the ASCII character set than ‘0’. Try:

        state: >-
          {% if states('sensor.laddbox_session_energy')|is_number and
                states('sensor.laddbox_session_energy')|int(0) > 0 %}
             {{ states('sensor.laddbox_session_energy')|int(0) / 1000 }}
          {% else %}
             {{ states('sensor.laddbox_session_energy2')  }}
          {% endif %}
1 Like
template:
  - sensor:
      - name: laddbox_session_energy2
        unit_of_measurement: "kWh"
        state: >-
          {% set e = states('sensor.laddbox_session_energy') %}
          {% if e | is_number and e | int(0) > 0 %}
             {{ e | int(0) / 1000  }}
          {% else %}
             {{ this.state }}
          {% endif %}
1 Like

Thanks, both solutions works fine.
I am new to this YAML syntax so pardon if this was a stupid question.

By the way, what does | int (0) mean?

Convert to to integer ? But the zero then?

The zero is a default value to be used in case the sensor state cannot be converted to an integer. Explained here:

…but:

1 Like

Thanks for that, really appreciate your answer!

im trying to use this same sensor but it converts my W to KW how can I make it not do that?

Which “same sensor”? The one in the post to which you are replying is an energy sensor (Wh or kWh) not a power sensor (W or kW).

Paste in the correctly-formatted code for the sensor you have. I suspect the answer will be as simple as “delete the / 1000” but need to see your code to be sure.

Power → watt
Energy → watt-hour

You have an energy sensor that reports in power units?

Please follow Troon’s instructions.