It might be helpful to describe the life of a command to a Dimmer Item.
First, as rossko57 says, it’s important to understand the distinction between a State and a Command (I like to use proper nouns when talking about OH concepts to avoid confusion). I think I already said this above, but I’ll repeat it here in case I have my threads mixed up.
Items store a State. All Items can store the special states of UNDEF and NULL. Beyond that, each Item type has it’s one singular way to store it’s state. This is documented in the table here. The middle column describes how the Item stores the State (note to self, that table should be updated to show the specific type the Item stores, not just a description of it). In Dimmers case we have
Item carrying a percentage value for dimmers
That translates to PercentType which is an integer number between 0 and 100.
You will only ever see an Item updating to or changing to a PercentType because that is how it stores it’s state.
But, each Item can also receive Commands and the possible Commands it can receive can differ from the State. Looking at that same table above we see that the Dimmer Item can receive:
OnOff, IncreaseDecrease, Percent
This means we can sendCommand(ON), for example, to a Dimmer Item.
So what happens when I sendCommand(ON) to a Dimmer Item which doesn’t store it’s state as an OnOffType? The process is as follows. Let’s assume that the ON command is coming from BasicUI.
-
The Dimmer Item has been put on the sitemap as a Switch. The user clicks on the toggle to turn ON the Dimmer.
-
BasicUI creates an ON Command Event on the Event Bus. The Dimmer Item has not changed state at this point.
-
Interested parts of OH pick up the command from the Event Bus. These may be:
- Rules that trigger on received command on that Item
- Persistence that is configured to save that Item’s state on receivedCommand
- The Binding that Item is linked to, we are going to focus only on the binding for the rest of this description.
-
The Binding sees that the Item has received an ON Command. The Binding determines what ON means for this given device and issues the messages to the device necessary for it to “turn ON”. The binding also interprets what the ON Command means in terms of the Dimmer’s State. (I’m skipping the whole issue of autoupdate here for clarity). Because a Dimmer Item can only store State as a PercentType, the binding Updates the Item with a PercentType. Since we are dealing with Dimmers, usually that Update will either be the last value the device was set to before it was last turned off, or 100. So in the logs you will see the Dimmer Item updating and changing to 100.
You will never see the Dimmer changing from OFF to 100 because OFF is not a State that a Dimmer can store. But you will see that a Dimmer can receive a Command. And it is possible to get the Dimmer’s state as if it were a Switch. In a Rule you can do this using MyDimmer.getStateAs(OnOffType)
. Somehow the sitemap also understands how to interpret the state of a Dimmer to an OnOffType so it knows how to properly set the toggle. It does not work with icons though because icons only work directly with the actual stored State.
As you can see, we’ve NOT said that Dimmers “work on on/off.” Dimmer can be Commanded with ON/OFF and INCREASE/DECREASE. It is the job of the Binding to interpret those Commands to the new Dimmer State.
I wonder what would happen if you tried MyDimmer.getStateAs(IncreaseDecreaseType)
? Probably a null or an exception. I don’t have any Dimmers right now so can’t quickly test, maybe something to try this evening.
Hopefully between rossko57’s description and my description this is all a bit more clear. Note also, this is why when you have a Rule triggered by received command, you cannot rely on the state of the Item in the Rule. It hasn’t necessarily changed state yet in response to the command.