Conditional calculation in value_template

I am calculating the realtime COP of my heat pump. But when the heatpump is off, there is a zero in the denominator (sensor.energy_heatpump_wp_electricity_usage)

sensor:
  - platform: template
    sensors:
      cop_wp:
        value_template: "{{ ((states('sensor.wp_generated_energy') | float) / ((states('sensor.energy_heatpump_wp_electricity_usage') | float))) | round(2) }}"
        device_class: power_factor
        entity_id: sensor.remeha_aanvoertempearatuur_wp

I have tried

value_template: 
{%if (states('sensor.energy_heatpump_wp_electricity_usage')) == 0 %}
"{{ ((states('sensor.wp_generated_energy') | float) / ((states('sensor.energy_heatpump_wp_electricity_usage') | float))) | round(2) }}"
{%else %}
0
{%endif %}

It works in the developer tools template editor but does not work in home-assistant
When I try to check configuration, the check process keeps running and the wheel keeps spinning.

So how can I do a conditional value_template calculation?

Try like this:

value_template: >
  {% if(states('sensor.energy_heatpump_wp_electricity_usage')) != 0 %}
    {{((states('sensor.wp_generated_energy') | float) / ((states('sensor.energy_heatpump_wp_electricity_usage') | float))) | round(2) }}
  {% else %}
    0
  {% endif %}

You cant place the template on its own line without telling HA that it is what you are doing.

value_template: >
  {% if states('sensor.energy_heatpump_wp_electricity_usage')|float(0) != 0 %}
    {{ ( states('sensor.wp_generated_energy')|float(0) / states('sensor.energy_heatpump_wp_electricity_usage')|float(0) )|round(2) }}
  {% else %}
    0
  {% endif %}

Well, that was helpful, but did not solve the problem. I have now a new version:

      value_template: >
        {% if (states('sensor.energy_heatpump_wp_electricity_usage') | float) > 0 %}
        "{{ ((states('sensor.wp_generated_energy') | float) / ((states('sensor.energy_heatpump_wp_electricity_usage') | float))) | round(2) }}"
        {% else %}
        0
        {% endif %}

But the same problem, it will not pass the configuration check (I actually don’ know because the proces seems to be in a loop)

You have quotes for no reason. That line is now evaluated as a string.

I updated my answer above to say if the result isnt 0 then use the template if it is return 0 instead.

value_template: >
  {% if(states('sensor.energy_heatpump_wp_electricity_usage') | float) != 0 %}
    {{((states('sensor.wp_generated_energy') | float) / ((states('sensor.energy_heatpump_wp_electricity_usage') | float))) | round(2) }}
  {% else %}
    0
  {% endif %}

That solved the check configuration loop! Thanks.
I have copied your answer in my value_template. The result (cop_wp) value is still unavailable, while both variables in the calculation have a value. There was a " at the end of the line in your answer, removed it, but that did not help.

Try mine (above).

The |float filter won’t work without a default (0) if your sensor state is unavailable. You will have errors in your log about this.

1 Like

Thank you both! My problems are solved. YAML is quite a steap learning curve coming from Domoticz. But the support from other users and moderators is great!

2 Likes