AVG Temp of last 12h

can i calculate the average temperature of a given time period?

Maybe avg. temp of last 6h/12h/24h?

I´m logging outside temperature every minute to a rrd4j-database.

And will rrd4j-database be still accurate enough with the data of last 24h or is there to much rounding already?

Rrd4j will save data as set in the respective config.
If you go with the default setup it will keep your exact values for every minute for the time frame of the last 8 houres. For longer time frames the values will be “compressed” , like one value for each 14 minutes in a frame of 159 houres…
When using a custom config you can keep each minute value for the last 24 houres. Additionally you can use rrd4j to keep a singe value for each 6 houre step in a time frame of your decision . This value could be configured to be the average.
You have to understand that rrd4j will not have any value beyond the respective timeframe. For the default setup there is no minute value keep beyond the 8 houres.

Hm, i don´t know, what you exactly want to say me with one single value each 6 hours step…

Temperature is changing maybe every minute, so average temp is changing also. So i don´t see any need for one single item in a time period of 6 hours???

Ok, then let´s do it for the last 6h at the moment.

How can i get the average temp of the last 6 hours out of the rrd4j-database?

Later maybe i will change rrd4j config to save more values. Whre can i find the config-file to change this?

you should read this: https://github.com/openhab/openhab/wiki/Persistence

Ok, i can edit openhab.cfg to do this.

Do i have to delete all my rrd4j databases first or can i keep them? I don´t want to lose all my data.

There is no explanation for “average” in the wiki.

Is this not possible?

Yes, there is an explanation for average in the wiki, see:

item.averageSince(AbstractInstant) - Gets the average value of the state of a given item since a certain point in time.

for example


I tried to point out that by using rrd4j in the default setting, you are getting a database that holds the saved value for every minute only for the last 8 houres. The next archive in the default setting holds one value for every 14 minutes for 156 houres. There are more archives in the default setting.
If you want to get an exact average for the last 6, 12 and 24 houres, you can either change the default setting or you have to use the compressed data that rrd4j holds.
The compression itself , which is done by rrd4j, is taking the average ( if not set differently) of during the time steps ( in the above mentioned second archive the average of 14 minutes).
Using this compressed data, the items function mentioned by @spy0r will calculate the average.

And I believe YES, you will delete all saved data if changing the settings of rrd4j.

Can i use this directly in a sitemap or can i only use this in rules?

I have one item called “Temp_Outside” and i want to get the average 6h Temp on my Display.

Good question, i think it does only work in rules or scripts.

Create an extra Temp_Outside_Avrg Number Item and create a simple rule:

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import java.util.Calendar
import java.util.Date
import java.util.TimeZone
import java.text.SimpleDateFormat

rule "Temperature Average"
    Item Temp_Outside received update

Something like that should work.

Thanks, that works just fine!

Using influxdb 0.13 as persistence provider, I am not getting the expected results from the averageSince function.
Maybe my expectations are wrong, but I would expect it to weight the values with the time they where valid.

Simple example:

Using a temperature item which is stored on every change.

12:00 - 20.0
12:20 - 22.0

So, it was 20.0 for 20 minutes and after that 22.0 for 40 minutes

If I query this at 13:00 with temp.averageSince(now.minusHours(1)), I would expect it to return

20.0 * (20/60) + 22.0 * (40/60) = 21.333333

but it seems to return

(20.0 + 22.0) / 2 = 21.0


I’m trying to do something similar and wanted to reuse your little script with some modifications.

Basically what I’m after is creaing items with the highest and lowest temperature value during the last 7 days. I have my temp sensor logging values every 10 minutes in a mariaDB instance, everything works fine.

I created an item (number) called “HighTempWeek” and then created a rule that looks like this:

rule “Weekly high”
Item Ute_Temperature received update

but the script doesnt work and I get the error message:

[ERROR] [untime.internal.engine.RuleEngineImpl] - Rule ‘Weekly high’: An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.postUpdate(org.eclipse.smarthome.core.items.Item,org.eclipse.smarthome.core.types.State) on instance: null

Any ideas what I’m doing wrong?

Try to post your Value to the log first, instead of putting it in the target item:

logInfo("Debug", Ute_Temperature.maximumSince(now.minusDays(7)).toString())

Your persistence service is properly configured?

Thanks for your support.

Output from your codestring when executed in the script:

12:42:56.610 [INFO ] [.eclipse.smarthome.model.script.Debug] - org.eclipse.smarthome.model.persistence.extensions.PersistenceExtensions$1@33bed0e8
12:49:44.369 [INFO ] [.eclipse.smarthome.model.script.Debug] - org.eclipse.smarthome.model.persistence.extensions.PersistenceExtensions$1@411d9018

The persistence service seems properly configured. The service stores sensor data in a table with two colums, time and value. The “time” column is of type timestamp and value is of type double. I havent created the schema myself, everything was auto generated by the persistence service.

Any advice?

that’s not what is supposed to get back here…
Since i have no testing object during the day… This looks like you want to access an Item value, but forget to use the .state statement. But in persistence cases, the syntax isn’t wrong at all i think.
If we assume that the persistence service is configured correctly, try to shrink the period and/or the statement first.

What if you replace yout statement with something like:

logInfo("Debug", Ute_Temperature.averageSince(now.minusHours(2)).toString())

Is the reply a valid number then?

This works since several years without any problems:

Thanks for your tip. Unfortunately though it doesnt work for me :frowning:

This is a sample output from the log after I customized your rule

13:12:21.709 [WARN ] [.ui.internal.items.ItemUIRegistryImpl] - Exception while formatting value '24.9 °C (13:12)' of item WeekTempHigh with format '%.1f °C': f != java.lang.String

It seems like it both fetches the value and time. If I just can get rid of the timestamp I guess it will work.

Displaying the Item in habpanel shows this

I also want to be able to display the item in openhab for android and that doesnt work right now…

Got it to work also in my phone now, thanks a lot!

Is there a way to remove the timestamp?