Store an average for the past day(influxdb)

Hello
At midnight I calculate an average temperature coming from a persistence rrd4j and I record it in an influxdb presitence. The problem is that the date of the recording is the day of the calculations of the average. I would like the date to be the 1 day before. Is it possible ? How to do ?

rule "Daily temp average"
	when
		Time cron "0 0 0 * * ?"
	then
		tempBedroomAvg.postUpdate(tempBedroom.averageSince(now.minusDays(1))) //tempBedroom persistence : rrd4j
		tempBedroomAvg.persist("influxdb")//store with time "today at 00:00" I would like "yesterday at 00:00"
	end

Yes, do it just before midnight:

rule "Daily temp average"
when
    Time cron "58 59 23 * * ?"
then
    tempBedroomAvg.postUpdate(tempBedroom.averageSince(now.withTimeAtStartOfDay())) //tempBedroom persistence : rrd4j
    tempBedroomAvg.persist("influxdb")//store with time "today at 00:00" I would like "yesterday at 00:00"
end

Thank you but the point on the graph will be near to the next day.

With the REST API it is possible http://serveur.local:8080/rest/persistence/items/tempBedroomAvg?time=2019-02-15T00%3A00%3A00.000Z&state=18.6 but … I find the method not very clean

method store from Interface ModifiablePersistenceService)seems to be the ideal method but I do not understand how used it in a rule

InfluxDB is a time point database, you can’t insert values with arbitrary timestamps without manipulating the database calls in an SQL type query.
What you basically want to do is go back in time. InfluxDB doesn’t like that. It’s possible but not easy nor elegant.

1 Like

Okay ! Thank you ! This is the first use of influxdb I did not know
is it possible with another type of persistence?
If so, what is the best persistence for this? MardiaDB/MySQL?

Again, openHAB will store the value with a time stamp of when the value was stored so you will need to use an custom SQL query.
I don’t understand you issue very well though. The date is correct now. How can you calculate the average temperatures of a day without having the data. It needs to be done around midnight.
By persisting just before midnight you date is correct.

I created a rule on another item with a houry average

rule "Température moyennes"
	when
		Time cron "58 59 * * * ?"
	then
		tempChMobHouryAvgP.postUpdate(tempChMob.averageSince(now.minusHours(1), "rrd4j"))
		tempChMobHouryAvgP.persist("influxdb") //store every hour with time HH:59:58)
		//val rep = sendHttpPostRequest("http://localhost:8086/write?db=openhab", "", 'tempChMobHouryAvgH value=' + tempChMobHouryAvgP.state.toString + ' ' + ((now.getMillis()-30*60*1000)*1000000).toString ) //store every hour with time HH-1:29:58
	end

I draw a graph with highstock (highchart)
From null
blue : instant temp (tempChMob)
black : houry average temp (tempChMobHouryAvgP)

For a good visual result I would like the average curve to be more left.

Then move the cron back by 1/2 hour

I want to calculate the average between 14 and 15h if I do the cron at 14h30 I do not know the temperature between 14h30 and 15h

it works with
sendHttpPostRequest (“http: // localhost: 8086 / write? db = openhab”, “”, ‘tempChMobHouryAvgH value =’ + tempChMobHouryAvgP.state.toString + ‘’ + ((now.getMillis () - 30 * 60 * 1000) * 1000000) .toString) // store every hour with time HH-1: 29: 58
but I do not find very elegant.

thanks for the help