Calculating COP out of two sensors

Hello all,

I hope somebody can give me some hints to get my challenge done…
I got a new heat pump and I’m able to collect consumed energy and produced energy. That’s working pretty well! I also configured an utility meter which resets kWh every hour two 0. My target is now, that I take the last values before resetting to 0 to calculate the COP for every hour. I did a lot of tries (also that version you can find in the community) with various configures sensors which use average calculations and so on but unfortunately the results are not satisfying.
Is there any possibility that you do not have a continuous measurement of COP but only one time every hour? After calculating I would like to show the results for every hour in a bar chart.
Thanks for any advise and regards
Fabian

Try this:

template:
  - trigger:
      - platform: time_pattern
        minutes: 59
        seconds: 59
    sensor:
      - name: "Hourly COP"
        unit_of_measurement: "%"
        icon: "mdi:gauge"
        state: "{{ states('sensor.one')|float(0) / states('sensor.two')|float(1) }}"
        availability: "{{ states('sensor.one')|is_number and states('sensor.two')|is_number }}"

FYI it helps if you give the actual sensor entity ids in your post.

The value of this sensor will be unknown until the end of the first hour after you have created it.

1 Like

@tom_l It works! Thank you so much. That’s exactly what I’m looking for!
Maybe one question as I’m new to HA: I will create a new sensor which calculates COP at the end of the day. I would like to store these values infinite like the energy dashboard so that I can compare different day over many years. How can I achieve this?

Regards
Fabian

You can add a measurement state class like this:

template:
  - trigger:
      - platform: time_pattern
        minutes: 59
        seconds: 59
    sensor:
      - name: "Hourly COP"
        unit_of_measurement: "%"
        state_class: measurement 
        icon: "mdi:gauge"
        state: "{{ states('sensor.one')|float(0) / states('sensor.two')|float(1) }}"
        availability: "{{ states('sensor.one')|is_number and states('sensor.two')|is_number }}"

This will store hourly values as LTS (long term statistics) indefinitely (24 values a day).

You can graph this easily enough (use a statistics graph card) but currently there is no easy way to use these values without creating SQL sensors to retrieve the wanted value from the database.

The other option is to set up influxDB for long term data storage and graph it with Grafana.

What are you using for the sensor “produced energy” ?

cool, thanks a lot…I will have a try!

@krizzziz I got the value “produced energy” directly via modbus from the heatpump. unit is “kWh”. Also consumed energy is available.

Regards
Fabian

Hello,
I am also trying to calculate COP but the Code here is providing me some errors. As I am a total newbie I am not sure about the correct wording.

I adjusted your coding with my sensor, that are providing W and both are based on mqtt.

template:
  - trigger:
      - platform: time_pattern
        minutes: 59
        seconds: 59
    sensor:
      - name: "Hourly COP"
        unit_of_measurement: "%"
        state_class: measurement 
        icon: "mdi:gauge"
        state: "{{ states('sensor.hc_power_production_heatpump')|float(0) / states('sensor.go_econtroller_heizung_power')|float(1) }}"
        availability: "{{ states('sensor.hc_power_production_heatpump')|is_number and states('sensor.go_econtroller_heizung_power')|is_number }}"

This is the error I get in the helper setup

Invalid state with length 289. State max length is 255 characters.