Hi. I use influxdb.
In db there is no data on 1.01.2017, data starts from 5.01.2017.
When I try get data with DayOfMonth(1) I got error.
How to get first valid data from db?
Use previousState()
to get the most recent value and previousState(true)
to get the most recent value that is different from the Item’s current state.
I think perhaps the question is “how to get oldest entry” or even “how to get oldest entry for month XX” ?
Ah, that is what he meant by “first valid data from db?”
In that case, I don’t know if there is a good what to do this. What error do you get? Is the error on the line where you call historicState or the line where you try to use the historicState?
Hi. @rossko57 you right, i mean exactly “oldest” value.
@rlkoshak error is:
cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null
val d = GasCounter.historicState(now.withTimeAtStartOfDay.withDayOfMonth(1)).state as Number
val e = GasCounter.historicState(now.withTimeAtStartOfDay.minusMonths(1).withDayOfMonth(1)).state as Number
If data in db starts not from first day of month - i got error.
OK, that is something we can work with. There is no simple single call way to do this but you can use a while loop to keep moving forward until you get a non-null return value.
val time = now.withTimeAtStartOfDay.withDayOfMonth(1)
val pastState = GasCounter.historicState(time)
while(pastState == null && time.isBefore(now)) {
time = time.plusDays(1)
pastState = GasCounter.historicState(time)
}
if(pastState == null) logError("Gas Counter", "Did not find a historic state starting from " + now.withTimeAtStartOfDay.withDayOfMonth(1) + " to present")
else {
// do stuff
}
Thank you @rlkoshak, your code works perfect
if variable time
is changed after definition, may be it must declared as var
?
i have a pv-plant, i would like to have the timestamp of the first state where the pv-plant’s power is bigger then 0, how this would look like ?
i only persist on change to influxdb, how do i get the first datarecord from today ?
There is no way to do this from Rules DSL that I know of. You can get the maximum or minimum over a period of time. You can get the sum or average over a period of time. You can get the state at a specific period of time. That’s it.
You will have to use the REST API to query for all the values and write the code to parse the results to find the value you need. See the REST Docs (installable from the Misc tab in PaperUI) under the Persistence category and the sendHttpGetRequest Action.
Same answer as above.