Calculation (Subtraction) of 2 Sensors

Hello, I´m going crazy, since I am unable to calculate (substract) 2 Sensors for a Solar Device.

I have 2 main sensors I need to use:
sensor.ess_ess_home_statistics_pcs_pv_total (current solar power in W)
sensor.ess_ess_common_load_load_power (current total power consumption in W)

My goal is to get two custom sensors:

  1. Sensor
    feed grid in calculation:
    sensor.ess_ess_home_statistics_pcs_pv_total - sensor.ess_ess_common_load_load_power = “feed grid in power total”

  2. Sensor
    grid consumptions calculation:
    sensor.ess_ess_common_load_load_power -sensor.ess_ess_home_statistics_pcs_pv_total - = “grid consumption power total”

I tried

template:
  - sensor:
      - name: "VattenfallVerbrauch"
        unit_of_measurement: "W"
        state: "{{ states('sensor.ess_ess_common_load_load_power')|float(0) - states('sensor.ess_ess_home_statistics_pcs_pv_total')|float(0) }}"

but I dont get a new sensor or any value, I´m stuck , can someone explain what I´m doing wrong?

Sensor looks good, but if it is your first template sensor, you need to restart HA.

1 Like

You can create this in the UI, under Helpers. Paste in the template {{ ... }} that you already have into the State Template box.

now I think I´m stupid… man, thanks… I only did a soft restart, but it appears after a full restart

thanks!

what settings in the UI I can use, to substract? I only found summary as calculation

Hey @bosx2,
depending on your system and setup you might experience another issue. As both sensors are from your ESS, probably not an actual problem. Just so you are aware.

The way you have set it up now, your template sensor will update when either the load_power changes, or when the pv_total changes. You might run into the issue in which for a brief second one value will have changed significantly, while the other one reacts to that a second later. You might end up seeing spikes in your resulting sensors when e.g. a cloud moves in front of your PV or when you turn on your washing machine. So look out for that.

Btw you should think about your device and entity names. The ones I am seeing are horrible :smile:

1 Like

This one, where you could paste

{{ states('sensor.ess_ess_common_load_load_power')|float(0) - states('sensor.ess_ess_home_statistics_pcs_pv_total')|float(0) }}

in state template

1 Like

wow! I am baffled by the support by this community. Thank you so much :slight_smile:

1 Like