I’m running openHAB 5.0.3 on Raspberry PI 4, I have added a bunch of Tuya smart plugs using Tuya binding and it’s all working fine - meaning I am able to switch them on/off and see current current Voltage, Power and Current. It’s all added via UI, items are attached as equipment to semantic model and I see nice charts for the above measures.
What I want to achieve is the measurment of power consumption in kWh along with some history, so that I could see how much power was cosumed in a given month, day or hour. I assume I need to calculate it using current power consumption item and put into a new item, but how to do it? Should it be a rule firing every few seconds, doing the calculation and updating item? Maybe there is a smarter way, or even out of the box extension/script I could use?
Ideally an energy item could be linked to a power (or current) channel via a profile that would calculate the integral. However so far as I know, nobody has yet written such a profile. ?? If that is true, maybe I will write something when I have some free time.
I guess you have an Item to persist the power values as you can visualize them in the UI. That together with the persistence functions you can easily calculate the power consumption over a given time period. You may use [PowerItem].riemannSumSince(ZonedDateTime) to sum up all individual power values since a given start time. Depending on what you want to do with that result, you may assign that to another Item, persist it and update per your needs.
I use a different plug (Tapo P110) and that has kWh for the day, so I can have a rule running at midnight and get totals for month, year, etc so do not need the ‘real time’ calculation that you do - However I also use an State O-Matic Binding thing (org.openhab.binding.omatic) for monitoring and running rules for my dishwasher, washing machine, etc:
This binding will show the current kWh consumption, total kWh consumption, runtime, and even costs. These can easily be picked up in rules and manipulated even further if needed. Whilst this is not the intended use, I think it might possibly suffice for your needs and may be worth a quick look??
Looks nice! So if I wanted to add a new item that would persist the power consumption values, should I i.e. create a rule that would fire every hour, calculate the consumption during the last houw and assign it to the item supposed to keep the track of consumption?
Or is there a better way where I could use these persistence functions and keep the history of power consumptions?
Yeah, i understand. However you will never get a true accurate value from those plugs unless it also reads the actual supply voltage and, more importantly, the power factor (unless you are using only 100% resitive loads and therefore a power factor of 1).
I would say that if you need acurate readings then youll need to replace the plugs with something else.
here an example how I use the riemannSumSince() function to aggregate Power [W] to Energy [kWh]. I have 2 Items:
myPower as Number:Power
myEnergy as Number:Energy
And I have a DSL rule to trigger the aggregation like this:
val tmpEnergy = myPower.riemannSumSince(MidNight)
myEnergy.postUpdate(tmpEnergy)
The riemannSumSince() function returns Wmin but the UoM of myEnergy Item setup as kWh takes care of the conversion. That gets you the daily Energy consumption. My rule is triggered by every change of the myPower Item, but you can setup time (cron) based trigger as you like - works too.