Persist power Consumption (return wrong result)

Hello Guys.
I wanna persist my power consumption for my heatpump, into a MySQL Database.
I wanna see the consumption per day/month/year…

If i setup the rule to persist the consumption every day, and reset that value everyday at midnight, then i get a daily value of around 4-5.
If i wanna have the weekly result, can i then call the summarized consumption for the last 5 days?

Or do i need to let the value count up and not zero it every night, then if i wanna have the consumption yesterday, the persistence handles this, lets say yesterday midnight the value was 45, 24 hours later it was 49 - then the result is 4?

ItemName.sumSince(now.minusDays(5))

The only minor gotcha is you have to call this from within a Rule. So a second or so after midnight when your previous day’s consumption is persisted execute a Rule that updates separate Items for LastWeek, LastMonth, LastYear, etc.

Thanks @rlkoshak - as always :slight_smile:

The Designer can’t fidne the sumSince command - is any import needed before it accepts that - or just a compiler bug?
Also where to put the Service selection?

rule "Updater forbrug ved midnat"
	when
	Time is midnight
	then
	Thread::sleep(2000)
	var int daily1
	daily1 = Heatpump_total_kWh.sumSince(now.minusDays(1) "jdbc")
end		

You need a comma between the now.minusDays(1) and the “jdbc”.

Hmm can’t get this working, seems like the sumSince command isn’t allowed in OH2? deltaSince works…
Can someone see the fault in my statement?

rule "Opdater forbrug ved midnat"
	when
	Time is midnight
	then
	Thread::sleep(2000)
	Heatpump_total_kwh_daily.postUpdate(Heatpump_total_kWh.deltaSince(now.minusDays(1), "mysql").state as DecimalType).toString()
end	

Also tried without the state as DecimalType
The SmarthomeDesigner says:

Ambigious feature call.
The extension methods
postUpdate(Item, Number) in BusEvent and postUpdate(Item, State) in BusEvent both match

That seems unlikely. Does it actually not work or is it just Designer that is complaining about it?

I think you have a missing set of parens:

Heatpump_total_kwh_daily.postUpdate((Heatpump_total_kWh.deltaSince(now.minusDays(1), "mysql").state as DecimalType).toString)

This seems to work:

	Heatpump_total_kwh_daily.postUpdate((Heatpump_total_kWh.deltaSince(now.minusDays(1), "mysql").toString))	

But why do i need to convert a number to a string?

haven’t tested the sumSince, wanted to have the sentence correct - but will test it :slight_smile:

Under most circumstances you wouldn’t need to. But when dealing with NumberItems sometimes the Rules engine gets confused when you use an object that is of two different valid types. In this case the .state returns an Object that is both a Number and a State (i.e. DecimalType). Both of these two types are valid so the Rules engine doesn’t know which one you really mean.

If you are not a programmer or are familiar with object oriented programming languages the above probably doesn’t make much sense. Suffice it to say there is a good reason and you should only ever see this sort of problem when trying to sendCommand or postUpdate with a DecimalType.

Heh okay… I think it’s pretty hard to define what is actually an error and what is only a Designer error…

When doing the .toString it makes a number that’s not correct… after deleting that and donøt care about the compiler error it works… also the sumSince works :wink:

Designer has some bugs in it right now that is causing it to mark lots of things as wrong that are not wrong. Hopefully these will get fixed, at least in ESH Designer.

Yes ok - must be better simply to test it :slight_smile:

When doing the sumReturn command for lets say last day, week, month, year, i get some odd results - seems like it can’t handle to retrieve data for minus1day if the item persistence only started for 10 hours ago…
When “sumSince” last 5 hours it works like it should, but when saying 1 day i get a very odd number…

Is there a way so it only handles correct data?
Otherwise i need to do some time calculations so it don’t takes data that’s not there - it isn’t a problem for days and weeks, but for months and year it takes year/month before i can use the data to anything :slight_smile:

I don’t know but I doubt it. If there is a way it will involve changes to the source code itself and not something we can do within Rules.

when I wanna get some logged historical data I receive a wrong result, I do a continuous logging, meaning the power consumption just counts up… i also count number of days I’ve logged, to use this when I recall data for etc. a month and only have logged 3 days I only call for 3 days of data…
but I’m getting wrong results when using the sumSince command, is there any limitation on how
Many minutes I can go back?

If you are using rrd4j realize that as the data gets older it replaces, for example, ten sequential values with the average of those values. If you need precise data for data older than a day or so you need to use some other persistence.

Sorry forgot to tell that I use the jdbc MySQL persistence