Energy sensor, subtract Devices from Total

I´m looking to create a device energy sensor for all energy that is not defined by a device on the total energy by taking the Total energy use and subtract all the devices energy basically.

However i´m not getting the result i need as the sensor wont show as a Energy device and i know im missing some entities for this to work but i´m at a loss and hope someone is kind to show me in the right direction or has an example.

What i have right now is,

  - platform: template
    sensors:
      rest_energy:
        friendly_name: Övrigt (Kwh)
        unit_of_measurement: Kwh
        value_template: >
          {{ states('sensor.ams_han_houruse')| float() 
          - states('sensor.vardagsrum_energy')| float() / 1000 }}

  - platform: integration
    source: sensor.rest_energy
    name: total rest
    unit_prefix: k
    round: 5

Thanks in advance,
Christian

Change to the new setup, you are using legacy template…I understood that legacy does nto support unitofmeas
Template - Home Assistant (home-assistant.io)

And I am not sure if you divide the right level…as I donot know what your sensors kick back…but do check this in devtools > template too if not sure

{{ (states('sensor.ams_han_houruse')| float() - states('sensor.vardagsrum_energy')| float() ) / 1000 }}

you are right, when i use the new template it does seem to work better and i can see the sensor in the devices.

Also, as you suspected I was using the wrong entities wich brings me another question, right now i use the Dayly usage in Kwh for the other devices, do need to specify that somehow for the template sensor?

template:
  - sensor:
      - name: "Övrigt (Kwh)"
        unique_id: "sensor.total_other_subtracted_energy"
        unit_of_measurement: "kWh"
        state: "{{ states('sensor.ams_han_dayuse')| float() 
        - states('sensor.vardagsrum_daily_energy_consumed')| float() }}"
        state_class: total_increasing
        device_class: energy
        attributes:         
          last_reset: '1970-01-01T00:00:00+00:00'

I believe that would work, but I have little to compare myself to be 100% sure
I have one total_increasing sensor for water_usage, which unit is m3 and I feed it with another sensor that is in m3…that works for me…maybe other can chime in?

Still having some issues and not sure how to get around it, some sensors that i need to subtract are only giving me the total energy use (Shelly Plug S) and not the daily usage so the new sensor value is way off.

How do i go about this, is there a way to get the daily usage values of the plugs energy reading? and if so, how will the value the new sensor get affect the long time statistics of the new sensor when i use the daily usage as a reference, do i set the state_class as measurement on the sensor?

    {{ states('sensor.ams_han_dayuse')| float() #daily usage, reported from the mains
      - states('sensor.vardagsrum_daily_energy_consumed')| float() #daily usage, heating system
      - states('sensor.plug_computer_energy')| float() #total usage, 
      - states('sensor.plug_freezer_energy')| float() #total usage
      - states('sensor.plug_refrigerator_energy')| float() #total usage
      - states('sensor.badrum_overvaning_energy_usage')| float() #total usage
    }}

I.E. All sensors above are set as devices in the Energy dashboard, and i want to have the new sensor (added as device) to show the amount of energy that is not being monitored.

I have exactly the same issue.

I have three kWh meters with pulse output. One for Electric vehicle charging (EV), heatpump (MLP) and main (ALL). I have integrated all these to Home Assitant. They show accurate and consistent power value (W).

I want to substract EV and heatpump (MLP) from total consumption (ALL) in order to add them all to “Energy” dashboard to see the consumption and price in one bar. I made helpers to convert Power to Energy end added them respectively to dash. BUT the problem is unaccurate values.

Here…
EnergyAll_minus_EVandMLP = Energy_All - Energy_EV - Energy_MLP

    sensors:
        energy_all_substracted:
            friendly_name: "energy_all_substracted"
            value_template: '{{ states("sensor.energy") | float - states("sensor.energy_ev") | float - states("sensor.energy_mlp") | float }}'
            unit_of_measurement: 'Wh'

Integration is done with Reiman LEFT helper

Is it possible to import kWh reading straight to Home Assistant as I have possibility to do that.
But I did not find any way to import just Energy. The system is built so that the only way is to import POWER and export it to ENERGY by using ** Reiman integration**

What do you mean ? A continuous integration or import from file (not possible yet)

Continuous integration .
I can get the kWh reading from API interface, but it does not seem to be possible to import …or use… that measurement.The only possibility sems to be import power and using reiman integration to convert W to Wh.

I already have running and working sytem…it’s just very unaccurate therefore I’m looking for alternative possibilities.

I am still not sure what you mean with ‘import’ …import where? Can you read it from the API by other means but it is not loaded/visible in a HA sensor? Or is it visble in a HA sensor but you cannot add this to the enrgy dash?

Aa…sorry. The most important information was missing.

Yes, I import from API.
I have used energomonitor (https://www.energomonitor.com) for years and utilizing their API

And everythig works just fine.I can import the POWER and have a helper to convert it to ENERGY.
Power readings from API are all good and accurate… But the ENERGY is very unaccurate (maybe due to Reiman integral) In fact so unaccurate that I satarted to think some alternative way to calculate ENERY.

Energomonitor offets ENERGY readings as well via API (based on 1 hour POWER) so I started to think if I can use that reading straight.

1 Like

So if you use their API (outside of HA) you have the accurate data but not in HA?

I have no means to even see how their API works as not a customer…is this REST?
EDIT…it is REST, still not sure what you see (or not see) in HA

The power reading im getting from API is exactly the same.

This is from Energomonitor dash:
image

And this is from HA based on data from Energomonitor API and is very accurate.

This is INSTATANT power.

When compared ENERGY (power in 1 hour)

There is a difference. To make this clear, the HA calculates the energy from power reading above.

Energomonitor:

HA:

The total is quite accurate: But the MLP reading is way off.
Maybe it’s due to the nature of those spikes?

But whatever the reason is…I can’t use it as it’s way too inaccurate

I see, so for the MLP you use the Rieman integration and with spikes like that…did you try to compare left with middle with right?
I myself only calculate Rieman from a few plugs and no way to see if this is corrcet or not…
So if you can get this from the API then that would be best, for my household I use what the meter is telling ,me…but now I will also try a calcaultation just for the fun :face_with_raised_eyebrow:

Sure I have…

However, Energy_all is fairly accurate. I guess I should create the same also for Energy_MLP

I started to calc power-rieman and compared to energy and it deviates already now, not massive but also not nothing