Template Sensor = 0 instead of null

Hello,

i try to calculate the COP from my heatpump.
So i take the energy production from Heishamon and the energy consumption in a code like this:

  - platform: template
    sensors:
      cop_wp_hk_hz:
        friendly_name: COP Waermepumpe_HK HZ
        value_template: >-
          {{ ((float(states('sensor.waermepumpe_hk_main_heat_energy_production'))) / (float(states('sensor.waermepumpe_hk_main_heat_energy_consumption')))) | round(1) }}

The Problem is, when the heatpump dont work i get a “not available” from my template sensor.
How can i say the template sensor that he should be Zero when he is in truth “not available”?

I have a big problem in Grafana (with Influx) because the “last value” is NOT zero if the sensor is suddenly not available.

Thanks

- platform: template
    sensors:
      cop_wp_hk_hz:
        friendly_name: COP Waermepumpe_HK HZ
        value_template: >
          {% if has_value('sensor.waermepumpe_hk_main_heat_energy_production') and 
                has_value('sensor.waermepumpe_hk_main_heat_energy_consumption') %}
            {{ ( states('sensor.waermepumpe_hk_main_heat_energy_production')|float / states('sensor.waermepumpe_hk_main_heat_energy_consumption')|float )|round(1) }}
          {% else %}
            0
          {% endif %}

Thx for the quick answer.
The sensor.cop_wp_hk_hz is still not available in Homeassistant.
Is it possible that the division with 0 is the problem?
sensor.waermepumpe_hk_main_heat_energy_consumption has actualy the value “0” and the result would be a calculation >>>> 0/0

So how can i avoid it like:
If sensor.waermepumpe_hk_main_heat_energy_consumption “0” then sensor.cop_wp_hk_hz = “0” without calculation?

Do you have division by zero errors in your log?

You said the source sensors were unavailable, not 0. Which is it?

What is their state when the heat pump is unavailable?

It look like this:

So im sorry the Heatpump and the sensors from it are still available but have the value “0” when not working.
At this point the template sensor.cop_wp_hk_hz (COP Waermepumpe_HK HZ) goes to not available.

- platform: template
    sensors:
      cop_wp_hk_hz:
        friendly_name: COP Waermepumpe_HK HZ
        value_template: >
          {% if has_value('sensor.waermepumpe_hk_main_heat_energy_production') and 
                states('sensor.waermepumpe_hk_main_heat_energy_consumption')float(0) != 0 %}
            {{ ( states('sensor.waermepumpe_hk_main_heat_energy_production')|float / states('sensor.waermepumpe_hk_main_heat_energy_consumption')|float )|round(1) }}
          {% else %}
            0
          {% endif %}
1 Like

I would recommend to replace has_value with is_number as you are doing calculations and any non-numeric input value will throw errors.

The only values other than numbers the sensor can have are unknown and unavailable. Both of which will register as false.

https://www.home-assistant.io/docs/configuration/templating/#states

With this code i get error at check:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘float’) for dictionary value @ data[‘sensors’][‘cop_wp_hk_hz’][‘value_template’]. Got “{% if has_value(‘sensor.waermepumpe_hk_main_heat_energy_production’) and \n states(‘sensor.waermepumpe_hk_main_heat_energy_consumption’)float(0) != 0 %}\n {{ ( states(‘sensor.waermepumpe_hk_main_heat_energy_production’)|float / states(‘sensor.waermepumpe_hk_main_heat_energy_consumption’)|float )|round(1) }}\n{% else %}\n 0\n{% endif %}\n”. (See ?, line ?).

Sorry I missed a |

        value_template: >
          {% if has_value('sensor.waermepumpe_hk_main_heat_energy_production') and 
                states('sensor.waermepumpe_hk_main_heat_energy_consumption')|float(0) != 0
                                                                            ^
                                                                            |
                                                                           Here

no reason to say sorry - my yaml is just to bad :slight_smile:
Realy realy Thx it works now as it should!!!

THANK YOU

1 Like