Power Usage calculation

Hi
I do use the following template to calculate my power usage in the house:

  • sensor:
    • name: “energie_hausverbrauch”
      unit_of_measurement: “W”
      state: “{{ states(‘sensor.solarnet_pv_leistung’) | float + states(‘sensor.wirkleistung_bezug_total’) | float - states(‘sensor.wirkleistung_lieferung_total’) | float }}”

So I talke the pv-production (sensor.solarnet_pv_leistung), add the consumption from grid (sensor.wirkleistung_bezug_total) minus delivering to grid (sensor.wirkleistung_lieferung_total)

Sometimes it gives me negative values, probably because one of the others is zero at this point?

image

How can I do that better?

Thanks; Lukas

The SolarNet device should provide a ready to use entity for that. It’s called power_load, but I don’t know the German translation right now.

PS: I guess the knx-label here is a mistake.

If you don’t have that sensor you can use an availability template to make the template sensor state unavailable when one or more of the template source sensors are unavailable. This will produce a gap in the graph.


    sensor:
        name: "energie_hausverbrauch"
        unit_of_measurement: "W"
        device_class: power
        state: "{{ states('sensor.solarnet_pv_leistung') | float(0) + states('sensor.wirkleistung_bezug_total') | float(0) - states('sensor.wirkleistung_lieferung_total') | float(0) }}"
        availability: >
          {{ states('sensor.solarnet_pv_leistung') | is_number and
             states('sensor.wirkleistung_bezug_total') | is_number and
             states('sensor.wirkleistung_lieferung_total') | is_number }}

Also in future please format your pasted config correctly for the forum.

Thank you very much for that. I just try it.

Is there also a way to define how often the sensors will be updated and probably when exactly (to synchronise them)?

The template will update when any of the source sensors change state.

There is no way to synchronise the source sensors to each other.

This template worked for me, the entity “sensor.pv_total_production” even after turning off the inverter Zeversolar gives the last highest value.

        - name: "PV produkcja całkowita"
          device_class: power
          unique_id: "pv_produkcja_calkowita"
          unit_of_measurement: "kWh"
          state: >
            {% if states('sensor.zeversolar_sensor_energy_today') not in ("unavailable", "unknown", "none")  %}
            {{ (states('sensor.zeversolar_sensor_energy_today')|float(0) ) | round(2) }}
            {% else %}
            {{ states('sensor.pv_produkcja_calkowita') }}
            {% endif %}  ```