Low Carbon kWh as a sensor?

This is what I’m getting at, the calculation and how it is formed is not documented, I’d make statistics sensors and such, but it seems silly to have to go through all that when the values are already right there on the front end…

Well, regardless where you start, you will need to get all the kWh information into entities, using utility meter. So that’s step 1. Step 2 is getting those percentages. I have no idea what your source sensors are, so it’s hard for me to give you any information. All you’ve provided is screenshots of the energy tab.






  - sensor:
        name: Grid Import Power
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ [0, states('sensor.envoy_SERIALNUMBER_current_power_consumption') | int - states('sensor.envoy_SERIALNUMBER_current_power_production') | int ] | max }}
  - sensor:
        name: Grid Export Power
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ [0, states('sensor.envoy_SERIALNUMBER_current_power_production') | int - states('sensor.envoy_SERIALNUMBER_current_power_consumption') | int ] | max }}

This is what the energy dashboard is feeding from. I use monthly utility meters, but can make daily ones. But I still don’t understand how you would get the % from this… Unless you know the formula for calculating % like you do kWh for low carbon.

So the values look like your daily values, so to start, you should make daily sensors for import and export energy. That will give you grid → and <-. You’ll also need solar. With those 3 values, you should be able to calculate what your house consumption is. House Consumption = (Solar - Export) + Import.


For the low carbon, I believe you need to sum the individual momentary percentages of kWh. You could try to create a template sensor with the following template, and use that in a daily utility meter.

  - sensor:
      - name:  Grid Fossil Fuel Energy
        state_class: measurement
        unit_of_measurement: kWh
        device_class: energy
        state: >
            {{ states('sensor.grid_import_energy') | float * states('sensor.co2_signal_grid_fossil_fuel_percent') | float }}

That should give how much is fossil fuel, and the rest from the grid should be low-carbon.

Yep, I’ve got a meter for this Monthly, just made one for ‘daily’ to feed a template.

So for sanity sake, I just made the daily meter, so it has no statistic data, but my import today is ~8.5kwh and my low-carbon on the energy dashboard is 6kwh

If I look at my monthly I got 77.967
image

So lets change the template with something hardcoded in the template to test

{{ 8.5 | float * states('sensor.co2_signal_grid_fossil_fuel_percentage') | float }}

image

and for sanity the original template above shows

image

I’m not quite sure that’s the right calculation.

Petro, I definitely appreciate your responses. But hope you can understand my frustration, when almost everything in HA if it is displayed in the front end, has an entity, or recorder, or statistic, or something in the database to pull from. Energy dashboard calculations seem like an oversight, most of them are easy to calculatate, like self consumed solar, and export vs import, house consumption, and net exports etc, all very easy to make your own templates - with the goal to use those entities in other cards outside of the very limited energy dashboard(in terms of customizing cards). Low Carbon is just one of those where you only see it in the power distribution card, or carbon guage.

If I could figure out the low-carbon consumed percentage then a template like below would work. But I need to be able to calculate/create low_carbon_consumed_percentage first

{{ ((100 - sensor.low_carbon_consumed_percentage) / 100 * (states('sensor.envoy_202307077673_today_s_energy_production') | float * 0.001)) | round(1) }}

You can vote for this feature request here:

2 Likes

Divide the result of the template I provided by 100. I forgot to add that in the template. You can either divide the whole percentage or the output result.

Also your quick calculations won’t match because you’re taking the end result and comparing it to a momentary result. You need to create the template and the results from the template need to get added to your database. Then you sum the results with utility meter by making a daily sensor with the resulting kWh template sensor. Your “quick math checks” will never match up.

The math is essentially:

Sum(A*x + b*y + c*z ….)

And your quick check is only

A*x
1 Like

Now that I’m at a computer instead of mobile, let me highlight what I believe the equation is. Also, highlight why your calculation is most likely not correct.

I’m pretty sure the equation is
image

The equation you’re trying to say is correct
image

Let me highlight the differences by simplifying this. For this example, I’ll remove the time element because it will most likely add confusion. We will also just perform a simple average instead of a summation just to highlight the differences in the mathematical approaches.

Lets take the following dataset:

kWh %
10 0.5
90 0.1
1.111… 0.9

You are trying to average the columns, then multiply. You’ve essentially been trying to get the 0.5 in the average column.

pt kWh % -
2 10 0.5 -
3 90 0.1 -
4 1.111… 0.9 total
Average 33.7 0.5 16.9 kWh

What you should be doing is multiplying the rows, then averaging that result. Notice how it’s wildly different.

pt kWh % average
2 10 0.5 5.0
3 90 0.1 9.0
4 1.111… 0.9 1
- - Total 5 kWh

This is why your ‘spot checks’ are not matching, and why it’s important to make the template sensor and pass the template sensor to utility meter. Completely forgoing the % amount you’re trying to obtain.

Take these 2 sensors and pass them to Utility meter.

  - sensor:
      - name:  Grid Fossil Fuel Energy
        state_class: measurement
        unit_of_measurement: kWh
        device_class: energy
        state: >
            {{ states('sensor.grid_import_energy') | float * states('sensor.co2_signal_grid_fossil_fuel_percent') | float / 100 }}
        availability: >
            {{ states('sensor.grid_import_energy') | is_number and states('sensor.co2_signal_grid_fossil_fuel_percent') | is_number }}
      - name:  Grid Clean Energy
        state_class: measurement
        unit_of_measurement: kWh
        device_class: energy
        state: >
            {{ states('sensor.grid_import_energy') | float * (1 - states('sensor.co2_signal_grid_fossil_fuel_percent') | float / 100) }}
        availability: >
            {{ states('sensor.grid_import_energy') | is_number and states('sensor.co2_signal_grid_fossil_fuel_percent') | is_number }}
1 Like

ah I misunderstood your intentions with the utility meter

no problem

this makes sense and clarifies why the utility meter is needed to perform over time

thank you for clarifying and explaining. I did not read it as this with the initial template, or see what you meant until I reread your intial response, the utility meter is the time piece. this makes ALOT more sense now, as I had thought in my initial post it was the co2 percentage at points in time of usage, meter gives you the time scale. awesome.

I still think we need some debugging here. I won’t be convinced that this is the solution until we have matching data (entity matching frontend). But to be honest, I don’t know what else could derive the result you’re looking for if this doesn’t work.

1 Like

No worries. I am putting this in place today so it is ready for 00:00:00 tonight at meter reset, and will report back tomorrow - as today’s numbers are already skewed. I seriously appreciate your input, and I’m sure others will as well.

For now I will leave the solution mark, until we can validate, and can update the solution in a further post after I have a full day of meter data from 00:00:00.

:pray::pray::raised_hands:

Welp, that numbers funky. Lol not sure that template is right either.



look correct to me based on your input sensor. The huge jump is because that sensor went to 100% for an extended duration.

@petro but my grid import usage is only 7.5kwh, so not sure how it got 70 and 68kwh

You aren’t looking at the daily value though, unless you skipped a step. Are those incorrectly labeled? Also your clean energy one should have a drop, not increass

I had some time to think and I know the problem

1 Like

It is a daily meter, resetting every day. I copied the templates exactly, and tested them in the template tester to verify they worked. Then utilized that template sensor to make a daily meter.

change your template sensors to:

 - trigger:
   - id: grid
     platfrom: state
     entity_id: sensor.grid_import_energy
     variables:
       old: >
         {{ trigger.from_state.state if trigger.from_state is defined else None }}
       new: >
         {{ trigger.to_state.state if trigger.to_state is defined else None }}
       delta: >
         {{ new - old if new | is_number and old | is_number else None }}
   sensor:
      - name: Momentary Grid Import Energy
        state_class: measurement
        unit_of_measurement: kWh
        state: "{{ delta }}"
        availability: "{{ delta | is_number }}"
 
 - sensor:
      - name:  Grid Fossil Fuel Energy
        state_class: measurement
        unit_of_measurement: kWh
        device_class: energy
        state: >
            {{ states('sensor.momentary_grid_import_energy') | float * states('sensor.co2_signal_grid_fossil_fuel_percent') | float / 100 }}
        availability: >
            {{ states('sensor.momentary_grid_import_energy') | is_number and states('sensor.co2_signal_grid_fossil_fuel_percent') | is_number }}
      - name:  Grid Clean Energy
        state_class: measurement
        unit_of_measurement: kWh
        device_class: energy
        state: >
            {{ states('sensor.momentary_grid_import_energy') | float * (1 - states('sensor.co2_signal_grid_fossil_fuel_percent') | float / 100) }}
        availability: >
            {{ states('sensor.momentary_grid_import_energy') | is_number and states('sensor.co2_signal_grid_fossil_fuel_percent') | is_number }}

This is going to add a 3rd sensor that takes the total_increasing sensor and turns it into a measurement so that the percent is applied correctly to the change in kWh (not the total).

1 Like

Hmm

Seems I’ve got something funky with the meter. As it is daily resetting but it reset 0 then immediately jumped to 12kWh.

10 minute span at reset

As of now the energy dashboard says 7kwh, but clean energy meter is reading as 12.8kwh, so the meter reset, jumped to 12.17kwh, and has only accumulated 0.8kwh since 12:01am

The jump is also weird as the template pushed a -12.17kwh value into the recorder

I’ve been trying wrap my head around why this is happening. Gonna think on this for a while and report back.

Here’s what I figured after I thought a while @petro

configuration.yaml

# Include Templates
template: !include_dir_list templates/
# Enable Custom Sensors
sensor: !include_dir_list sensors/

Create Grid Power Sensors

templates/utilities/grid_sensors.yaml

sensor:
  - name: Grid Import Power
    state_class: measurement
    icon: mdi:transmission-tower
    unit_of_measurement: W
    device_class: power
    state: >
        {{ [0, states('sensor.envoy_SERIALNUMBER_current_power_consumption') | int - states('sensor.envoy_SERIALNUMBER_current_power_production') | int ] | max }}

  - name: Grid Export Power
    state_class: measurement
    icon: mdi:transmission-tower
    unit_of_measurement: W
    device_class: power
    state: >
        {{ [0, states('sensor.envoy_SERIALNUMBER_current_power_production') | int - states('sensor.envoy_SERIALNUMBER_current_power_consumption') | int ] | max }}

Create Grid Fossil Fuel & Low Carbon Power Sensors

templates/utilities/carbon_sensors.yaml

sensor:
  - name:  Grid Fossil Fuel Power
    state_class: measurement
    unit_of_measurement: W
    device_class: power
    state: >
        {{ (states('sensor.grid_import_power') | float * states('sensor.co2_signal_grid_fossil_fuel_percentage') | float / 100) | round(0) }}
    availability: >
        {{ states('sensor.grid_import_power') | is_number and states('sensor.co2_signal_grid_fossil_fuel_percentage') | is_number }}

  - name:  Grid Low Carbon Power
    state_class: measurement
    unit_of_measurement: W
    device_class: power
    state: >
        {{ (states('sensor.grid_import_power') | float * (1 - states('sensor.co2_signal_grid_fossil_fuel_percentage') | float / 100)) | round(0) }}
    availability: >
        {{ states('sensor.grid_import_power') | is_number and states('sensor.co2_signal_grid_fossil_fuel_percentage') | is_number }}

Create Grid Fossil Fuel & Low Carbon Energy Integration Sensors

sensors/grid_fossil_fuel_energy.yaml

platform: integration
name: Grid Fossil Fuel Energy
source: sensor.grid_fossil_fuel_power
unit_prefix: k
unit_time: h
method: left

sensors/grid_low_carbon_energy.yaml

platform: integration
name: Grid Low Carbon Energy
source: sensor.grid_low_carbon_power
unit_prefix: k
unit_time: h
method: left

Then, create a utility meter for the new Fossil Fuel, and Low Carbon Energy Sensors.

1 Like