# Automated CO₂ reduction calculation

Hello together!

i kindly request some help…

I’m stuck for some days, hence i don’t get a satisfying solution to create a utility meter or counter, thats showing me my “reduced” CO² in kg, while creating energy via PV.

The idea is to create a nearly “live” increasing counter.

The given sensors are:

• actual gramms CO² per kWh in the grid via
“sensor.electricity_maps_co2_intensitat”

• actual produced PV power in W via
“sensor.pv_leistung_kombiniert”

I tried different ways and helpers with utility meters and or riemmans integral calculations, templates and more…

My last idea was, to create a automation, with some step by step - steps.
every hour, take the created kwh, multiplied with the average CO² intensity of the past 60 min.
After this, add this value to a counter or utility meter.

finally reset the kWh counter to start the hour from 0 and the cycle can begin again.

jinja isn’t my strenght

Here’s what i got so far:
Shows automation yaml code

``````alias: CO² Berechnen
description: ""
trigger:
- platform: time_pattern
hours: /1
condition: []
action:
- sequence:
- variables:
template:
- sensor:
- name: CO² in g
unit_of_measurement: g
device_class: weight
state_calss: measurement
state: >
{% set co2_gkwh = states('sensor.average_co2') | float | default(335) %}
{% set energy_kwh = states('sensor.produzierte_leistung_fur_co2_berechnung_zahler') | float | default(0) %}
{{ (co2_gkwh * energy_kwh) | float | round(3) }}
- service: utility_meter.calibrate
target:
entity_id: sensor.produzierte_leistung_fur_co2_berechnung_zahler
data:
value: "0"
mode: single
``````

Has anyone some idea???

If you have power (W) then you first have to calculate energy with the Riemann Sum integration. Once you have that sensor you can simply multiply that new sensor with your co2/kg sensor.

If i’m simply multiplying the produced kWh with the CO²/kWh value,
i always get the product for the whole produced energy, with the “at the moment CO²/kWh” value.
Sorry, it’s hard to explain…

What i want is a time window, of lets say 15 min, count the energy within, multiply with the average CO²/kWh of the last 15 min and add this value to a utility meter… and do this again

it may be helpful for you to re-read your information and take another swing at explaining what you want. or write the pseudocode for what you want.

in your first description, you talk about two sensors… but in your code example, you’re not using either and you’re using two different sensors. so i’m not sure what you want there.

in your reply just now you talked about needing to have a 15 minute sampling, which you didn’t talk about above, so no one could have predicted that as a requirement.

in the recent reply you said you get ony a “at the moment” value, but your sensor actually says “sensor.average_co2” which sounds like it’s not “at the moment” but is more an average over some time period… and if it’s actually an average over a time period, then perhaps the ideal thing is to get the kwh produced during that same time period?

anyways, i think the problem posed is unclear and a bit confusing. it would help if you could clarify… and i personally like clarifications that are pedantic and even pseudo-coded out… ie, write the code that you wish would work, and we can help turn that wish coding into real code.

I’m sorry to confuse you. This wasn’t my intention.

As i said, i try to create a sensor, or value, that shows me the reduced or saved CO² in kg.

I also tried many different ways to do so.
Maybe thats why i’m jumping in my thoughts. Verry sorry for that.

I have 2 sensors as input for that, the CO² per kWh (variable) and my actual solar power from my PV array. (also variable)

I try to make this calculation as exact as possible. (nearly in time)
Shure, i could collect the kWh per day and multiply this with the average CO²/kWh of this day, BUT i want it more detailed

I hope this clears some questions and i’m also thankful for your help.

ok, so we should ignore these:

?

“sensor.pv_leistung_kombiniert” this is my power from my solar array in “W”

The other one “sensor.electricity_maps_co2_intensitat” is the CO² per kWh

in your automation, you don’t use either of those and you use these two instead:

so i’m still not following what you want.

i think it would still be helpful and clearer if you would write the psuedo code of what you wish would work.

taking a guess as to what i understand so far… and doing it with number helpers because it’s easier to prototype… the below will calculate a value every 15 minutes, and aggregate the value during the hour.

then at the end of the hour, it will allow you to do something with the aggregated value and also reset the value to start calculating again.

you can change the /15 to /5 or whatever to increase the granularity of calculation.

does that get closer to what you want?

``````trigger:
- platform: time_pattern
minutes: /15
id: increment
- platform: time_pattern
hours: /1
id: hourly
condition: []
action:
- choose:
- conditions:
- condition: trigger
id:
- increment
sequence:
- service: input_number.set_value
data:
value: >
{% set co2_gkwh = states('sensor.average_co2') | float |
default(335) %}
{% set energy_kwh =   states('sensor.produzierte_leistung_fur_co2_berechnung_zahler') | float |   default(0) %}
{{ states(input_number.aggregate) | float +  (co2_gkwh * energy_kwh) | float | round(3) }}
target:
entity_id: input_number.aggregate
#### is this "calibrate" call needed here in order to start the new total kWh value?
- service: utility_meter.calibrate
target:
entity_id: sensor.produzierte_leistung_fur_co2_berechnung_zahler
data:
value: "0"
- conditions:
- condition: trigger
id:
- hourly
sequence:
####  do soething with the value you've accumulated.
- service: input_number.set_value
data:
value: 0
target:
entity_id: input_number.aggregate
mode: single

``````

however this is a poor reimann sum approach. you could create a simple sensor with just the “last 15” value then use the reinmann sum integration to do the aggregation calculation.

intent with the above code is just to clarify understanding of the question.

Yes, this looks good

Now i only need to put this “input.number” into a counter?

input_number.aggregate is a helper. you can create it in settings->helper

an you shouldn’t need to create a counter for it. look at the last line here:

it’s adding the value to itself, so it’s already counting up each iteration until it’s reset hourly.

if this is directionally right, as i said, the “better” answer is to turn this into a sensor that’s updated every couple minutes and then use this integration:

to do the integration of the curve.

if you do that, then you don’t actually need to do the summing. just create the sensor that updates every 15 minutes with the 15 minute value (or 5 minutes or whatever)

oh, i edited the code to move the calibrate call. i’m not sure what it’s supposed to do, but thinking about it, i’m guessing that you need to call it each 15 minute block to “restart” the meter on how much kWh is generated? is that the purpose of calling calibrate? if so, the edit i just made to the code does that.