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:
when Item gForecastSolarForecastTomorrow changed
gForecastSolarForecastTomorrowAVG.postUpdate ( (newState as Number + gForecastSolarForecastTomorrow.previousState as Number ) / 2)
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 ?
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.
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.
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.
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.