Calculate cop as template sensor

Please be paitient with me. This is my first topic an english not my first language.
I try to calculate the cop of my heat pump.
I created a template sensor but the sensor dosen´t work as i want to.

value_template:  '{{(( sensor.vl | float) - (sensor.rl | float))*(sensor.vol | float) * 0,28 / (sensor.gesamtleistung | float)}}'

This is my calculation code.
Every sensor is an MQTT sensor alredy implemented in homeassistant.
Dose anybody know whats wrong?

  1. Everything under the hood uses English notation for numbers.
  2. To grab states, you have to use the states() function and pass the entity_id to it. states(entity_id)
value_template:  "{{ (states('sensor.vl') | float - states('sensor.rl') | float) * states('sensor.vol') | float * 0.28 / states('sensor.gesamtleistung') | float }}"

Also, you’ll have issues if sensor.gesamtleistung goes unavailable. You’ll end up dividing by zero which will result in an error.

Another thing, you’ve got some unnecessary parenthesis based on the math you are performing. Makes it a bit harder to read. You’re welcome to put them back, but I removed them below.

Lastly, you can make variables to make the equation more readable.

value_template: >
  {% set vl = states('sensor.vl') | float %}
  {% set rl = states('sensor.rl') | float %}
  {% set vol = states('sensor.vol') | float %}
  {% set gesamtleistung = states('sensor.gesamtleistung') | float %}
  {% if gesamtleistung != 0 %}
    {{ (vl - rl) * vol * 0.28 / gesamtleistung }}
  {% else %}
    0
  {% endif %}

Thanks for response.
Now there is no error code but the sensor is 0 all the time.
I used the entity names of the sensors. Is that ok or shuld i try to use the MQTT masages istead?
I think the sensors are not korrekt linked.

you want to use the entity_id’s. Verify that you have the correct spelling of them. Also, you can use the template editor to verify your code.

Thank you!
I found the problem. Now it works.

1 Like

Hello Peter,
How did you solve it, can you please share your solution and your experience with the cop calulcation.
What kind of heatpump do you have?
And maybe you can explain the factor 0,28, is this the inner diameter of the pipe?

Thanks.
Regards,
Dominik

Now i have it running with

      heatpump_cop:
        friendly_name: "Wärmepumpe cop"
        unique_id: uniqueid_heatpump_cop
        value_template: >
          {% set Leistungthermisch = states('sensor.warmepumpe_leistung_thermisch') | float %}
          {% set Leistungelektrisch = states('sensor.hp_sdm630_total_system_power') | float %}
          {{ (Leistungthermisch / Leistungelektrisch) | float | round(3) }}

But if i try it with the if else. i only get 0.00 and the total sensor ist not shown as a numer, also not the statistics or the history.

        value_template: >
          {% set vl = states('sensor.warmepumpe_temperatur_vorlauf') | float %}
          {% set rl = states('sensor.warmepumpe_temperatur_rucklauf') | float %}
          {% set vol = states('sensor.warmepumpe_volumenstrom') | float %} 
          {% set Leistungthermisch = states('sensor.warmepumpe_leistung_thermisch') | float %}
          {% set Leistungelektrisch = states('sensor.hp_sdm630_total_system_power') | float %}
          {% if Leistungelektrisch != 0 | float | round(3) %}
          {{ (Leistungthermisch / Leistungelektrisch) | float | round(3) }}
          {% else %}
          0,00
         {% endif %}

This i how it gets dieplayed:
image

But i would like to have it with history and shown as a number.
Can you maybe help?