openhab-scripters / openhab-helper-libraries

Scripts and modules for use with openHAB
Eclipse Public License 1.0
88 stars 69 forks source link

event.ItemState returning incorrect state for Rollershutter #321

Closed wawa79 closed 3 years ago

wawa79 commented 4 years ago

Describe the bug Within a rule triggered by an update of a Rollershutter item, event.ItemState does not report the state of the item (0-100 number) but the command sent to the item (UP DOWN).

To Reproduce

  1. Define a Rollershutter item named MyRollershutter Rollershutter MyRollershutter "Test rollershutter"

  2. Create the following Jython script

    
    from core.rules import rule
    from core.triggers import when
    from core.log import logging, LOG_PREFIX, log_traceback

@rule("bug_itemState", description=u"Bug for itemState with Rollershutter item", tags=[]) @when("Item MyRollershutter received update") def bug_itemState(event): bug_itemState.log.debug("Item state: {}={} {}".format(event.itemName, event.itemState.toString(), ir.getItem(event.itemName).state.toString()))


3. Send a command to MyRollershutter  from the console:
`smarthome:send MyRollershutter DOWN`

4. See the log:
`2020-06-10 00:25:38.232 [DEBUG] [jsr223.bug_itemState                ] - Item state: MyRollershutter=DOWN 100`

**Expected behavior**
event.itemName should return the state of the triggering item and not a command.
As a workaround, ir.getItem(event.itemName).state returns the expected state.

**Environment (please complete the following information):**
 - Jython version: [e.g. 2.7.0 standalone]
 - openHAB version: [2.5.4]
 - Release: [release version]
5iver commented 3 years ago

If this was a bug, which I do not believe it to be, it should not be reported in the Jython helper libraries but openhab-core. The forum would be the best place to discuss this, where it's been discussed before.

event.itemState is reporting the state that the Item was updated to, which can be PercentType, UpDiownType, or UnDefTyoe. When reading the state of the Item, you will need to specify the format that you'd like it in or accept the type that you get. To specify PercentType, then you need to ask for it with...

event.itemState.as(PercentType)

You can also request UpDownType...

event.itemState.as(UpDownType)

... but be careful, since you will get None if the value is not 0 or 100. Read through this... https://www.openhab.org/docs/concepts/items.html#a-note-on-items-which-accept-multiple-state-data-types.