Need MQTT sensor template that filter state=0

Hello.

I have a little problem where I have a blockage in the brain :nerd_face:

I have an MQTT sensor that sends a value.
Unfortunately, sometimes, it sends a “0”.
How can I do it that only values that are “not zero” are recorded?

 - platform: mqtt
    name: "HM-600 Energie Total"
    state_topic: "Hoymiles/HM-600/ch0/YieldTotal"
    state_class: total_increasing
    last_reset_value_template: '1970-01-01T00:00:00+00:00'
    unit_of_measurement: 'kWh'
    device_class: energy

Thank you

This template keeps the last state if the value is 0.

value_template: "{{ value if value != 0 else states('sensor.hm_600_energie_total') }}"

Also delete this line:

last_reset_value_template: '1970-01-01T00:00:00+00:00'

it is not reqired for the state class you are using.

Thank you. It works :slightly_smiling_face:

Hello again.

Unfortunately it still doesn’t work. Last night I had zeros in my statistics again.

    name: "HM-600 Energie Total"
    state_topic: "Hoymiles/HM-600/ch0/YieldTotal"
    state_class: total_increasing
    value_template: "{{ value if value != 0 else states('sensor.hm_600_energie_total') }}"
    unit_of_measurement: 'kWh'
    device_class: energy

What is wrong with this template?

and can i do rounding a value like this?

 {{value if value != 0 else states('unknown') | round(1)}}

Thanks in advance

No that is not valid. The string ‘unknown’ has no state and you can’t round strings.

Try this:

    name: "HM-600 Energie Total"
    state_topic: "Hoymiles/HM-600/ch0/YieldTotal"
    state_class: total_increasing
    value_template: "{{ value if value not in [0, 'unknown', 'unavailable', 'none'] else states('sensor.hm_600_energie_total') }}"
    unit_of_measurement: 'kWh'
    device_class: energy

I will test and report.

TNX
Timo

Tonight again. Still zeros.
I don’t understand.

Show the sensor config you are using.

  - platform: mqtt
    name: "HM-600 Energie Total"
    state_topic: "Hoymiles/HM-600/ch0/YieldTotal"
    state_class: total_increasing
    value_template: "{{ value if value not in [0, 'unknown', 'unavailable', 'none'] else states('sensor.hm_600_energie_total') }}"
    unit_of_measurement: 'kWh'
    device_class: energy

Try:

 value_template: "{{ value if value not in [0, '0', 'unknown', 'unavailable', 'none'] else states('sensor.hm_600_energie_total') }}"
1 Like

Still no success

I write a Node red flow that mail the value.
It is “0.000”

Maybe:

value_template: "{{ value if value not in ['0.000', 'unknown', 'unavailable', 'none'] else states('sensor.hm_600_energie_total')

This work’s

Thank you

1 Like