[SOLVED] Energy cost calculation based on runtime

Hi community,

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.

// Electricity_Unit_Cost = ÂŁ0.18kWh
MyDevice1_Energyused_Cost = MyDevice1_Power_Used * Electricity_Unit_Cost

I would really appreciate any support with the above

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

2019-03-10 00:05:13.723 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'MyDevice1_Energyused_Cost' for widget org.eclipse.smarthome.model.sitemap.Text
2019-03-10 00:05:13.727 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'MyDevice1_Energyused_Cost' for widget org.eclipse.smarthome.model.sitemap.Text
2019-03-10 00:05:13.732 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Text

Assuming that missing something fundamental in my rule.

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

Hi Stuart,

Sorry for late response, been away this week.

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

Hi Andy,

the trigger for your rule must be an event.

rule "Energy cost"
when
  Item MyDevice1_Time_Day changed
then
  MyDevice1_Energyused_Cost.postUpdate(MyDevicce1_Time_Day.state as number * Energy)
end

Tried the above rule but I get the following error in log

2019-04-03 22:44:50.229 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'home.rules'
2019-04-03 22:45:05.099 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Energy cost': The name 'MyDevice2_Energyused_Cost' cannot be resolved to an item or type; line 483, column 5, length 25
2019-04-03 22:45:20.555 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'MyDevice2_Energyused_Cost' for widget org.eclipse.smarthome.model.sitemap.Text
2019-04-03 22:45:20.561 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'MyDevice2_Energyused_Cost' for widget org.eclipse.smarthome.model.sitemap.Text
2019-04-03 22:45:20.566 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Text
2019-04-03 22:45:45.487 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'home.items', using it anyway:
2019-04-03 22:48:05.085 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Energy cost': Could not cast 183 to void; line 483, column 42, length 34
2019-04-03 22:49:05.111 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Energy cost': Could not cast 184 to void; line 483, column 42, length 34

I only changed Device 1 to Device 2.

To summarise my syntax

Rule

rule "Energy cost"
when
  Item MyDevice2_Time_Day changed
then
  MyDevice2_Energyused_Cost.postUpdate(MyDevice2_Time_Day.state as number * Energy)
end

Sitemap

Text item=MyDevice2_Energyused_Cost

Items

Number MyDevice2_Energyused_Cost "Energy cost"

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 :disappointed:

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.

Hi,

Changed my rule slightly but still no result.

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 :neutral_face:
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

As you want to change the state of an Item, you have to use

MyDevice2_Energyused_Cost.postUpdate((MyDevice2_Time_Active.state as Number * Energy) / 60)

This is the (preferred) Method of the Item. Another option would be the according action:

postUpdate(MyDevice2_Energyused_Cost, (MyDevice2_Time_Active.state as Number * Energy) / 60)
1 Like

Thanks @Udo_Hartmann . That did the trick, it calculates properly, now I know where the mistake was previously.

You have worked with postUpdate last week… :wink:

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.

Can you please post the final result please ,I wood like to be able to do this to thanks :smile:
Items , Sitemap and Rule and if there is more ?