How to prevent NULL values in template sensor

I’m using the below template sensor.
It is a sensor that reads a value from our solar panel converter.
Of course, when it is dark it returns a NULL value which messes up my graph card.
How can I prevent the NULL value and store 0.0 instead?

  - platform: command_line
    name: Solar_EnergieVandaag
    command: 'curl "http://xxx.xxx.x.xx/real_time_data.xml" 2>&1 | grep -E -m1 -o "<e-today>(.*)</e-today>" | sed -e "s,.*<e-today>\([^<]*\)</e-today>.*,\1,g"'
    unit_of_measurement: "kWh"
    scan_interval: 3600

What exactly do you mean by “NULL value”? Do you mean an empty string? If so, try adding:

    value_template: "{{ value if value != '' else 0.0 }}"
1 Like

I have kind of the same issue.

I’m having values of 0 when the value is certainly not zero (indoor temperature sensors). I’ve used a template sensor to change zero’s to NaN, but this looks ugly in the frontend + gives warnings/errors when exporting to influxdb. Is there a way to just don’t update the state at all?

seems you need to fix the sensor itself rather than create a workaround for updating?
how come it shows 0 when temp isn’t 0?

A related question, and my first so please bear with me.

I have an RFLINK receiver plugged into a raspberry pi. A Node Red script transcribes the RFLINK messages into per-device JSON strings which get broadcast to a broker and on to my HASS.IO running on another Pi.

I’m processing received values in HASS.IO using a snippet like this:

sensor:
  - platform: mqtt
    state_topic: 'rflink/Alecto V45f58'
    value_template: '{{ value_json.hum }}'
    unit_of_measurement: '%'
    name: 'Kitchen H'

  - platform: mqtt
    state_topic: 'rflink/Alecto V45f58'
    value_template: '{{ value_json.temp }}'
    unit_of_measurement: '°C'
    name: 'Kitchen T'`

This works fine if the device spits out both temperature AND humidity values in one record. However my LaCrosse sensor spits out a temperature record and immediately thereafter a humidity record. Both state_topic fragments get actioned for each record as you might expect. However, half the time the fragment where I’m trying to extract a temperature value will fail because the JSON fragment contains just a humidity value whereas the fragment that tries to extract humidity will fail if the message contains just a temperature reading. Each fail half the time and, as far as I can tell, are returning NULL values and messing up the value displayed.

Any ideas how I can write a template that ignores the null values or is there another trick I’m missing?

BTW There’ll be LOTS of tricks I’m missing as I’m VERY new to this.

Regards.

Hi,
I’m also very new and finding it all a very steep learning curve despite being an ex automation engineer and now an IT professional.
You described my problem exactly and its a pity nobody else has answered you. Did you find a solution?

In my case , I have flashed a SonOFF Zigbee bridge with Tasmota. I’ve given up trying to get either Tasmota or MQTT auto-discovery working. So I have manually configured SonOFF sensors, which are providing separate messages for humidity and temperature.
The problem is that when the humidity message comes, the temperature value in HA goes to 0 and vice versa.
My configuration looks like this but its been a lot of guessing to get this far so I’d welcome any error spotting:

  - platform: mqtt
    name: "Tasmota 2 Temperature"
    state_topic: "tele/tasmota-zb1/SENSOR"
    value_template: "{{ value_json.ZbReceived['0x8EAB'].Temperature }}"
    unit_of_measurement: "°C"
    availability_topic: "tele/tasmota-zb1/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"
    device_class: temperature
    
  - platform: mqtt
    name: "Tasmota 2 Humidity"
    state_topic: "tele/tasmota-zb1/SENSOR"
    value_template: "{{ value_json.ZbReceived['0x8EAB'].Humidity }}"
    unit_of_measurement: "%"
    availability_topic: "tele/tasmota-zb1/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"
    device_class: humidity

I had the same issue with my barometric pressure sensor. Upon restart it would report 0 and mess up the charts, so… assistance from another contributor here helped me end up with creating a template like this:

    outdoor_barometer:
      friendly_name: Barometric Pressure
      unit_of_measurement: "inHg"
      value_template: >
          {% if (states('sensor.outdoor_multi_sensor_pressure')|float * 0.0295301)|round(2) <= 25.8 %}
            30.0
          {% else %}
            {{((states('sensor.outdoor_multi_sensor_pressure')|float * 0.0295301)+0.34)|round(2)}}
          {% endif %}

which, in the “if” statement, if the value presented is out of normal stuff, it sets it to a normal stuff value to start out.
Now my barometer shows decent graphing between the real world values of 26 and 31 inches.

Hi Dixey,
Thanks for the example. I’ll try to work out the syntax for my situation.
There must be something simple that just says if the attribute isn’t there, just keep the previous value and not go to zero/null.

Look at the availability_template option.

what does the topic look like when information for Temperature comes through? What does the topic look like when Humidity comes through?

Hi,

These are the kind of messages I receive.
Sometimes Temperature and Humidity values but usually just one or the other is included :

{"ZbReceived":{"0x8EEF":{"Device":"0x8EEF","Humidity":45.7,"Endpoint":1,"LinkQuality":118}}}


{"ZbReceived":{"0x8EEF":{"Device":"0x8EEF","Temperature":20.63,"Endpoint":1,"LinkQuality":118}}}


{"ZbReceived":{"0x8EEF":{"Device":"0x8EEF","Temperature":21.16,"Humidity":90.72,"Endpoint":1,"LinkQuality":107}}}

After quite a bit of trial and error guessing I’ve come up with the following value_template syntax that seems to have solved it. Not sure whether this is very correct but it works somehow. Now I don’t get any 0 values in HA.

    value_template: >
      {%- if value_json.ZbReceived['0x8EEF']['Temperature'] |float >= 0 %}
        {{ value_json.ZbReceived['0x8EEF']['Temperature'] }}
      {%- else -%}
        nan
      {%- endif -%}
2 Likes