Error trying to use event.itemState in Jython

Hi all,

I’m in the process of converting my rules to Jython in the new 2.5 release and am hitting this error trying to use event.itemState in them:

AttributeError: '' object has no attribute 'itemState'

An extract from one of these rules is:

@rule('Occupancy Light On')
@when('Item Bed1_Occupancy received command')
@when('Item Dining_Occupancy received command')
@when('Item Bed2_Occupancy received command')
@when('Item Shed_Occupancy received command') 
@when('Item Garage_Occupancy received command')
@when('Item Office_Occupancy received command')
def occupancy_light_on(event):
    LogAction.logInfo('OLO', 'item is {}', event.itemName)
    LogAction.logInfo('OLO', 'state is {}', event.itemState)

The first log at the start of the rule works fine but the second throws the error. I can workaround this by using items[event.itemState] but I don’t understand why I need to.

This seems pretty basic so obviously I’m missing something?

Review this…

The “received command” trigger does not provide an event.itemState object, but it does provide event.itemCommand. The DSL equivalent to these are the implicit variables available within a rule that are dependent on the type of trigger used. In the DSL, a rule triggered by “received command” would have receivedCommand. In scripted automation, a rule triggered by “received command” has event.itemCommand.

Change your trigger to “received update” or “changed” so that you can use event.itemState, or keep the “received command” triggers and use event.itemCommand.

1 Like

I want to elaborate a little bit on Scott’s answer. Why is event.itemState not provided on a received command trigger?

  • The Rule triggers at the same as the Item is updating, the Item may not have yet reached the state that is a response to the command. In some cases, e.g. when autoupdate is turned off, it can be some time before the Item updates in response to the command, if there is an error it may never update.
  • Some commands are not storable as an Item state. For example, if you send ON to a Dimmer Item, the Item will eventually update to a State over 1, usually 100.

A command is a signal of intent. “I want X to happen.” It comes with no information about the state of the Item. Therefore, when you are working with a received command triggered Rule, you should not be doing anything with the state of the Item that received the command. You should only be working with events.itemCommand for a received command triggered Rules.


Thanks Scott and Rich - problem solved and understanding increased!