How to use time-series from rules (DSL or JS)?

Yes, that needs to be corrected.

To create a TimeSeries with add policy:

TimeSeries.new(:add)

@Mherwege the policy for add or replace is part of the timeseries class itself. Jruby is simply picking a default whereas in the Java API the policy must be specified when instantiating an object.

See: TimeSeries (openHAB Core 4.2.0-SNAPSHOT API)

The persistence service is the one that needs to remove the stored values to honour the policy of the given timeseries data.

Thanks. I missed that.

Ok, I changed the policy to add by TimeSeries.new(:add)
Yet that does not change anything, the past time stamp entries get removed.

Also, the time based triggers, both using every and cron lead to the same error as reported before.

And I realized I may need to move to OH4.2 to get the .averageBetween() function to work as desired - see: Return units in persistence extension commands and support future persisted states by mherwege · Pull Request #3736 · openhab/openhab-core · GitHub

Please post the full rule code

1 Like

Thanks for catching this error! I originally meant to have :add as the default, but then changed my mind, but forgot to update the example :slight_smile:

Hi @JimT,

I figured what the issue is. When using every or cron as the trigger, the |event| is missing. So I’m doing it like this now:

rule "Persist Time Series" do
	changed HTTP_URL_Thing_Voltego_VoltegoVerguetung_copy
	between "23:45".."23:59"
	run do |event|
		time_series = TimeSeries.new(:add)
...

This seems to do the trick. Now I need to find out how to make .averageBetween() behave.

Thanks!

No, it isn’t missing. When using every or cron, the event is a TimerEvent

It seems that you were checking for event.state. state isn’t a part of TimerEvent as it doesn’t make sense for a TimerEvent to contain a state data.

event.state is applicable for triggers that involve ItemState events such as Item changed and item updated events.

In other words, the type of the event object depends on the type of the trigger that triggered the rule.

This is important to know when you have multiple different types of triggers within a rule, e.g.

rule do
  changed MyItem
  every :day
  run do |event| # event here depends on which of the above triggers triggered us
    # .....
  end
end

Can you show a code that I can run in order to reproduce this issue?

Hi JimT,

first thanks for explaining the issue with the trigger type. That’s what I actually wanted to say: " When using every or cron as the trigger, the |event| is missing the expected type.

Here my JRuby rule code:

require "json"

rule "Persist Time Series" do
	changed HTTP_URL_Thing_Voltego_VoltegoVerguetung_copy
#	between "23:45".."23:59"
	run do |event|
		time_series = TimeSeries.new(:add)

		data = JSON.parse(event.state.to_s)
		data["elements"].each do |element|
			timestamp = ZonedDateTime.parse(element["begin"])
									.with_zone_same_local(ZoneId.system_default)
#									.with_zone_same_instant(ZoneId.system_default)
			time_series.add(timestamp, element["price"])
		end

		VoltegoVerguetungTS.time_series = time_series
    end
end

And here DSL rule code trying to make use of the timeseries:

when
	Time cron "20 14,29,44,59 * ? * * *"
then
	val sunActions = getActions("astro","astro:sun:local")
	val SunRiseTime = sunActions.getEventTime("SUN_RISE",ZonedDateTime.now,"START")
	val SunSetTime  = sunActions.getEventTime("SUN_SET",ZonedDateTime.now,"END")
	val AvgPreis = VoltegoVerguetungTS.averageBetween(SunRiseTime,SunSetTime)
	val MinPreis = VoltegoVerguetungTS.minimumBetween(SunRiseTime,SunSetTime)
	val MaxPreis = VoltegoVerguetungTS.maximumBetween(SunRiseTime,SunSetTime)
	logInfo("pv.rules","SunRiseTime: " + SunRiseTime + "; SunSetTime: " + SunSetTime + "; AvgPreis: " + AvgPreis)
	logInfo("pv.rules","MinPreis: " + MinPreis)
	logInfo("pv.rules","MaxPreis: " + MaxPreis)
end

What I see in the event.log is the time series gets updated on every change of the JSON by the JRuby rule:

2024-06-08 17:53:24.472 [INFO ] [hab.event.ItemTimeSeriesUpdatedEvent] - Item 'VoltegoVerguetungTS' updated timeseries [Entry[timestamp=2024-06-08T16:00:00Z, state=5.903], Entry[timestamp=2024-06-08T16:15:00Z, state=6.818], Entry[timestamp=2024-06-08T16:30:00Z, state=7.698], Entry[timestamp=2024-06-08T16:45:00Z, state=9.307], Entry[timestamp=2024-06-08T17:00:00Z, state=7.957], Entry[timestamp=2024-06-08T17:15:00Z, state=8.901], Entry[timestamp=2024-06-08T17:30:00Z, state=9.482], Entry[timestamp=2024-06-08T17:45:00Z, state=9.75], Entry[timestamp=2024-06-08T18:00:00Z, state=7.899], Entry[timestamp=2024-06-08T18:15:00Z, state=8.274], Entry[timestamp=2024-06-08T18:30:00Z, state=8.3], Entry[timestamp=2024-06-08T18:45:00Z, state=8.727], Entry[timestamp=2024-06-08T19:00:00Z, state=10.505], Entry[timestamp=2024-06-08T19:15:00Z, state=8.702], Entry[timestamp=2024-06-08T19:30:00Z, state=8.406], Entry[timestamp=2024-06-08T19:45:00Z, state=8.387], Entry[timestamp=2024-06-08T20:00:00Z, state=7.684], Entry[timestamp=2024-06-08T20:15:00Z, state=7.536], Entry[timestamp=2024-06-08T20:30:00Z, state=7.534], Entry[timestamp=2024-06-08T20:45:00Z, state=7.003], Entry[timestamp=2024-06-08T21:00:00Z, state=9.503], Entry[timestamp=2024-06-08T21:15:00Z, state=8.939], Entry[timestamp=2024-06-08T21:30:00Z, state=7.5], Entry[timestamp=2024-06-08T21:45:00Z, state=7.274], Entry[timestamp=2024-06-08T22:00:00Z, state=8.091], Entry[timestamp=2024-06-08T22:15:00Z, state=6.774], Entry[timestamp=2024-06-08T22:30:00Z, state=6.251], Entry[timestamp=2024-06-08T22:45:00Z, state=5.903], Entry[timestamp=2024-06-08T23:00:00Z, state=5.992], Entry[timestamp=2024-06-08T23:15:00Z, state=4.18], Entry[timestamp=2024-06-08T23:30:00Z, state=3.88], Entry[timestamp=2024-06-08T23:45:00Z, state=3.463], Entry[timestamp=2024-06-09T00:00:00Z, state=3.194], Entry[timestamp=2024-06-09T00:15:00Z, state=2.238], Entry[timestamp=2024-06-09T00:30:00Z, state=1.919], Entry[timestamp=2024-06-09T00:45:00Z, state=2.09], Entry[timestamp=2024-06-09T01:00:00Z, state=2.46], Entry[timestamp=2024-06-09T01:15:00Z, state=2.892], Entry[timestamp=2024-06-09T01:30:00Z, state=2.547], Entry[timestamp=2024-06-09T01:45:00Z, state=2.007], Entry[timestamp=2024-06-09T02:00:00Z, state=2.009], Entry[timestamp=2024-06-09T02:15:00Z, state=1.999], Entry[timestamp=2024-06-09T02:30:00Z, state=1.993], Entry[timestamp=2024-06-09T02:45:00Z, state=1.839], Entry[timestamp=2024-06-09T03:00:00Z, state=4.33], Entry[timestamp=2024-06-09T03:15:00Z, state=2.595], Entry[timestamp=2024-06-09T03:30:00Z, state=1.198], Entry[timestamp=2024-06-09T03:45:00Z, state=-1.281], Entry[timestamp=2024-06-09T04:00:00Z, state=6.197], Entry[timestamp=2024-06-09T04:15:00Z, state=2.293], Entry[timestamp=2024-06-09T04:30:00Z, state=0.004], Entry[timestamp=2024-06-09T04:45:00Z, state=-3.614], Entry[timestamp=2024-06-09T05:00:00Z, state=7.331], Entry[timestamp=2024-06-09T05:15:00Z, state=2.626], Entry[timestamp=2024-06-09T05:30:00Z, state=-3], Entry[timestamp=2024-06-09T05:45:00Z, state=-6.466], Entry[timestamp=2024-06-09T06:00:00Z, state=5.053], Entry[timestamp=2024-06-09T06:15:00Z, state=-0.007], Entry[timestamp=2024-06-09T06:30:00Z, state=-0.85], Entry[timestamp=2024-06-09T06:45:00Z, state=-4.996], Entry[timestamp=2024-06-09T07:00:00Z, state=1.797], Entry[timestamp=2024-06-09T07:15:00Z, state=-0.358], Entry[timestamp=2024-06-09T07:30:00Z, state=-1.258], Entry[timestamp=2024-06-09T07:45:00Z, state=-3.008], Entry[timestamp=2024-06-09T08:00:00Z, state=-1.217], Entry[timestamp=2024-06-09T08:15:00Z, state=-0.594], Entry[timestamp=2024-06-09T08:30:00Z, state=-1.786], Entry[timestamp=2024-06-09T08:45:00Z, state=-3.202], Entry[timestamp=2024-06-09T09:00:00Z, state=0.017], Entry[timestamp=2024-06-09T09:15:00Z, state=-2], Entry[timestamp=2024-06-09T09:30:00Z, state=-3.009], Entry[timestamp=2024-06-09T09:45:00Z, state=-5.008], Entry[timestamp=2024-06-09T10:00:00Z, state=-2.408], Entry[timestamp=2024-06-09T10:15:00Z, state=-3.305], Entry[timestamp=2024-06-09T10:30:00Z, state=-4.499], Entry[timestamp=2024-06-09T10:45:00Z, state=-5.188], Entry[timestamp=2024-06-09T11:00:00Z, state=-5.4], Entry[timestamp=2024-06-09T11:15:00Z, state=-5.005], Entry[timestamp=2024-06-09T11:30:00Z, state=-3.47], Entry[timestamp=2024-06-09T11:45:00Z, state=-2.707], Entry[timestamp=2024-06-09T12:00:00Z, state=-5.495], Entry[timestamp=2024-06-09T12:15:00Z, state=-3.863], Entry[timestamp=2024-06-09T12:30:00Z, state=-2.266], Entry[timestamp=2024-06-09T12:45:00Z, state=-0.296], Entry[timestamp=2024-06-09T13:00:00Z, state=-8.212], Entry[timestamp=2024-06-09T13:15:00Z, state=-1.951], Entry[timestamp=2024-06-09T13:30:00Z, state=1.198], Entry[timestamp=2024-06-09T13:45:00Z, state=6.245], Entry[timestamp=2024-06-09T14:00:00Z, state=-5.996], Entry[timestamp=2024-06-09T14:15:00Z, state=-1.195], Entry[timestamp=2024-06-09T14:30:00Z, state=1.994], Entry[timestamp=2024-06-09T14:45:00Z, state=7.797], Entry[timestamp=2024-06-09T15:00:00Z, state=-3.932], Entry[timestamp=2024-06-09T15:15:00Z, state=3.281], Entry[timestamp=2024-06-09T15:30:00Z, state=7.235], Entry[timestamp=2024-06-09T15:45:00Z, state=9.443], Entry[timestamp=2024-06-09T16:00:00Z, state=6.193], Entry[timestamp=2024-06-09T16:15:00Z, state=9.108], Entry[timestamp=2024-06-09T16:30:00Z, state=9.573], Entry[timestamp=2024-06-09T16:45:00Z, state=11.839], Entry[timestamp=2024-06-09T17:00:00Z, state=9.027], Entry[timestamp=2024-06-09T17:15:00Z, state=9.549], Entry[timestamp=2024-06-09T17:30:00Z, state=11.478], Entry[timestamp=2024-06-09T17:45:00Z, state=12.706], Entry[timestamp=2024-06-09T18:00:00Z, state=9.563], Entry[timestamp=2024-06-09T18:15:00Z, state=10.578], Entry[timestamp=2024-06-09T18:30:00Z, state=10.288], Entry[timestamp=2024-06-09T18:45:00Z, state=12.051], Entry[timestamp=2024-06-09T19:00:00Z, state=12.709], Entry[timestamp=2024-06-09T19:15:00Z, state=11.642], Entry[timestamp=2024-06-09T19:30:00Z, state=10.285], Entry[timestamp=2024-06-09T19:45:00Z, state=9.452], Entry[timestamp=2024-06-09T20:00:00Z, state=10.501], Entry[timestamp=2024-06-09T20:15:00Z, state=10.111], Entry[timestamp=2024-06-09T20:30:00Z, state=9.662], Entry[timestamp=2024-06-09T20:45:00Z, state=8.596]]
2024-06-08 18:08:24.484 [INFO ] [hab.event.ItemTimeSeriesUpdatedEvent] - Item 'VoltegoVerguetungTS' updated timeseries [Entry[timestamp=2024-06-08T16:15:00Z, state=6.818], Entry[timestamp=2024-06-08T16:30:00Z, state=7.698], Entry[timestamp=2024-06-08T16:45:00Z, state=9.307], Entry[timestamp=2024-06-08T17:00:00Z, state=7.957], Entry[timestamp=2024-06-08T17:15:00Z, state=8.901], Entry[timestamp=2024-06-08T17:30:00Z, state=9.482], Entry[timestamp=2024-06-08T17:45:00Z, state=9.75], Entry[timestamp=2024-06-08T18:00:00Z, state=7.899], Entry[timestamp=2024-06-08T18:15:00Z, state=8.274], Entry[timestamp=2024-06-08T18:30:00Z, state=8.3], Entry[timestamp=2024-06-08T18:45:00Z, state=8.727], Entry[timestamp=2024-06-08T19:00:00Z, state=10.505], Entry[timestamp=2024-06-08T19:15:00Z, state=8.702], Entry[timestamp=2024-06-08T19:30:00Z, state=8.406], Entry[timestamp=2024-06-08T19:45:00Z, state=8.387], Entry[timestamp=2024-06-08T20:00:00Z, state=7.684], Entry[timestamp=2024-06-08T20:15:00Z, state=7.536], Entry[timestamp=2024-06-08T20:30:00Z, state=7.534], Entry[timestamp=2024-06-08T20:45:00Z, state=7.003], Entry[timestamp=2024-06-08T21:00:00Z, state=9.503], Entry[timestamp=2024-06-08T21:15:00Z, state=8.939], Entry[timestamp=2024-06-08T21:30:00Z, state=7.5], Entry[timestamp=2024-06-08T21:45:00Z, state=7.274], Entry[timestamp=2024-06-08T22:00:00Z, state=8.091], Entry[timestamp=2024-06-08T22:15:00Z, state=6.774], Entry[timestamp=2024-06-08T22:30:00Z, state=6.251], Entry[timestamp=2024-06-08T22:45:00Z, state=5.903], Entry[timestamp=2024-06-08T23:00:00Z, state=5.992], Entry[timestamp=2024-06-08T23:15:00Z, state=4.18], Entry[timestamp=2024-06-08T23:30:00Z, state=3.88], Entry[timestamp=2024-06-08T23:45:00Z, state=3.463], Entry[timestamp=2024-06-09T00:00:00Z, state=3.194], Entry[timestamp=2024-06-09T00:15:00Z, state=2.238], Entry[timestamp=2024-06-09T00:30:00Z, state=1.919], Entry[timestamp=2024-06-09T00:45:00Z, state=2.09], Entry[timestamp=2024-06-09T01:00:00Z, state=2.46], Entry[timestamp=2024-06-09T01:15:00Z, state=2.892], Entry[timestamp=2024-06-09T01:30:00Z, state=2.547], Entry[timestamp=2024-06-09T01:45:00Z, state=2.007], Entry[timestamp=2024-06-09T02:00:00Z, state=2.009], Entry[timestamp=2024-06-09T02:15:00Z, state=1.999], Entry[timestamp=2024-06-09T02:30:00Z, state=1.993], Entry[timestamp=2024-06-09T02:45:00Z, state=1.839], Entry[timestamp=2024-06-09T03:00:00Z, state=4.33], Entry[timestamp=2024-06-09T03:15:00Z, state=2.595], Entry[timestamp=2024-06-09T03:30:00Z, state=1.198], Entry[timestamp=2024-06-09T03:45:00Z, state=-1.281], Entry[timestamp=2024-06-09T04:00:00Z, state=6.197], Entry[timestamp=2024-06-09T04:15:00Z, state=2.293], Entry[timestamp=2024-06-09T04:30:00Z, state=0.004], Entry[timestamp=2024-06-09T04:45:00Z, state=-3.614], Entry[timestamp=2024-06-09T05:00:00Z, state=7.331], Entry[timestamp=2024-06-09T05:15:00Z, state=2.626], Entry[timestamp=2024-06-09T05:30:00Z, state=-3], Entry[timestamp=2024-06-09T05:45:00Z, state=-6.466], Entry[timestamp=2024-06-09T06:00:00Z, state=5.053], Entry[timestamp=2024-06-09T06:15:00Z, state=-0.007], Entry[timestamp=2024-06-09T06:30:00Z, state=-0.85], Entry[timestamp=2024-06-09T06:45:00Z, state=-4.996], Entry[timestamp=2024-06-09T07:00:00Z, state=1.797], Entry[timestamp=2024-06-09T07:15:00Z, state=-0.358], Entry[timestamp=2024-06-09T07:30:00Z, state=-1.258], Entry[timestamp=2024-06-09T07:45:00Z, state=-3.008], Entry[timestamp=2024-06-09T08:00:00Z, state=-1.217], Entry[timestamp=2024-06-09T08:15:00Z, state=-0.594], Entry[timestamp=2024-06-09T08:30:00Z, state=-1.786], Entry[timestamp=2024-06-09T08:45:00Z, state=-3.202], Entry[timestamp=2024-06-09T09:00:00Z, state=0.017], Entry[timestamp=2024-06-09T09:15:00Z, state=-2], Entry[timestamp=2024-06-09T09:30:00Z, state=-3.009], Entry[timestamp=2024-06-09T09:45:00Z, state=-5.008], Entry[timestamp=2024-06-09T10:00:00Z, state=-2.408], Entry[timestamp=2024-06-09T10:15:00Z, state=-3.305], Entry[timestamp=2024-06-09T10:30:00Z, state=-4.499], Entry[timestamp=2024-06-09T10:45:00Z, state=-5.188], Entry[timestamp=2024-06-09T11:00:00Z, state=-5.4], Entry[timestamp=2024-06-09T11:15:00Z, state=-5.005], Entry[timestamp=2024-06-09T11:30:00Z, state=-3.47], Entry[timestamp=2024-06-09T11:45:00Z, state=-2.707], Entry[timestamp=2024-06-09T12:00:00Z, state=-5.495], Entry[timestamp=2024-06-09T12:15:00Z, state=-3.863], Entry[timestamp=2024-06-09T12:30:00Z, state=-2.266], Entry[timestamp=2024-06-09T12:45:00Z, state=-0.296], Entry[timestamp=2024-06-09T13:00:00Z, state=-8.212], Entry[timestamp=2024-06-09T13:15:00Z, state=-1.951], Entry[timestamp=2024-06-09T13:30:00Z, state=1.198], Entry[timestamp=2024-06-09T13:45:00Z, state=6.245], Entry[timestamp=2024-06-09T14:00:00Z, state=-5.996], Entry[timestamp=2024-06-09T14:15:00Z, state=-1.195], Entry[timestamp=2024-06-09T14:30:00Z, state=1.994], Entry[timestamp=2024-06-09T14:45:00Z, state=7.797], Entry[timestamp=2024-06-09T15:00:00Z, state=-3.932], Entry[timestamp=2024-06-09T15:15:00Z, state=3.281], Entry[timestamp=2024-06-09T15:30:00Z, state=7.235], Entry[timestamp=2024-06-09T15:45:00Z, state=9.443], Entry[timestamp=2024-06-09T16:00:00Z, state=6.193], Entry[timestamp=2024-06-09T16:15:00Z, state=9.108], Entry[timestamp=2024-06-09T16:30:00Z, state=9.573], Entry[timestamp=2024-06-09T16:45:00Z, state=11.839], Entry[timestamp=2024-06-09T17:00:00Z, state=9.027], Entry[timestamp=2024-06-09T17:15:00Z, state=9.549], Entry[timestamp=2024-06-09T17:30:00Z, state=11.478], Entry[timestamp=2024-06-09T17:45:00Z, state=12.706], Entry[timestamp=2024-06-09T18:00:00Z, state=9.563], Entry[timestamp=2024-06-09T18:15:00Z, state=10.578], Entry[timestamp=2024-06-09T18:30:00Z, state=10.288], Entry[timestamp=2024-06-09T18:45:00Z, state=12.051], Entry[timestamp=2024-06-09T19:00:00Z, state=12.709], Entry[timestamp=2024-06-09T19:15:00Z, state=11.642], Entry[timestamp=2024-06-09T19:30:00Z, state=10.285], Entry[timestamp=2024-06-09T19:45:00Z, state=9.452], Entry[timestamp=2024-06-09T20:00:00Z, state=10.501], Entry[timestamp=2024-06-09T20:15:00Z, state=10.111], Entry[timestamp=2024-06-09T20:30:00Z, state=9.662], Entry[timestamp=2024-06-09T20:45:00Z, state=8.596]]

In the openhab.log I see this output from the DSL rule:

2024-06-08 17:59:20.014 [INFO ] [g.openhab.core.model.script.pv.rules] - SunRiseTime: 2024-06-08T04:53+02:00[Europe/Berlin]; SunSetTime: 2024-06-08T21:19+02:00[Europe/Berlin]; AvgPreis: 9.047
2024-06-08 17:59:20.014 [INFO ] [g.openhab.core.model.script.pv.rules] - MinPreis: 6/8/24, 4:53 AM: VoltegoVerguetungTS -> 9.047
2024-06-08 17:59:20.014 [INFO ] [g.openhab.core.model.script.pv.rules] - MaxPreis: 6/8/24, 4:53 AM: VoltegoVerguetungTS -> 9.047
2024-06-08 18:14:20.012 [INFO ] [g.openhab.core.model.script.pv.rules] - SunRiseTime: 2024-06-08T04:53+02:00[Europe/Berlin]; SunSetTime: 2024-06-08T21:19+02:00[Europe/Berlin]; AvgPreis: 9.047
2024-06-08 18:14:20.013 [INFO ] [g.openhab.core.model.script.pv.rules] - MinPreis: 6/8/24, 4:53 AM: VoltegoVerguetungTS -> 9.047
2024-06-08 18:14:20.013 [INFO ] [g.openhab.core.model.script.pv.rules] - MaxPreis: 6/8/24, 4:53 AM: VoltegoVerguetungTS -> 9.047

So somehow the DSL rule does not consider the range I specify the in .averageBetween(). My guess is I need to move to OH4.2 to get the recent changes from the Pull Request #3736, no?

Thanks!

I believe averageBetween didn’t allow future dates prior to that PR. Try 4.2 snapshot (or maybe M3)

Also, I think your timeseries should use the default replace policy instead of the add policy.