Rule DSL: Arithmetic calculation not yet finished when next line of code wants to pick up the result

I’m running openHAB 3.4.4 and am currently writing a rule that does a couple of arithmetic calculations around energy consumption, PV energy generation, autonomy grade etc. In that context I’m facing one very odd problem.

If (example)…

  • … in code line #1, I perform an arithmetic calculation (e.g. converting from W into Wh)…
  • … and then, in the subsequent code line #2, I write the result into the log…
  • … the result is often still the previous (old) result, as if the calculation in line #1 was not yet finished when the result was written into the log in line #2.

Is there a way to fix this, e.g. only continue with the next line of code only after the previous line of code (calculation) has been completed?

Of course I could tell the code to just wait for e.g. 1 second, but this seems like a rather coarse hack.

It sounds like you are writing your result to an item and then read it again from that item. Is this the case? If not I would be really surprised to see this happen…

You should not expect that an item value has changed instantly after a line of code. You should work with local variables instead. If you’re doing that already I don’t have any clever ideas :sweat_smile:

Oh man, you‘re right (blushing red, looking at the floor in full embarrassment). :wink:

Never mind, I guess that’s probably an experience that a lot of users made at some point (me included) :grimacing:

2 Likes