newState with group Item?

Hi,

Been using newState in DSL rules a lot, but apparently I miss something when using it with a group triggerd rule. In the following example, gForecastSolarForecastTomorrow is the group item.
Group:Number:SUM gForecastSolarForecastTomorrow

If I use it in this rule:

rule "gForecastSolarForecastTomorrowAVG"
when Item gForecastSolarForecastTomorrow changed
then 
    gForecastSolarForecastTomorrowAVG.postUpdate ( (newState as Number + gForecastSolarForecastTomorrow.previousState as Number ) / 2)
end

I get this error:

2023-11-20 16:00:30.387 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'solarforecast-2' failed: Could not cast 20.11.23, 16:00: gSolcastForecastTomorrow -> 6.238 to java.lang.Number; line 43, column 16, length 48 in solarforecast

Seems like newState contains a string 20.11.23, 16:00: gSolcastForecastTomorrow -> 6.238 when I expected it to contain the Number 6.238
What am I missing ?

What is the state of your item?
Whats with oldState?

MouseOver in the VSC says:
grafik

Don’t just guess. Log it out and confirm. Also log out gForecastSolarForecastTomorrow.previousState.

I think you will find that the error is coming from the call to previousState because that returns a HistoricItem, not a State. A HistoricItem isn’t a Number can cannot be cast to a Number. It has two properties, the state and the timestamp when the Item became that state, whose toString() looks like "20.11.23, 16:00: gSolcastForecastTomorrow -> 6.238".

Use gForecastSolarForecastTomorrow.previousState.state or, even better, use the previousState implicit variable.

from the manual: previousState - implicitly available in every rule that has at least one status change event trigger. May I ask why previousState is not available with a “received update” trigger ?

Because it hasn’t been implemented that way. Typically if you care about the previous state it’s because you care about the change. If you care about the change there is the changed rule trigger.

If you have a compelling use case where using received update to trigger the rule and you still need the previous state that cannot be handled with a changed rule trigger please file an issue with OH core.

Note you are using the changed trigger above.

I would like it if it was available whenever possible.
Example: one rule gets triggered regular by updates (e.g. a sensor) , runs some calculations and the result goes to an item with expire statement. Update trigger is what you want in that case.

Like I said , you can file an issue. if you think you have a compelling use case.

Note, in JS Scripting and I suspect jRuby as well, the equivalent of previousState is there for both updates and changes.

AFAIK, openhab core doesn’t tell you previousState on state updates. It only provides this information on state changes.

Does jsscripting save the previousstate independently of core in order to provide this information for update triggers?

I don’t know how it’s implemented but according to the docs it’s there for ItemStateUpdated and ItemChanged

I was looking at the wrong row on in the table. Indeed event.oldState is not there for ItemStateUpdateTrigger, only ItemStateChangeTrigger and GroupStateChangeTrigger. I was looking at the receivedState row for some reason. :woozy_face: