How to convert kWh to euros and CO2?


I have several energy meter in OpenHab. I’m setting up a a sum of all of them.
I would like to have this sum also available in euros and CO2.
The formula are very simple:

  • cost (euro) = 0.1206 * kWh
  • Co2 (kg) = 0.09 * kWh
    Is there a configuration file to do these transformations ?

Here are some ideas. Define items like these:

Group:Number:SUM kWh "kWh [%.2f kWh]"
Group:Number:SUM Cost "Cost [€%.2f]"
Group:Number:SUM CO2 "CO2 [%.2f kg]"

Number Meter1_kWh "Meter1_kWh [%.1f kWh]" (kWh)
Number Meter1_Cost "Meter1_Euros [€%.2f]" (Cost)
Number Meter1_CO2 "Meter1_CO2 [%.2f kg]" (CO2)

Number Meter2_kWh "Meter2_kWh [%.1f kWh]" (kWh)
Number Meter2_Cost "Meter2_Euros [€%.2f]" (Cost)
Number Meter2_CO2 "Meter2_CO2 [%.2f kg]" (CO2)

Write rules that update the Euros and CO2 items when kWh is updated:

import org.openhab.core.library.types.*

val Number kWh_cost = 0.1206
val Number kWh_co2 = 0.09

rule UpdateMeter1Costs
  Item Meter1_kWh changed
  Meter1_Cost.postUpdate((Meter1_kWh.state as DecimalType) * kWh_cost)
  Meter1_CO2.postUpdate((Meter1_kWh.state as DecimalType) * kWh_co2)

rule UpdateMeter2Costs
  Item Meter2_kWh changed
  Meter2_Cost.postUpdate((Meter2_kWh.state as DecimalType) * kWh_cost)
  Meter2_CO2.postUpdate((Meter2_kWh.state as DecimalType) * kWh_co2)

(@rlkoshak has a clever technique for collapsing this into fewer rules)

The point is that your Group items will give you a total across all members, and when a kWh value is updated, your rule will calculate the companion cost items, which will then automatically update the groups.

You could trigger another rule at midnight to persist the day’s values and reset them all to 0.

1 Like

Thanks a lot for your fast reply.

Hi there!
Could you please help me with the following:
I have a dimmer 2 which works ok with the example you posted above.
Than I have a FGS 222 which doesn’t have the power consumption measurement option so knowing that I have 3 lights attached to it, each one 9w (2+1) I would like to make a rule that will display 9w when 1 light is on and 27w when all 3 lights are on. I would like to have also the total of kWh and to include them in the sum group.
Could you please help with that?

Note that kWh is not the same as Watts. kWh measures consumption over time, while Watts is only what is consumed at one moment. You could approximate consumption by having a rule that triggered when a light was turned off, and did maths to compute kWh for how long the light was on. But for the simpler goal you described, something like this?

Group:Number:SUM LightsWatts "Lights Watts [%d w]"

Number Light1Watts "Light 1 Watts [%d w]" (LightsWatts)
Number Light2Watts "Light 2 Watts [%d w]" (LightsWatts)
Number Light3Watts "Light 3 Watts [%d w]" (LightsWatts)
rule Light1Changed
  Item Light1 changed
  Light1Watts.postUpdate( if (Light1.state == ON) 9 else 0)

rule Light2Changed
  Item Light2 changed
  Light2Watts.postUpdate( if (Light2.state == ON) 9 else 0)

rule Light3Changed
  Item Light3 changed
  Light3Watts.postUpdate( if (Light3.state == ON) 9 else 0)

Thanks man for your time.
I made the rule like this:

Group:Number:SUM LightsWatts "Lights Watts [%d w]"
Number Bedroom_power1 "Consum curent iluminat Dormitor1 [%.1f W]" (LightsWatts)
Number Bedroom_power2 "Consum curent iluminat Dormitor2 [%.1f W]" (LightsWatts)

rule Light1mode
Item bedroom_light_one changed
Bedroom_power1.postUpdate( if (bedroom_light_one.state == ON) 9 else 0)

rule Light2mode
Item bedroom_light_two changed
Bedroom_power2.postUpdate( if (bedroom_light_two.state == ON) 18 else 0)

and works great so if i switch the first light on ill see 9 w and if ill switch the 2nd one on ill see 18 w. if i switch the both lights on ill see 27 w which is great.

Now how do i make them store the kwh? As i said above i already have a dimmer 2 which shows me the instant power consumption, total kwh (since the dimmer installation) and the costs in a group called cost:
Group:Number:SUM Cost "cost [%2f]"
So the next step will be to display the kwh starting now till ill delete the item :slight_smile: and to integrate this in the cost group.
Thanks for your time.

I think there could be some good advice here:

I found out that i was asking for help on that topic more than 1 year ago :slight_smile:
Anyhow its a good way to start. I found and adapted this rule:
Item Bedroom_power1 received update
var long currentTime = now.millis
if (LastUpdate != 0) {
var long timeElapsed = currentTime - LastUpdate
if (timeElapsed > 0) {
var Number power = Bedroom_power1.state as DecimalType
var Number energyConsumption = (power * timeElapsed) / 3600000 / 1000 // kWh
postUpdate(Bedroom_energy, Bedroom_energy.state as DecimalType + energyConsumption) //increment
LastUpdate = currentTime
The item Bedroom_power1 displays the instant power consumption for the 1st light and its one of the two items in the group Group:Number:SUM LightsWatts “Lights Watts [%d w]” which works great with the rule you showed me. The Bedroom_energy item should display the kWh but unfortunately i need to use the group LightsWatts which contains both lights and displays the sum and i think i need the sum value in this rule.
Anyhow trying both the group item or one of the two W items its not showing anything in the sitemap…
Could you please take a look o the rule and maybe point me the right direction?

Would it make sense to continue the questions in that discussion, since many past participants might be subscribed to it still?

I don’t know maybe you right, I posted on that topic too but i saw that no one posted anything since mar 13 and the all discussion was made between 3 people so maybe that topic is not quite the right way to get some help…

Could you keep a new set of items and SUMmed group to contain them which holds total kWh, alongside the items which hold the simple wattage? Then augment your rules to compute the running kWh counters, update the respective items, and the Group item over the individual kWh items will contain the total used? This, along with persistence and restoreOnStartup, should get you approximated kWh usage of your lights.

Unfortunately for me I don’t know how to do this and I’m still trying to make this rule posted above to convert w to kWh.