Device:refresh() method put in a function to handle it in a subdriver or the main driver Zwave (Z-Wave Switch stock and others)

Hi @nayelyz

device:refresh() method put in a function to handle it in a subdriver or the main driver Zwave (Z-Wave Switch stock and others) causes the driver to enter an infinite loop until the driver is reset

local function device_refresh( self, device)
    device:refresh()
end

local fibaro_double_switch = {
  NAME = "fibaro double switch",
  capability_handlers = {
    [capabilities.refresh.ID] = {
      [capabilities.refresh.commands.refresh.NAME] = device_refresh,
    }
  },
Loop Logs after refresh command until driver reinitialize it self

2022-09-30T19:12:29.773944967+00:00 TRACE Z-Wave Switch and Child Mc Found CapabilityCommandDispatcher handler in zwave_switch
2022-09-30T19:12:29.799463301+00:00 TRACE Z-Wave Switch and Child Mc Found CapabilityCommandDispatcher handler in zwave_switch
2022-09-30T19:12:29.865944967+00:00 TRACE Z-Wave Switch and Child Mc Found CapabilityCommandDispatcher handler in zwave_switch
2022-09-30T19:12:29.892842634+00:00 TRACE Z-Wave Switch and Child Mc Found CapabilityCommandDispatcher handler in zwave_switch
2022-09-30T19:12:29.949766634+00:00 TRACE Z-Wave Switch and Child Mc Found CapabilityCommandDispatcher handler in zwave_switch
2022-09-30T19:12:30.021680967+00:00 TRACE Z-Wave Switch and Child Mc Found CapabilityCommandDispatcher handler in zwave_switch
2022-09-30T19:12:30.132494967+00:00 TRACE Z-Wave Switch and Child Mc Found CapabilityCommandDispatcher handler in zwave_switch
2022-09-30T19:12:30.769417301+00:00 TRACE Z-Wave Switch and Child Mc Found CapabilityCommandDispatcher handler in zwave_switch
2022-09-30T19:12:31.297622968+00:00 TRACE Z-Wave Switch and Child Mc Found CapabilityCommandDispatcher handler in zwave_switch
2022-09-30T19:12:31.420268635+00:00 TRACE Z-Wave Switch and Child Mc Found CapabilityCommandDispatcher handler in zwave_switch
2022-09-30T19:12:31.507650301+00:00 TRACE Z-Wave Switch and Child Mc Found CapabilityCommandDispatcher handler in zwave_switch
2022-09-30T19:12:31.610564968+00:00 TRACE Z-Wave Switch and Child Mc Found CapabilityCommandDispatcher handler in zwave_switch
2022-09-30T19:12:41.137265639+00:00 TRACE Z-Wave Switch and Child Mc Setup driver zwave_switch with lifecycle handlers:
DeviceLifecycleDispatcher: zwave_switch
default_handlers:
added:
removed:
infoChanged:
driverSwitched:
doConfigure:
init:
child_dispatchers:
DeviceLifecycleDispatcher: zwave_switch → eaton accessory dimmer
default_handlers:
child_dispatchers:
DeviceLifecycleDispatcher: zwave_switch → Inovelli LED
default_handlers:
child_dispatchers:
DeviceLifecycleDispatcher: zwave_switch → inovelli dimmer
default_handlers:
child_dispatchers:
DeviceLifecycleDispatcher: zwave_switch → Dawon smart plug
default_handlers:
child_dispatchers:
DeviceLifecycleDispatcher: zwave_switch → Inovelli 2 channel smart plug
default_handlers:
init:
doConfigure:
child_dispatchers:
DeviceLifecycleDispatcher: zwave_switch → zwave dual switch
default_handlers:
added:
child_dispatchers:
DeviceLifecycleDispatcher: zwave_switch → zwave dual switch → fibaro walli double switch
default_handlers:
init:
child_dispatchers:

With Z-wave Switch stock with device:refresh() in subdriver “fibaro-double-switch”
Tihis log is after driver self reinitialized and to perform added lifecycle added, that contains device:refresh()

2022-09-30T19:10:32.915238004+00:00 TRACE Z-Wave Switch Z-Wave Device: bd5c15c7-76f5-4430-b95d-c8ea349e9f61
Manufacturer: 0x010F Product Type: 0x0203 Product ID: 0x1000
[0]: SWITCH_MULTILEVEL, SWITCH_BINARY, DEVICE_RESET_LOCALLY, ASSOCIATION, CONFIGURATION, MULTI_CHANNEL_ASSOCIATION, MULTI_CHANNEL, PROTECTION, CENTRAL_SCENE, ZWAVEPLUS_INFO, VERSION, MANUFACTURER_SPECIFIC, ASSOCIATION_GRP_INFO, POWERLEVEL, APPLICATION_STATUS, CRC_16_ENCAP, METER, NOTIFICATION, SECURITY, FIRMWARE_UPDATE_MD [1]: SWITCH_MULTILEVEL, SWITCH_BINARY, DEVICE_RESET_LOCALLY, ASSOCIATION, CONFIGURATION, MULTI_CHANNEL_ASSOCIATION, MULTI_CHANNEL, PROTECTION, CENTRAL_SCENE, ZWAVEPLUS_INFO, VERSION, MANUFACTURER_SPECIFIC, ASSOCIATION_GRP_INFO, POWERLEVEL, APPLICATION_STATUS, CRC_16_ENCAP, METER, NOTIFICATION, SECURITY, FIRMWARE_UPDATE_MD
2022-09-30T19:10:32.930331004+00:00 TRACE Z-Wave Switch Received event with handler _resync
2022-09-30T19:10:32.939160004+00:00 TRACE Z-Wave Switch Received event with handler environment_info
2022-09-30T19:10:32.982978338+00:00 TRACE Z-Wave Switch Found DeviceLifecycleDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:32.989219338+00:00 DEBUG Z-Wave Switch Luz Entrada device thread event handled
2022-09-30T19:10:32.999226671+00:00 TRACE Z-Wave Switch Received event with handler environment_info
2022-09-30T19:10:33.004994004+00:00 DEBUG Z-Wave Switch Z-Wave hub node ID environment changed.
2022-09-30T19:10:33.027262338+00:00 TRACE Z-Wave Switch Received event with handler device_lifecycle
2022-09-30T19:10:33.039204671+00:00 INFO Z-Wave Switch <ZwaveDevice: bd5c15c7-76f5-4430-b95d-c8ea349e9f61 [1E] (Luz Entrada)> received lifecycle event: added
2022-09-30T19:10:33.065859004+00:00 TRACE Z-Wave Switch Found DeviceLifecycleDispatcher handler in zwave_switch
2022-09-30T19:10:33.077784004+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:33.089521338+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:33.101314671+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:33.192578338+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:33.236287671+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:33.264326338+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:33.279162671+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch2022-09-30T19:10:32.915238004+00:00 TRACE Z-Wave Switch Z-Wave Device: bd5c15c7-76f5-4430-b95d-c8ea349e9f61
Manufacturer: 0x010F Product Type: 0x0203 Product ID: 0x1000
[0]: SWITCH_MULTILEVEL, SWITCH_BINARY, DEVICE_RESET_LOCALLY, ASSOCIATION, CONFIGURATION, MULTI_CHANNEL_ASSOCIATION, MULTI_CHANNEL, PROTECTION, CENTRAL_SCENE, ZWAVEPLUS_INFO, VERSION, MANUFACTURER_SPECIFIC, ASSOCIATION_GRP_INFO, POWERLEVEL, APPLICATION_STATUS, CRC_16_ENCAP, METER, NOTIFICATION, SECURITY, FIRMWARE_UPDATE_MD [1]: SWITCH_MULTILEVEL, SWITCH_BINARY, DEVICE_RESET_LOCALLY, ASSOCIATION, CONFIGURATION, MULTI_CHANNEL_ASSOCIATION, MULTI_CHANNEL, PROTECTION, CENTRAL_SCENE, ZWAVEPLUS_INFO, VERSION, MANUFACTURER_SPECIFIC, ASSOCIATION_GRP_INFO, POWERLEVEL, APPLICATION_STATUS, CRC_16_ENCAP, METER, NOTIFICATION, SECURITY, FIRMWARE_UPDATE_MD
2022-09-30T19:10:32.930331004+00:00 TRACE Z-Wave Switch Received event with handler _resync
2022-09-30T19:10:32.939160004+00:00 TRACE Z-Wave Switch Received event with handler environment_info
2022-09-30T19:10:32.982978338+00:00 TRACE Z-Wave Switch Found DeviceLifecycleDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:32.989219338+00:00 DEBUG Z-Wave Switch Luz Entrada device thread event handled
2022-09-30T19:10:32.999226671+00:00 TRACE Z-Wave Switch Received event with handler environment_info
2022-09-30T19:10:33.004994004+00:00 DEBUG Z-Wave Switch Z-Wave hub node ID environment changed.
2022-09-30T19:10:33.027262338+00:00 TRACE Z-Wave Switch Received event with handler device_lifecycle
2022-09-30T19:10:33.039204671+00:00 INFO Z-Wave Switch <ZwaveDevice: bd5c15c7-76f5-4430-b95d-c8ea349e9f61 [1E] (Luz Entrada)> received lifecycle event: added
2022-09-30T19:10:33.065859004+00:00 TRACE Z-Wave Switch Found DeviceLifecycleDispatcher handler in zwave_switch
2022-09-30T19:10:33.077784004+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:33.089521338+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:33.101314671+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:33.192578338+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:33.236287671+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:33.264326338+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch
2022-09-30T19:10:33.279162671+00:00 TRACE Z-Wave Switch Found CapabilityCommandDispatcher handler in zwave_switch → fibaro double switch

I have seen in several subdrivers that this method is not used directly and a GET command is used to obtain the status of each C.C.

The Z-Wave Switch stock and others have in main driver init.lua device:refresh() in the function that handles the lifecycle added. It works fine normally

local function device_added(driver, device)
  device:refresh()
end

-------------------------------------------------------------------------------------------
-- Register message handlers and run driver
-------------------------------------------------------------------------------------------
local driver_template = {
  supported_capabilities = {
    capabilities.switch,
    capabilities.switchLevel,
    capabilities.battery,
    capabilities.energyMeter,
    capabilities.powerMeter,
    capabilities.colorControl,
    capabilities.button,
    capabilities.temperatureMeasurement,
    capabilities.relativeHumidityMeasurement
  },
  sub_drivers = {
    require("eaton-accessory-dimmer"),
    require("inovelli-LED"),
    require("dawon-smart-plug"),
    require("inovelli-2-channel-smart-plug"),
    require("zwave-dual-switch"),
    require("eaton-anyplace-switch"),
    require("fibaro-wall-plug-us"),
    require("dawon-wall-smart-switch"),
    require("zooz-power-strip"),
    require("aeon-smart-strip"),
    require("qubino-switches"),
    require("fibaro-double-switch"),
    require("fibaro-single-switch"),
    require("eaton-5-scene-keypad"),
    require("ecolink-switch"),
    require("zooz-zen-30-dimmer-relay")
  },
  lifecycle_handlers = {
    init = device_init,
    infoChanged = info_changed,
    doConfigure = do_configure,
    added = device_added
  }

Can device:refresh() be used to handle Refresh capability command manually on Z-Wave drivers as used in Zigbee without problems?

There’s your loop - you’re calling refresh from within the refresh handler

I don’t understand why.

It only enters this function when the External Refresh command is received and what this function executes is the reading of the attributes in zigbee or the C.C. in Zwave, it doesn’t resend the refresh() command and so don’t have to go back in here.

I did this test on a zigbee controller. I added a print <<<<<<< refresh function handler>>>>>>>> to see how many times it executes the function and it works perfectly in zigbee drivers, it only executes it once:

local function device_refresh( self, device)
   print("<<<<<<< refresh fuction handler>>>>>>>>")
    device:refresh()

end

---- Driver template config
local zigbee_switch_driver_template = {
  supported_capabilities = {
    capabilities.switch,
    capabilities.battery,
    capabilities.refresh
  },
  lifecycle_handlers = {
    infoChanged = random.do_Preferences,
    init = random.do_init,
    removed = random.do_removed,
    driverSwitched = driver_Switched,
    doConfigure = do_configure
  },
  capability_handlers = {
    [capabilities.refresh.ID] = {
      [capabilities.refresh.commands.refresh.NAME] = device_refresh,
    },

2022-09-30T21:01:11.822078813+00:00 TRACE Zigbee Switch Mc-test Received event with handler capability
2022-09-30T21:01:11.830761480+00:00 INFO Zigbee Switch Mc-test <ZigbeeDevice: 0a4b023f-c186-43a2-b025-938277836195 [0x4B10] (Lidl Plug)> received command: {“args”:{},“capability”:“refresh”,“command”:“refresh”,“component”:“main”,“positional_args”:{}}
2022-09-30T21:01:11.836742480+00:00 TRACE Zigbee Switch Mc-test Found CapabilityCommandDispatcher handler in Zigbee_Switch
2022-09-30T21:01:11.841834480+00:00 PRINT Zigbee Switch Mc-test <<<<<<< refresh fuction handler>>>>>>>>
2022-09-30T21:01:11.850298480+00:00 INFO Zigbee Switch Mc-test <ZigbeeDevice: 0a4b023f-c186-43a2-b025-938277836195 [0x4B10] (Lidl Plug)> sending Zigbee message: < ZigbeeMessageTx || Uint16: 0x0000, < AddressHeader || src_addr: 0x0000, src_endpoint: 0x01, dest_addr: 0x4B10, dest_endpoint: 0x0B, profile: 0x0104, cluster: OnOff >, < ZCLMessageBody || < ZCLHeader || frame_ctrl: 0x00, seqno: 0x00, ZCLCommandId: 0x00 >, < ReadAttribute || AttributeId: 0x0000 > > >
2022-09-30T21:01:11.876072813+00:00 DEBUG Zigbee Switch Mc-test Lidl Plug device thread event handled
2022-09-30T21:01:11.925079146+00:00 TRACE Zigbee Switch Mc-test Received event with handler zigbee
2022-09-30T21:01:11.936168813+00:00 INFO Zigbee Switch Mc-test <ZigbeeDevice: 0a4b023f-c186-43a2-b025-938277836195 [0x4B10] (Lidl Plug)> received Zigbee message: < ZigbeeMessageRx
|| type: 0x00, < AddressHeader || src_addr: 0x4B10, src_endpoint: 0x0B, dest_addr: 0x0000, dest_endpoint: 0x01, profile: 0x0104, cluster: OnOff >, lqi: 0xC8, rssi: -71, body_length: 0x0008, < ZCLMessageBody || < ZCLHeader || frame_ctrl: 0x18, seqno: 0x52, ZCLCommandId: 0x01 >, < ReadAttributeResponse || < AttributeRecord || AttributeId: 0x0000, ZclStatus: SUCCESS, DataType: Boolean, OnOff: false > > > >
2022-09-30T21:01:11.971677146+00:00 TRACE Zigbee Switch Mc-test Found ZigbeeMessageDispatcher handler in Zigbee_Switch
2022-09-30T21:01:11.978068480+00:00 INFO Zigbee Switch Mc-test Executing ZclClusterAttributeValueHandler: cluster: OnOff, attribute: OnOff
2022-09-30T21:01:11.988868813+00:00 PRINT Zigbee Switch Mc-test value.value >>>>>>>>>>> false
2022-09-30T21:01:11.994052146+00:00 PRINT Zigbee Switch Mc-test New value.value >>>>>>>>>>> false
2022-09-30T21:01:12.004728480+00:00 INFO Zigbee Switch Mc-test <ZigbeeDevice: 0a4b023f-c186-43a2-b025-938277836195 [0x4B10] (Lidl Plug)> emitting event: {“attribute_id”:“signalMetrics”,“capability_id”:“legendabsolute60149.signalMetrics”,“component_id”:“main”,“state”:{“value”:“dni: 0x4B10, lqi: 200, rssi: -71dBm”},“visibility”:{“displayed”:false}}
2022-09-30T21:01:12.041103146+00:00 PRINT Zigbee Switch Mc-test <<<<<<< Power meter Timer >>>>> OFF
2022-09-30T21:01:12.047384146+00:00 INFO Zigbee Switch Mc-test <ZigbeeDevice: 0a4b023f-c186-43a2-b025-938277836195 [0x4B10] (Lidl Plug)> emitting event: {“attribute_id”:“switch”,“capability_id”:“switch”,“component_id”:“main”,“state”:{“value”:“off”}}
2022-09-30T21:01:12.072069146+00:00 DEBUG Zigbee Switch Mc-test Lidl Plug device thread event handled
2022-09-30T21:01:33.296563149+00:00 DEBUG Zigbee Switch Mc-test driver device thread event handled
2022-09-30T21:02:03.305179486+00:00 DEBUG Zigbee Switch Mc-test driver device thread event handled
2022-09-30T21:02:33.312842823+00:00 DEBUG Zigbee Switch Mc-test driver device thread event handled
2022-09-30T21:03:03.319861160+00:00 DEBUG Zigbee Switch Mc-test driver device thread event handled
2022-09-30T21:03:33.317558497+00:00 DEBUG Zigbee Switch Mc-test driver device thread event handled
2022-09-30T21:04:03.322744833+00:00 DEBUG Zigbee Switch Mc-test driver device thread event handled
2022-09-30T21:04:33.322983883+00:00 DEBUG Zigbee Switch Mc-test driver device thread event handled
2022-09-30T21:05:03.338529898+00:00 DEBUG Zigbee Switch Mc-test driver device thread event handled
2022-09-30T21:05:33.335751579+00:00 DEBUG Zigbee Switch Mc-test driver device thread event handled
2022-09-30T21:06:03.346205260+00:00 DEBUG Zigbee Switch Mc-test driver device thread event handled
2022-09-30T21:06:12.063239264+00:00 TRACE Zigbee Switch Mc-test Received event with handler zigbee

I asked the team and they mentioned the refresh function on a Z-Wave device just sends the refresh command to the driver, which is why this ends up in an infinite loop. So:

2 Likes

Feedback - this isn’t a good architecture - it should behave the same on both objects since both are special instances of the parent Driver object and since zwave default_refresh accomplishes the same outcome as the ZigBee refresh, it should be normalized and behave the same way in both cases.

2 Likes

I can’t read that without hearing fingernails dragging down a blackboard.

I am sure it may have applications outside creating infinite loops but it instinctively feels like something to be hidden away in a jam jar on a shelf in the shed.

1 Like