Calculating energy cost and power usage knowing device running time

energy
rules
openhab2
Tags: #<Tag:0x00007f51dffc8c70> #<Tag:0x00007f51dffc8ae0> #<Tag:0x00007f51dffc8888>

(Andy) #1

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


(Andy) #2

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.


(Stuart Hanlon) #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

[Solved] Cron trigger - Every 5 minutes works, daily doesn't
(Andy) #4

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

(Andreas Imhof) #5

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