Virtual Item receives command (event) but state is (still) wrong

It’s driving me crazy. Any help is greatly appreciated. I sendCommand a value to a virtual Item and try to react to it with a rule. Which worked great in OH1 but now in OH2 the event triggers, but the state is the one from before. It’s a bit complex. I try to reduce it to the minimum here.

I’m sending Licht_Aus.sendCommand(ON) and it should switch off the hues in a room. It receives a 0 (STATE_OFF) as event, but the read state is not 0. It’s the value from before (2 in this case).

Events lock great, but the If check for (.state) is getting an old value. It looks like the event is triggered, just before the value (state) is saved.

2017-02-14 14:25:09.530 [ItemStateChangedEvent     ] - Licht_Aus changed from OFF to ON
2017-02-14 14:25:09.532 [ItemCommandEvent          ] - Item 'Licht_Szene_Flur' received command 0
2017-02-14 14:25:09.596 [ItemStateChangedEvent     ] - Licht_Szene_Flur changed from 2 to 0
2017-02-14 14:25:09.649 [ItemCommandEvent          ] - Item 'Hue_State_Flur' received command 0
2017-02-14 14:25:09.884 [ItemStateChangedEvent     ] - Licht_Aus changed from ON to OFF
2017-02-14 14:25:09.919 [ItemStateChangedEvent     ] - Hue_State_Flur changed from 2 to 0
2017-02-14 14:25:09.922 [ItemCommandEvent          ] - Item 'gHueDimFlur' received command 100

This is my setup.

Switch Licht_Aus				//Funktion alle Lichter aus
Number Licht_Szene_Flur			//für Schnellauswahl (Szenen)
Number Hue_State_Flur			//Funktion
Dimmer Hue1_Dim_Flur (gHueDimFlur) {channel="hue:0220:00178840611f:5:brightness"}
Dimmer Hue2_Dim_Flur (gHueDimFlur) {channel="hue:0220:00178840611f:6:brightness"}
Dimmer Hue3_Dim_Flur (gHueDimFlur) {channel="hue:0220:00178840611f:7:brightness"}


val Integer STATE_OFF = 0
val Integer STATE_COZY = 1
val Integer STATE_NORMAL = 2
val Integer STATE_BRIGHT = 3 //also used as STATE_NIGHT
val Integer STATE_UNKNOWN = 9

rule "Licht_Aus Function"
when
	Item Licht_Aus received command ON
then
	Licht_Szene_Flur.sendCommand(STATE_OFF)
	Licht_Aus.postUpdate(OFF)
end

rule "Licht_Szene_Flur Event"
when
	Item Licht_Szene_Flur received command
then
	if(Licht_Szene_Flur.state == STATE_OFF){
		Hue_State_Flur.sendCommand(STATE_OFF)
	} else if(Licht_Szene_Flur.state == STATE_COZY){
		Hue_State_Flur.sendCommand(STATE_COZY)
	} else if(Licht_Szene_Flur.state == STATE_NORMAL){
		Hue_State_Flur.sendCommand(STATE_NORMAL)
	} else if(Licht_Szene_Flur.state == STATE_BRIGHT){//nachtlicht
		Hue_State_Flur.sendCommand(STATE_BRIGHT)
	}
end

rule "Hue_State_Flur Function"
when
	Item Hue_State_Flur received command
then
	if(Hue_State_Flur.state == STATE_OFF){
			gHueDimFlur.sendCommand(OFF)
	} else if(Hue_State_Flur.state == STATE_COZY){
			gHueDimFlur.sendCommand(50)
			Licht_Temp_Flur.sendCommand(TEMP_WARM)
	} else if(Hue_State_Flur.state == STATE_NORMAL){
			gHueDimFlur.sendCommand(100)
			Licht_Temp_Flur.sendCommand(TEMP_NORMAL)
	} else if(Hue_State_Flur.state == STATE_BRIGHT){//nachtlicht
			gHueDimFlur.sendCommand(1)
	}
end

Here is a simple setup to test the issue. Just switch it from 0 to 1 to 2 and repeat some times. Do it fast. Then look into the openhab.log. Is the state logged in the order you pressed the switch? Am I the only one with that issue?

Item:
Number TestItem

Rule:
rule "TestItem Event"
when
	Item TestItem received command
then
	logInfo("TestItem","state: "+TestItem.state)
end

Sitemap:
Switch item=TestItem mappings=[0="0", 1="1", 2="2"]

If I use “changed” instead of “received command” then the state is really updated when event is triggered.

And while typing this and made a break, I remembered that you can get the received command by using the buildin value “receivedCommand” instead of using the .state.
This will work:

rule "TestItem Event"
when
	Item TestItem received command
then
	logInfo("TestItem","state: "+receivedCommand)
end