Rule triggered by zigbee2mqtt item runs multiple times

  • Platform information:
    • Hardware: Raspberry PI 4B
    • OS: OpenHabianOS 4.1.1
    • Java Runtime Environment: OpenJDK 64 bit 17.0.13
    • openHAB version: 4.2.2
  • Issue of the topic: When running a rule with a zigbee2mqtt item by “core.ItemStateUpdateTrigger”, it will execute its actions 3 times (including scripts)
  • Please post configurations (if applicable):
    • Items configuration related to the issue
UID: mqtt:topic:401af0a7:187a3efffee9bd66
label: Smart plug (with power monitoring by polling)
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:401af0a7
channels:
  - id: state_binary
    channelTypeUID: mqtt:switch
    label: ts011f_plug_3 State
    description: On/off state of the switch
    configuration:
      commandTopic: z2m/wall_plug/set
      qos: 0
      stateTopic: z2m/wall_plug
      transformationPattern: REGEX:(.*state.*)∩JSONPATH:$.state
      off: '{"state":"OFF"}'
      on: '{"state":"ON"}'
  • Rules code related to the issue
configuration:
triggers:
  - id: "1"
    configuration:
      itemName: ts011f_plug_3_state_187a3efffee9bd66
      state: ON
    type: core.ItemStateUpdateTrigger
actions:
  - inputs: {}
    id: 1_action_action
    configuration:
      itemName: ""
      type: application/javascript
      script: >
        processScript()
        function processScript() {
          console.log('============start==============')
          console.log('script running')
          console.log('=========++++end++++==========')
        }
    type: script.ScriptAction
  • Services configuration related to the issue
    The MQTT broker thing configuration
UID: mqtt:broker:401af0a7
label: MQTT Broker
thingTypeUID: mqtt:broker
configuration:
  lwtQos: 0
  publickeypin: true
  keepAlive: 60
  hostnameValidated: true
  birthRetain: true
  secure: false
  certificatepin: true
  shutdownRetain: true
  password: password
  protocol: TCP
  qos: 0
  reconnectTime: 60000
  mqttVersion: V3
  host: localhost
  lwtRetain: true
  enableDiscovery: true
  username: admin
  • If logs where generated please post these here using code fences:
2025-01-17 16:34:11.949 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'ts011f_plug_3_state_187a3efffee9bd66' received command ON
2025-01-17 16:34:11.951 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'ts011f_plug_3_state_187a3efffee9bd66' predicted to become ON
2025-01-17 16:34:11.960 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'ts011f_plug_3_state_187a3efffee9bd66' changed from OFF to ON
2025-01-17 16:34:32.941 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'ts011f_plug_3_state_187a3efffee9bd66' received command OFF
2025-01-17 16:34:32.944 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'ts011f_plug_3_state_187a3efffee9bd66' predicted to become OFF
2025-01-17 16:34:32.946 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'ts011f_plug_3_state_187a3efffee9bd66' changed from ON to OFF
==> /var/log/openhab/openhab.log <==
2025-01-17 16:12:07.218 [INFO ] [b2172900-0e79-4af9-a054-94fef604ea04] - ============start==============
2025-01-17 16:12:07.219 [INFO ] [b2172900-0e79-4af9-a054-94fef604ea04] - script running
2025-01-17 16:12:07.221 [INFO ] [b2172900-0e79-4af9-a054-94fef604ea04] - =========++++end++++==========
2025-01-17 16:12:07.233 [INFO ] [b2172900-0e79-4af9-a054-94fef604ea04] - ============start==============
2025-01-17 16:12:07.235 [INFO ] [b2172900-0e79-4af9-a054-94fef604ea04] - script running
2025-01-17 16:12:07.236 [INFO ] [b2172900-0e79-4af9-a054-94fef604ea04] - =========++++end++++==========
2025-01-17 16:12:07.245 [INFO ] [b2172900-0e79-4af9-a054-94fef604ea04] - ============start==============
2025-01-17 16:12:07.247 [INFO ] [b2172900-0e79-4af9-a054-94fef604ea04] - script running
2025-01-17 16:12:07.249 [INFO ] [b2172900-0e79-4af9-a054-94fef604ea04] - =========++++end++++==========

I have tested triggering the item with Virtual items and the rule will run only 1 time. But as soon as I start testing it with real zigbee devices they will all have this issue.

Another weird behaviour I observe is that, setting up 2 rules with 2 zigbee devices that previously have this problem, then restarting the whole machine hosting openhab. The first rule with the zigbee device stopped having this problem (triggering the rule action only 1 time), and the second rule will still have the problem (triggering the rule 3 times).

Edit: After debugging further, the source could be from just one device which is this model Tuya TS011F_plug_3 control via MQTT | Zigbee2MQTT

Change the logging level for openhab.events.ItemStateUpdatedEvent to INFO. That will show the Item update events in events.log.

The most likely case is that zigbee2mqtt is publishing three messages which results in three updates to the Item which triggers the rule three timers.

Are you sure you want to trigger the rule by updates in the first place? Usually one only want to do something on a change to the Item, not just an update. Updates can occur for lots of reasons.

Oh the logs in zigbee2mqtt I am hosting is actually showing that there is only one published message.

info 2025-01-20 10:44:41z2m:mqtt: MQTT publish: topic 'z2m/wall_plug', payload '{"ac_frequency":null,"child_lock":"UNLOCK","current":0,"energy":0.11,"indicator_mode":"off/on","linkquality":160,"power":0,"power_apparent":null,"power_factor":null,"power_outage_memory":"off","state":"ON","voltage":227}'

But regardless, setting it to “changed” to solves this problem! So I think I will move on to using this method of trigger detection.

Really weird as I have other smart plugs and only this model brought up this issue.