I need to remove zero from the sensor

Hello,
I have to read the meter using MQTT, which starts every 15 minutes. Unfortunately, always the first result with zero and the next the actual kWh values. Is there any way to get rid of zero from the sensor? So far I use sensor filter, but there is necessary to set the minimum value, so there is no zero, but set the lowest value.
Is it somehow possible to set the last non-zero value or not to write at all when it is zero?
http://fii.cz/jtzxkn

Thank you

Yes this is possible. Show me your mqtt sensor config.

  - platform: mqtt
    state_topic: "tele/tele/SENSOR"
    name: "Celková spotřeba"
    unit_of_measurement: "kWh"
    value_template: "{{ value_json.STROM.Celkova_spotreba }}"

  - platform: filter
    name: "Celková spotřeba filtr"
    entity_id: sensor.Celkova_spotreba
    filters:
      - filter: range
        lower_bound: 10000
1 Like

This sensor definition will keep the last value of the sensor if the new reading is zero:

  - platform: mqtt
    state_topic: "tele/tele/SENSOR"
    name: "Celková spotřeba"
    unit_of_measurement: "kWh"
    value_template: >
      {% if value_json.STROM.Celkova_spotreba == 0 %}
        {{ states('sensor.celkova_spotreba') }}
      {% else %}
        {{ value_json.STROM.Celkova_spotreba }}
      {% endif %}

You should not need the filter sensor any more. In fact I am surprised it ever worked considering you have a capital letter in the sensor entity id.

  - platform: filter
    name: "Celková spotřeba filtr"
    entity_id: sensor.Celkova_spotreba
                      ^
                      ^
2 Likes

@tom_l
:frowning: doesnt work

missed comma between flow collection entries at line 102, column 8:
          {% if value_json.STROM.Celkova_sp ... 
           ^
value_template: >
1 Like

Thanks Steve. Post updated.

Fantastic! Thank you. :slight_smile:

Hello guys,

Hope you can help me with this, I am not sure how to do it, even though it is probably really easy.

I have this template sensor:

 - platform: template
    sensors:
      apartment_1_energy_kwh:
        entity_id: sensor.apartment_1_energy
        value_template: "{{ states('sensor.apartment_1_energy') | float / 1000 }}"
        friendly_name: "Apartment 1 energy kWh"
        unit_of_measurement: kWh

The problem is that the energy sensor sometimes sends a 0, which then messes up the utility meter value.
And I would like that if sensor.apartment_1_energy is equal to 0, then it should use the previous value of sensor.apartment_1_energy, otherwise, it should use the new value .

Thank you

Try:

 - platform: template
    sensors:
      apartment_1_energy_kwh:
        entity_id: sensor.apartment_1_energy
        value_template: >
          {% set value = states('sensor.apartment_1_energy') | float %}
          {{ value / 1000 if value else states('apartment_1_energy_kwh') }}
        friendly_name: "Apartment 1 energy kWh"
        unit_of_measurement: kWh

It will filter out all zero results that sensor.apartment_1_energy produces.

Thanks very much.
I have updated the config, will update this thread after some time to let you know wether it’s working
Regards

Excuse me for reviving this thread, I’m trying to do something very similar but am lost trying because of the math in my template.

My template sensor outputs -10.848 when ‘sensor.vwc_s1’ state = 0. This destroys my graph, because every measurement of that sensor outputs a 0 just a millisecond before it gives the measured value. Could I rewrite the template to ignore any -10.848 value?

Ultimately I would try to only let values between 0.01 and 0.99 come through this template

  - platform: template
    sensors:
      c_vwc_s1:
        friendly_name: Calculated Volumetric Water Content Sensor 1
        unit_of_measurement: "θ"
        value_template: >
          {{ ((6.771 * 10 **-10) * (states('sensor.vwc_s1')|float ** 3) -
             (5.105 * 10 **-6) * (states('sensor.vwc_s1')|float ** 2) +
             (1.302 * 10 **-2) * (states('sensor.vwc_s1')|float) -10.848) | round(3)  }}
- platform: template
    sensors:
      c_vwc_s1:
        friendly_name: Calculated Volumetric Water Content Sensor 1
        unit_of_measurement: "θ"
        value_template: >
          {% if is_state('sensor.vwc_s1', '0') %}
            {{ states('sensor.c_vwc_s1') }}
          {% else %}
            {{ ((6.771 * 10 **-10) * (states('sensor.vwc_s1')|float ** 3) -
               (5.105 * 10 **-6) * (states('sensor.vwc_s1')|float ** 2) +
               (1.302 * 10 **-2) * (states('sensor.vwc_s1')|float) -10.848) | round(3)  }}
          {% endif %}

Hi,

I have a similar problem, I have a sensor that takes information from the integration, but in every restart , starts with 0 value for a seconds , and after taht recover the last value, in this case if I configure a helper to count the increasing ,this sum the same value twice, How can avoid this 0 when I start de HA, and take as a last value, the value that this sensor had before restart HA.?

Thanks

Hello,

Thanks for this topic i am facing the same situation but with zigbee2mqtt i did not find were to put this script in order to avoir this 0 causing wrong data collection.

Do yo have any idea ?

EDIT : Issue solved by creation of the helper hourly with parameter “Net consumption” enable

Enable if the source is a net meter, meaning it can both increase and decrease.

This permit to deduc and add when the sensor go to 0