[ST Edge] Zigbee Illuminance Measurement

I have tried my Xiaomi Mijia Smart Light Sensor with the zigbee-illuminance-sensor driver, which pairs the device with the driver successfully, however the illuminance value doesn’t seem to change in the app nor do any values show up in the device logs for the illuminance values.

When checking the history tab on the device screen in the app no values are shown here also.

There’s currently an issue with the illuminanceMeasurement default handlers, this prevents the creation of the events.
This is already reported, if you want to use a Driver with this capability you should define your own handlers for now.

Thanks and apologies I had not noticed this in the reported bugs.

I did try creating a device handler based on the custom groovy DTH however I couldn’t get it to report anything either.
My experience with these drivers is very minimal, still trying to understand how all the prices fit together

Don’t worry @Benjamin_Ginders, you can post your questions and we can go solving them while you continue your tests.

Thanks I appreciate your support.

So far from what I understand, the main 2 components of configuring a device to SmartThings is:

  1. Capabilities - which is basically a library of functions in the form of JSON files

  2. Drivers - which map the device functions to the capabilities, also mapping actions and display tiles.

So for the current illuminance driver is it the driver which is faulty or the capabilities or something else?

Within the driver there are several files, for the basic case of an illuminance Sensor with just lux variables being passed I would have the following:

Config.yml - gives the driver a name/ package ID along with opening the device communication type e.g. ZIGBEE ZWAVE…

Fingerprint.yml - which provides the identification of the device and its capabilities either listed with codes or names e.g. illuminanceMeasurement or 0x0400
and LUMI.LUX… OR 0106/0104

INIT.lua - which provides the device commands receives and sends also mapping fingerprint items to actions

Test-luminamce-sensor/ Xiaomi-luminance-sensor - which adds the ability to add multiple device configurations e.g. Xiaomi, LUMI and Philips.

Is this correct?
If that is correct what files would I need to adjust to map a new handler… Is it a new capability or the LUA files

You need to:

  • Import the corresponding cluster:
local clusters = require "st.zigbee.zcl.clusters"
local IlluminanceMeasurement = clusters.IlluminanceMeasurement
  • Create the function that will handle the events, taking, for example, the default handler:
function illuminance_attr_handler(driver, device, value, zb_rx)
  local lux_value = math.floor(10 ^ ((value.value - 1) / 10000))
  device:emit_event_for_endpoint(zb_rx.address_header.src_endpoint.value, capabilities.illuminanceMeasurement.illuminance(lux_value))
end

There, the Math.pow() is replaced as that’s the one causing an error.

local mydriver = {
    supported_capabilities = { ... },
    zigbee_handlers = {
        global = {},
        cluster = {},
        attr = {
            [IlluminanceMeasurement.ID] = {
                [IlluminanceMeasurement.attributes.MeasuredValue.ID] = illuminance_attr_handler
            }
        }
    }
 }

Let me know if you have any questions.

@nayelyz is this all to be added to the init.lua.

just tried adding and I get a fatal error in the logs:
FATAL Zigbee Illuminance Sensor Lua: runtime error: [string “init.lua”]:43: attempt to index a nil value (global ‘zigbee_illuminance_driver’)

EDIT:

after correcting a few bugs, I am now getting logs for the device but no illuminance measurements:

Ok, do you see any logs of the reporting configuration from the illuminanceMeasurement cluster?
If not, please add the doConfigure lifecycle as in this example, just ommit the “driver specific behavior” so it would look like this:

local function configure_device(self, device)
    device:configure()
end

Check if after this, there are is a TX message of the reporting configuration and if you receive the RX message of “success”

I am getting the below RX messages

ok, that’s before making the change I suggested?

It seems the reporting configuration is not being sent, you should see a similar message to the one below, a sample from the temperatureMeasurement cluster:

Apologies for the delay in response, I have had a busy week at work.

Progress, I can now see battery reports but I cannot see the illuminance still.

Below is the init.lua from the src folder:

local capabilities = require "st.capabilities"
local ZigbeeDriver = require "st.zigbee"
local defaults = require "st.zigbee.defaults"
local constants = require "st.zigbee.constants"


local zigbee_illuminance_driver = {
  supported_capabilities = {
    capabilities.illuminanceMeasurement,
    capabilities.battery,
  },
sub_drivers = {
                require("lumi-lux")
              }
    }

defaults.register_for_default_handlers(zigbee_illuminance_driver, zigbee_illuminance_driver.supported_capabilities)
local driver = ZigbeeDriver("zigbee-illuminance", zigbee_illuminance_driver)
driver:run(false)

And here is the init.lua for the sub driver:

local constants = require "st.zigbee.constants"
local clusters = require "st.zigbee.zcl.clusters"
local capabilities = require "st.capabilities"
local battery_defaults = require "st.zigbee.defaults.battery_defaults"
local log = require "log"

local function configure_device(self, device)
  device:configure()
end

local  XIAOMI_ILLUMINANCE_SENSOR_FINGERPRINTS = {
    {mfr = "LUMI", model = "lumi.sen_ill.mgl01"},
    {mfr = "XIAOMI", model = "lumi.sen_ill.mgl01"},
    {mfr = "LUMI", model = "lumi.sen_ill"},
    {mfr = "XIAOMI", model = "lumi.sen_ill"}
}

local is_xiaomi_illuminance = function(opts, driver, device)
    for _, fingerprint in ipairs(XIAOMI_ILLUMINANCE_SENSOR_FINGERPRINTS) do
        if device:get_manufacturer() == fingerprint.mfr and device:get_model() == fingerprint.model then
            return true
        end
    end
    return true
end


local function illuminance_attr_handler(driver, device, value, zb_rx)
  log.debug("illuminance_attr_handler " .. tostring(value))

  local lux_value = value.value --math.floor(10 ^ ((value.value - 1) / 10000))
  device:emit_event_for_endpoint(zb_rx.address_header.src_endpoint.value, capabilities.illuminanceMeasurement.illuminance(lux_value))
end

local function battery_attr_handler(driver, device, value, zb_rx)
    log.debug("battery_attr_handler value is " .. tostring())
    device:emit_event(capabilities.battery.battery(55)) 
end

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

local lumi_illuminance_handler = {
  NAME = "Lumi Illuminance Handler",
  lifecycle_handlers = {
    init = battery_defaults.build_linear_voltage_init(2.1, 3.0),
    doConfigure = configure_device
  },
  zigbee_handlers = {
    attr = {
      [clusters.IlluminanceMeasurement.ID]= {
        [clusters.IlluminanceMeasurement.attributes.MeasuredValue.ID] = illuminance_attr_handler
      },
      [clusters.Basic.ID] = {
        [0xFF01] = battery_attr_handler 
      }
    }
  },
  can_handle = is_xiaomi_illuminance
} 
return lumi_illuminance_handler 

For the logs I am seeing the following:

2021-09-10T21:17:33.837578612+00:00 TRACE Zigbee Illuminance Sensor  Setup driver zigbee-illuminance with lifecycle handlers:
DeviceLifecycleDispatcher: zigbee-illuminance
  default_handlers:
    infoChanged:
    driverSwitched:
    removed:
    added:
    init:
    doConfigure:
  child_dispatchers:
    DeviceLifecycleDispatcher: zigbee-illuminance -> Lumi Illuminance Handler
      default_handlers:
        init:
        doConfigure:
      child_dispatchers:

2021-09-10T21:17:33.844228945+00:00 TRACE Zigbee Illuminance Sensor  Setup driver zigbee-illuminance with Capability handlers:
CapabilityCommandDispatcher: zigbee-illuminance
  default_handlers:
    refresh:
      refresh
  child_dispatchers:
    CapabilityCommandDispatcher: zigbee-illuminance -> Lumi Illuminance Handler
      default_handlers:
      child_dispatchers:

2021-09-10T21:17:33.854522612+00:00 TRACE Zigbee Illuminance Sensor  Setup driver zigbee-illuminance with Zigbee handlers:
ZigbeeMessageDispatcher: zigbee-illuminance
  default_handlers:
    attr:
      ZclClusterAttributeValueHandler: cluster: PowerConfiguration, attribute: BatteryVoltage
      ZclClusterAttributeValueHandler: cluster: PowerConfiguration, attribute: BatteryPercentageRemaining
      ZclClusterAttributeValueHandler: cluster: IlluminanceMeasurement, attribute: MeasuredValue
    global:
    cluster:
    zdo:
  child_dispatchers:
    ZigbeeMessageDispatcher: zigbee-illuminance -> Lumi Illuminance Handler
      default_handlers:
        attr:
          ZclClusterAttributeValueHandler: cluster: Basic, attribute: 0xFF01
          ZclClusterAttributeValueHandler: cluster: IlluminanceMeasurement, attribute: MeasuredValue
        global:
        cluster:
        zdo:
      child_dispatchers:

2021-09-10T21:17:33.903933612+00:00 TRACE Zigbee Illuminance Sensor  Received event with handler _resync
2021-09-10T21:17:33.910001945+00:00 TRACE Zigbee Illuminance Sensor  Received event with handler environment_info
2021-09-10T21:17:33.924586612+00:00 TRACE Zigbee Illuminance Sensor  Found DeviceLifecycleDispatcher handler in zigbee-illuminance -> Lumi Illuminance Handler
2021-09-10T21:17:33.931271945+00:00 DEBUG Zigbee Illuminance Sensor  Lumi Aqara Illuminance Sensor device thread event handled
2021-09-10T21:17:33.937628945+00:00 TRACE Zigbee Illuminance Sensor  Received event with handler _resync
2021-09-10T21:17:33.943220279+00:00 TRACE Zigbee Illuminance Sensor  Received event with handler environment_info
2021-09-10T21:17:33.949361612+00:00 DEBUG Zigbee Illuminance Sensor  Z-Wave hub node ID environment changed.
2021-09-10T21:17:53.872575281+00:00 TRACE Zigbee Illuminance Sensor  Received event with handler capability
2021-09-10T21:17:53.884203281+00:00 INFO Zigbee Illuminance Sensor  <ZigbeeDevice: 2504b279-2583-48da-ba07-331622ac5954 [0xD77A] (Lumi Aqara Illuminance Sensor)> received command: {"component":"main","args":[],"command":"refresh","positional_args":[],"capability":"refresh"}
2021-09-10T21:17:53.891019281+00:00 TRACE Zigbee Illuminance Sensor  Found CapabilityCommandDispatcher handler in zigbee-illuminance
2021-09-10T21:17:53.903138614+00:00 INFO Zigbee Illuminance Sensor  <ZigbeeDevice: 2504b279-2583-48da-ba07-331622ac5954 [0xD77A] (Lumi Aqara Illuminance Sensor)> sending Zigbee message: < ZigbeeMessageTx || Uint16: 0x0000, < AddressHeader || src_addr: 0x0000, src_endpoint: 0x01, dest_addr: 0xD77A, dest_endpoint: 0x01, profile: 0x0104, cluster: PowerConfiguration >, < ZCLMessageBody || < ZCLHeader || frame_ctrl: 0x00, seqno: 0x00, ZCLCommandId: 0x00 >, < ReadAttribute || AttributeId: 0x0020 > > >

and my folder layout is:

zigbee-illuminance-sensor

  • profiles
    • lluminance-battery.yml
  • src
    • lumi-lux
      • init.lua
    • test
      • test-illuminance-sensor.lua
  • init.lua
  • config.yml
    -fingerprint.yml

Are they all the logs?

I see only one TX message for the PowerConfiguration cluster to read the attribute 0x0020. Do you receive a value in the battery capability?

Try to add this in the subdriver:

--This goes in the imports section
local zcl_clusters = require "st.zigbee.zcl.clusters"
local lightMeasurement = zcl_clusters.IlluminanceMeasurement

--this in the do_configure function before device:configure()
device:send(device_management.build_bind_request(device, lightMeasurement.ID, self.environment_info.hub_zigbee_eui))
device:send(lightMeasurement.attributes.MeasuredValue:configure_reporting(device, minReportingInterval, maxReportingInterval, reportingChange))

Thanks @nayelyz I will give that a try.

Yes I am receiving battery values successful on in the logs and the device screen in the app.

But nothing for the illuminance at all, will see if this change works though.