Random result of a rule

I have created a rule, and I feed in values for “WaterMeter_Output_dummy” by MQTT.
On a site map I see “WaterMeter_Output_dummy”, “Dummy_Water_Meter2” and “Dummy_Water_Meter3”. I refresh the web browser after sending each number by MQTT.

The problem is that I get random outputs from the rule. An example is below, but the output is usually not repeatable, it is a bit random when the errors occures.
I start by sending “1” 10 times

Input on “WaterMeter_Output_dummy” 2 3 4 5 6 7 8
Output on “Dummy_Water_Meter2” 1 3 3 4 5 7 7
Output on “Dummy_Water_Meter3” 1 1 3 3 4 5 7

Some time it can run rather stable with increasing values of “WaterMeter_Output_dummy”

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
import java.lang.Math

var Number Last_Water_Usage=0
var Number Current_Water_Usage=0

rule "Water Meter temporary usage"
Item WaterMeter_Output_dummy received update

	WaterMeter_Number_New.sendCommand(Float::parseFloat(String::format("%s",WaterMeter_Output_dummy.state).replace(' ','')))
	Current_Water_Usage = WaterMeter_Number_New.state as DecimalType
	sendCommand(Dummy_Water_Meter2, Current_Water_Usage)
	sendCommand(Dummy_Water_Meter4, Last_Water_Usage )

Last_Water_Usage = Current_Water_Usage							

Why does it behave so strange?

When you send a command to an Item, it may or may not affect its state, depending on how you have configured it.
If it is affected, it won’t happen immediately.

So sending a command to an Item and immediately reading its state is not likely to give the result you expected.

By inserting Thread::sleep(100) it works perfectly! Thanks!

I’m too sure why, having sent ‘blah’ to the Item, you don’t use ‘blah’ in your rule, rather than waiting for the Item to update.