I would like to ask you for help to create rule or JS script calculating energy cost and power usage as still not very familiar with scripting yet.
I already calculating device runtime so currently displaying minutes running and total runtime during day in sitemap.
MyDevice1_Time_Active - Time Since device started
MyDevice1_Time_Day - Total Runtime for each day (reset at midnight)
I thoughts on that is this could be calculated using the following method but not exactly sure how to put it together hence asking for help in community.
My thoughts on calculations so far:
// This calculates power usage in kWh
MyDevice1_Power_Used = ((1000W * MyDevice1_Time_Day) / 60 minutes ) / 1000
In terms of energy cost perhaps something like this could be used.
Having issue to implement it. This is what I done so far.
Created two items:
/* Electricity cost and power
Number MyDevice1_Power_Used "Power"
Number MyDevice1_Energyused_Cost "Energy cost"
Sitemap
Text item=MyDevice1_Power_Used
Text item=MyDevice1_Energyused_Cost
And then created this rule to calculate energy usage
var Energy = 0.18 //located at the top of rule file
//Rule to calculate energy
rule "Energy cost"
when
Item MyDevice1_Time_Day > 0
then
MyDevice1_MyDevice1_Energyused_Cost = MyDevice1_Time_Day * Energy
end
My design doesn`t seems to work, in the log can see the following errors
Assuming that missing something fundamental in my rule.
MDAR
(Stuart Hanlon, UK importer of Velbus hardware)
3
Hi Andy
I think you’re missing .state in the maths.
Or even .state as Number
I created this rule yesterday, which seems to be working.
It uses a group to hold the ON states of my heating zones.
I have a couple of persisted Number items to hold the values.
Instead if holding the unit cost in the rules, I have it as a number item so that I can change it in a UI.
// I need to re-write this rule so that every 5 minutes it calculates the cost and adds that value to
// the daily, monthly & yearly total.
// then Zero the daily at midnight, monthly on the 1st and yearly on Jan 1st
// Number items used in this rule
//
// HeatingCostPer15Minutes
//
// HeatingCostDaily
//
// HeatingCostDailyAccum
//
// HeatingCostMonth
//
// HeatingCostYear
//
// gGroup_switch_count.state
rule "Accumlate Heating costs every 5 minutes"
when
Time cron "0 0/5 * 1/1 * ? *" // change to "0 0/15 * 1/1 * ? *" for every 15 minutes
or Item testtrigger changed // Only a test switch to force the rule to run for testing
// or Time cron "0 0/1 * 1/1 * ? *" // Testing
then
val Number count = gGroup_switch_count.state
val Number quartercost = (HeatingCostPer15Minutes.state as Number)
val Number Accum = HeatingCostDailyAccum.state
val Number cost = Accum + (count * quartercost)
HeatingCostDailyAccum.postUpdate(cost)
end
rule "Daily Cost"
when
Time cron "0 59 23 1/1 * ? *" // 1 minute to Midnight every day
or Item testtrigger changed // Only a test switch to force the rule to run for testing
// or Time cron "0 0/3 * 1/1 * ? *" // Testing
then
val Day = HeatingCostDailyAccum.state as Number
val DayCost = Day * 0.01
HeatingCostDaily.postUpdate(DayCost)
val Number month = (HeatingCostMonth.state as Number) + DayCost
HeatingCostMonth.postUpdate(month)
HeatingCostDailyAccum.postUpdate(0)
end
rule "Month Cost"
when
Time cron "0 1 0 1 1/1 ? *" // Every 1st of month at 1 minute past Midnight
or Item testtrigger changed // Only a test switch to force the rule to run for testing
// or Time cron "0 0/6 * 1/1 * ? *" // Testing
then
val Number year = HeatingCostMonth.state as Number + HeatingCostYear.state as Number
HeatingCostMonth.postUpdate(0)
HeatingCostYear.postUpdate(year)
end
rule "Year Cost"
when
Time cron "0 2 0 1 1 ? *" // Every year on Jan 1st at 2 minutes past midnight
or Item testtrigger changed // Only a test switch to force the rule to run for testing
// or Time cron "0 0/30 * 1/1 * ? *" // Testing
then
HeatingCostYear.postUpdate(0)
end
I tried amending my rule , added .state but still doesn’t seems to work. Not sure what else could I try.
//Rule to calculate energy
rule "Energy cost"
when
Item MyDevice2_Time_Day.state as Number > 0
then
MyDevice2_Energyused_Cost.state as Number = (MyDevice2_Time_Day * Energy)
end
The item should have a place holder for the value (e.g. [%0.2f]) within the string…
Did the item file load successfully? The second line of your log says it can’t find the item…
Just restart openhab to look for errors on start-up and see if the files (items, rules and sitemaps) are loaded correctly and the inital state of your items are set…
Thank @imhofa , tried that but doesn’t seems to be working.
Maybe I`m making wrong calculation?
Lets say my device is consuming 1000W (1kW) if used for 1 hour. If we take that 1kWh is at the cost of ÂŁ0.18 I should probably use this rule:
rule "Energy cost"
when
Item MyDevice2_Time_Active changed //Shown in minutes on sitemap - this value increases every minute if device is switched ON
then
MyDevice2_Energyused_Cost = ((MyDevice2_Time_Active.state as Number) * Energy / 60)
end
Legend:
MyDevice2_Time_Active // This tag is showing in minutes on my sitemap and updates every 1 minute if device is switched ON.
I tried this now and restarted OH but still no joy
Looking at the log can see the following error
2019-04-05 23:05:05.088 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Energy cost': An error occurred during the script execution: Cannot assign a value in null context.
Any ideas? My rule seems to be basic, just want to multiply two values and then divide it by 60.
rule "Energy cost"
when
Item MyDevice2_Time_Active changed
then
val Number MyDevice2_Energyused_Cost = ((MyDevice2_Time_Active.state as Number) * Energy / 60)
end
Would anyone be able to offer some suggestions please ?
Hi Andy,
Just a couple of ideas.
Have you assigned a value to energy?
I use these lines of code
var Number power93 = itm_uber1_HotWater_current_mqtt.state as DecimalType * 240
var Number energyConsumption93 = (power93 * timeElapsed93) / 3600000 / 1000 // kWh
Hi Chris,
Thanks for suggestions. I have amended my rule, changed val to var but still is not parsing at all. Everything seems to be working well on excel
I will post my configs again perhaps you can spot mistake somewhere.
Sitemap
Default item=MyDevice2_Energyused_Cost
Items
/* Electricity cost*/
Number MyDevice2_Energyused_Cost "Energy cost [%d]"
Rules
val Energy = 0.18 //Placed at the top of the rule file
rule "Energy cost"
when
Item MyDevice2_Time_Active changed
then
var Number MyDevice2_Energyused_Cost = (MyDevice2_Time_Active.state as Number * Energy) / 60
end
NB:
MyDevice2_Time_Active is displayed in minutes on my sitemap
I think last week I forgot to persist my item. Also one change I made was to change .state as Number to .state as DecimalType , all works now. Thanks to all of you for help guys.