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