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 @jimtng,

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.

Hi JimT,

I meanwhile upgraded to OH 4.2 release version. So far so good, but I still have trouble to have average/minimum/maximumBetween look into the future.

This is the timeseries:

2024-07-13 12:13:09.047 [INFO ] [hab.event.ItemTimeSeriesUpdatedEvent] - Item 'VoltegoPreisTS' updated timeseries [Entry[timestamp=2024-07-13T11:00:00Z, state=-0.5], Entry[timestamp=2024-07-13T12:00:00Z, state=-1.49], Entry[timestamp=2024-07-13T13:00:00Z, state=-1.147], Entry[timestamp=2024-07-13T14:00:00Z, state=-0.348], Entry[timestamp=2024-07-13T15:00:00Z, state=-0.001], Entry[timestamp=2024-07-13T16:00:00Z, state=2.877], Entry[timestamp=2024-07-13T17:00:00Z, state=5.033], Entry[timestamp=2024-07-13T18:00:00Z, state=7.545], Entry[timestamp=2024-07-13T19:00:00Z, state=7.626], Entry[timestamp=2024-07-13T20:00:00Z, state=7.884], Entry[timestamp=2024-07-13T21:00:00Z, state=6.3]]

This is my rule code:

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 = VoltegoPreisTS.averageBetween(SunRiseTime,SunSetTime)
    val MinPreis = VoltegoPreisTS.minimumBetween(SunRiseTime,SunSetTime)
    val MaxPreis = VoltegoPreisTS.maximumBetween(SunRiseTime,SunSetTime)
    logInfo("pv.rules","SunRiseTime: " + SunRiseTime + "; SunSetTime: " + SunSetTime)
    logInfo("pv.rules","EPreis: " + EPreis + "; AvgPreis: " + AvgPreis)
    logInfo("pv.rules","MinPreis: " + MinPreis)
    logInfo("pv.rules","MaxPreis: " + MaxPreis)
end

This is the output:

2024-07-13 13:00:00.007 [INFO ] [g.openhab.core.model.script.pv.rules] - SunRiseTime: 2024-07-13T05:07+02:00[Europe/Berlin]; SunSetTime: 2024-07-13T21:17+02:00[Europe/Berlin]
2024-07-13 13:00:00.007 [INFO ] [g.openhab.core.model.script.pv.rules] - EPreis: -0.5; AvgPreis: 0.8572247422680412
2024-07-13 13:00:00.008 [INFO ] [g.openhab.core.model.script.pv.rules] - MinPreis: 7/13/24, 12:00 PM: VoltegoPreisTS -> -0.001
2024-07-13 13:00:00.008 [INFO ] [g.openhab.core.model.script.pv.rules] - MaxPreis: 7/13/24, 5:07 AM: VoltegoPreisTS -> 3.105

For the timeseries I use “add” policy. I can plot the data in the Charts across all time stamps in history and future. But the Persistence Extensions consider up to current time only, I think.

Do you have a sample code to port the Rule DSL code to JRuby to verify?

Thanks!

I would think that both RulesDSL and JRuby should give you the same result, because they’re both calling the same core API.

Here’s your code in JRuby

sun = things["astro:sun:local"]
sun_rise = sun.get_event_time("SUN_RISE", ZonedDateTime.now, "START")
sun_set = sun.get_event_time("SUN_SET", ZonedDateTime.now, "END")
avg_preis = VoltegoPreisTS.average_between(sun_rise, sun_set)
min_preis = VoltegoPreisTS.minimum_between(sun_rise, sun_set)
max_preis = VoltegoPreisTS.maximum_between(sun_rise, sun_set)
logger.info("SunRiseTime: #{sun_rise}; SunSetTime: #{sun_set}")
logger.info("EPreis: #{EPreis}; AvgPreis: #{avg_preis}")
logger.info("MinPreis: #{min_preis.inspect}") # Note, without .inspect, it will just show the state
logger.info("MaxPreis: #{max_preis.inspect}")

Also make sure that your persistence strategy is forecast.

… well, I’m using InMemory persistence where the Item is part of group “gInMemory”. The persistence configuration looks like this (see below) and should persist all group members per documentation:

<groupName>* - all members of this group will be persisted, but not the group itself. Optionally, an alias may be provided if the persistence service requires special names (e.g. a table to be used in a database, a feed id for an IoT service, etc.) Note that * is NOT a wildcard match character in this context.

Strategies {
	default = forecast
}

Items {
	gInMemory* : strategy = forecast
}

I’ll add the Item in addition - let’s see…

You could try this

sun = things["astro:sun:local"]
sun_rise = sun.get_event_time("SUN_RISE", ZonedDateTime.now, "START")
logger.info VoltegoPreisTS.all_states_until(sun_set)

… right, I did that in DSL which returns an empty result:

logInfo("pv.rules","Debug2: " + VoltegoPreisTS.getAllStatesUntil(SunSetTime))
2024-07-13 15:15:20.689 [INFO ] [g.openhab.core.model.script.pv.rules] - Debug2: []

Could you modify this to:

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)

    logger.info "raw data: #{event.state}"
    data = JSON.parse(event.state.to_s)
    logger.info "parsed data: #{data}"
    data["elements"].each do |element|
      timestamp = ZonedDateTime.parse(element["begin"])
                               .with_zone_same_local(ZoneId.system_default)
      time_series.add(timestamp, element["price"])
    end

    time_series.each do |entry|
      logger.info "entry: #{entry}"
    end

    VoltegoVerguetungTS.time_series = time_series
  end
end

I’ve just realised, that you don’t need to tinker with time zones at all whilst adding to the time series, because it’s going to be converted to Instant anyway.

So this (part) would work too:

    data["elements"].each do |element|
      timestamp = ZonedDateTime.parse(element["begin"])
      time_series.add(timestamp, element["price"])
    end
1 Like

… something odd is going on here. I added your JRuby code to output what I do in the DSL code. I get the same results - as expected. This is also with the Item being added directly to the InMemory persistence configuration with strategy forecast:

2024-07-13 16:23:39.110 [INFO ] [on.jrubyscripting.rule.timeseries2:3] - SunRiseTime: 2024-07-13T05:07+02:00[Europe/Berlin]; SunSetTime: 2024-07-13T21:17+02:00[Europe/Berlin]
2024-07-13 16:23:39.112 [INFO ] [on.jrubyscripting.rule.timeseries2:3] - EPreis: ; AvgPreis: 0.5499680412371134
2024-07-13 16:23:39.114 [INFO ] [on.jrubyscripting.rule.timeseries2:3] - MinPreis: #<OpenHAB::Core::Items::Persistence::PersistedState -1.49 at: 2024-07-13T14:00+02:00[Europe/Berlin] item: VoltegoPreisTS>
2024-07-13 16:23:39.116 [INFO ] [on.jrubyscripting.rule.timeseries2:3] - MaxPreis: #<OpenHAB::Core::Items::Persistence::PersistedState 3.105 at: 2024-07-13T05:07+02:00[Europe/Berlin] item: VoltegoPreisTS>

Will post the other debug output as soon as I get it.