Posting an update to a new item help

Hi guys

I am trying to take a average group temperature item and “copy” its output to a new item so I can chart it separately from the existing group item but all I get is “gall_temperature_avg updated to NULL” in the logs.

My formatting of my rule may be wrong

Group:Number:AVG                gall_temperature 		"Avg. Room Temperature [%.1f °C]"   <temperature> 	 (Status)
Number   						gall_temperature_avg    "Temperature [%.1f °C]"
rule "gall_temperature_avg"
when
    Item gall_temperature changed
then	
	gall_temperature_avg.postUpdate(gall_temperature.state)
	logInfo("system.rules", "LOG - gall_temperature_avg updated to " + gall_temperature_avg.state)
end

A postUpdate is not instantaneous. The update gets put on the message bus, eventually OpenHAB actually updates the Item, triggers any rules looking for the update, etc.

Have you logged the value the are using for the postUpdate, rather than looking for the result? Do your logs show the Item updating AFTER the “failed” logInfo?

I checked and it works.
Post the definitions of the members of group gall_temperature please.

if (gall_temperature.state != NULL) {gall_temperature_avg.postUpdate(gall_temperature.state)}

@rossko57 you are correct, the item does in fact update but my rule logs the change as NULL. I was also referencing this item for a chart and after a restart it is now persisting and working as I expected.

Thanks Guys!

It’s quite important to understand OpenHABs asynchronous nature, it does set a few traps for of this kind. postUpdate is not a command that “just does it”, instead it sends a message off for OpenHAB to do the update later, and your rule continues without waiting.

So, examining the state of an Item you have just postUpdated within the same rule - you will not yet see the effect of the update. Or perhaps you might sometimes, depending on other things happening in the system. It is indeterminate, and that makes it difficult for us humans to think about !

In this case it is fairly trivial and doesn’t matter much - but it is a good lesson for more complex rules. Never assume that a postUpdate or sendCommand has an immediate effect.

There’s a very common variation of this trap I will highlight as well. Triggering a rule from a command to an Item, and then looking at the state of the same Item within the rule. That initial command may or may not have also triggered the autoupdate feature before you look at the state - indeterminate again.
Of course, if you trigger the rule from an update, then you can be sure the update has happened.

Sometimes it takes some thought and careful choice of rule triggers to get the correct effects. So, if you really wanted to log the new state in your original rule, a good way would be to use two rules (don’t be frightened of having many rules)

rule "gall_temperature_avg"
when
    Item gall_temperature changed
then	
	gall_temperature_avg.postUpdate(gall_temperature.state)
end

rule "gall_temperature_avg logging"
when
    Item gall_temperature_avg updated
then	
	logInfo("system.rules", "LOG - gall_temperature_avg updated to " + gall_temperature_avg.state)
end
2 Likes

Thankyou for the explanation & I have adjusted my rule as per your recommendation above. The item in question is now updating correctly but for some reason it will not persist. On its initial change from NULL it did persist in the MySQL DB but any further updates have not persisted to the DB. I am persisting all items on every change

* : strategy = everyChange, everyDay, restoreOnStartup 

If you modify the persistence, OH will need to be restarted. It doesn’t quite work on the fly the same way as items or rule files.

Your statement might not be true! Yes, I agree that there are some cases where the items to be persisted are not, but AFAIK when the persist file is parsed, it checks for the triggers declared for the items enlisted in the persistence file, and if there is a missing item, it basically skips it (when a write procedure is involved)! This is just my experience, if you have any proof that it happens otherwise, then please let us know!

I know that if I add an item to a persisted group, the item doesn’t get persisted until after a restart.

True, but that has nothing to do with touching a persistence file (*.persist)!

Good to know.
So does that mean that if I touch the persistence file (*.persist), the newly added item to the group will be picked up and persisted and I don’t need to restart OH?

Normally, yes! At least AFAIK!

Coolio, Thanks