Hi @Benjamin_Ginders,
Seeing the code you put in the previous post, it can be put in the subdriver, but I would make some changes.
For the battery I think it is best to use the xiaomi_utils.lua library.
This library make the configuration, the battery calculation and emit the event. For xiaomi temperature sensor it is working fine.
For illuminance, if device use the standard cluster you would not need a subdriver, simply add the Lifecycle doConfigure and run the configuration with the values you want. If you later want to add interval settings, reports in preferences, it can be done easily later.
This is what i would do
init.lua:
local capabilities = require "st.capabilities"
local ZigbeeDriver = require "st.zigbee"
local defaults = require "st.zigbee.defaults"
local constants = require "st.zigbee.constants"
local clusters = require "st.zigbee.zcl.clusters"
local device_management = require "st.zigbee.device_management"
local xiaomi_utils = require "xiaomi_utils"
local function do_configure(self, device)
-- configure Illuminance reports
--Maxt time interval 600 sec
local maxTime = 600
-- Reportable change 5 lux
local changeRep = math.floor(10000 * (math.log((5 + 1), 10)))
print ("Illuminance maxTime y changeRep: ",maxTime, changeRep )
device:send(device_management.build_bind_request(device, clusters.IlluminanceMeasurement.ID, self.environment_info.hub_zigbee_eui))
device:send(clusters.IlluminanceMeasurement.attributes.MeasuredValue:configure_reporting(device, 60, maxTime, changeRep))
device:configure()
end
---illuminance_measurement_defaults
local function illuminance_measurement_defaults(driver, device, value, zb_rx)
--local lux_value = math.floor(math.pow(10, (value.value - 1) / 10000)) --- defualt librarie edge lua
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
local zigbee_illuminance_driver = {
supported_capabilities = {
capabilities.illuminanceMeasurement,
capabilities.battery,
},
lifecycle_handlers = {
doConfigure = do_configure,
},
zigbee_handlers = {
attr = {
[clusters.basic_id] = {
[0xFF02] = xiaomi_utils.battery_handler,
[0xFF01] = xiaomi_utils.battery_handler
},
[clusters.IlluminanceMeasurement.ID] = {
[clusters.IlluminanceMeasurement.attributes.MeasuredValue.ID] = illuminance_measurement_defaults
}
},
},
}
--------- driver run ------
defaults.register_for_default_handlers(zigbee_illuminance_driver, zigbee_illuminance_driver.supported_capabilities)
local driver = ZigbeeDriver("zigbee-illuminance", zigbee_illuminance_driver)
driver:run()
Make a file with name “xiaomi_utils” in scr folder. Are from Zach Varberg, SmartThings:
or copyfrom his github link
https://github.com/varzac/EdgeDrivers/tree/master/xiaomi-sensor/src
-- Copyright 2021 Zach Varberg, SmartThings
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
local data_types = require "st.zigbee.data_types"
local capabilities = require "st.capabilities"
local buf = require "st.buf"
local xiaomi_utils = {}
xiaomi_utils.xiaomi_custom_data_type = {
deserialize = function(data_buf)
local out = {
items = {}
}
while data_buf:remain() > 0 do
local index = data_types.Uint8.deserialize(data_buf)
local data_type = data_types.ZigbeeDataType.deserialize(data_buf)
local data = data_types.parse_data_type(data_type.value, data_buf)
out.items[#out.items + 1] = {
index = index,
data_type = data_type,
data = data,
}
end
return out
end,
}
xiaomi_utils.emit_battery_event = function(self, device, battery_record)
local raw_bat_volt = (battery_record.value / 1000)
local raw_bat_perc = (raw_bat_volt - 2.5) * 100 / (3.0 - 2.5)
local bat_perc = math.floor(math.max(math.min(raw_bat_perc, 100), 0))
device:emit_event(capabilities.battery.battery(bat_perc))
end
xiaomi_utils.battery_handler = function(self, device, value)
if value.ID == data_types.CharString.ID then
local bytes = value.value
local message_buf = buf.Reader(bytes)
local xiaomi_data_type = xiaomi_utils.xiaomi_custom_data_type.deserialize(message_buf)
for i, item in ipairs(xiaomi_data_type.items) do
if item.data_type.value == data_types.Uint16.ID then
xiaomi_utils.emit_battery_event(self, device, item.data)
return
end
end
elseif value.ID == data_types.Structure.ID then
for i, record in ipairs(value.elements) do
if record.data_type.value == data_types.Uint16.ID then
xiaomi_utils.emit_battery_event(self, device, record.data)
return
end
end
end
end
return xiaomi_utils
If the configuratin works fine you must see log similar to this:
Cofiguration sent to device:
2021-12-21T16:39:40.648401266+00:00 PRINT Zigbee Sensor Illuminance maxTime y changeRep: 600 12041
2021-12-21T16:39:40.661478266+00:00 INFO Zigbee Sensor <ZigbeeDevice: a683d001-effc-4048-9381-0d9d22476fdc [0xD7DD] (Sensor)> sending Zigbee message: < ZigbeeMessageTx || Uint16: 0x0000, < AddressHeader || src_addr: 0x0000, src_endpoint: 0x01, dest_addr: 0xD7DD, dest_endpoint: 0x08, profile: 0x0000, cluster: 0x0021 >, < ZDOMessageBody || < ZDOHeader || seqno: 0x00 >, < BindRequest || src_address: 00124B001BAC6626, src_endpoint: 0x08, cluster: IlluminanceMeasurement, dest_addr_mode: 0x03, dest_address: D052A8727F4F0001, dest_endpoint: 0x01 > > >
2021-12-21T16:39:40.691325516+00:00 INFO Zigbee Sensor <ZigbeeDevice: a683d001-effc-4048-9381-0d9d22476fdc [0xD7DD] (Sensor)> sending Zigbee message: < ZigbeeMessageTx || Uint16: 0x0000, < AddressHeader || src_addr: 0x0000, src_endpoint: 0x01, dest_addr: 0xD7DD, dest_endpoint: 0x08, profile: 0x0104, cluster: IlluminanceMeasurement >, < ZCLMessageBody || < ZCLHeader || frame_ctrl: 0x00, seqno: 0x00, ZCLCommandId: 0x06 >, < ConfigureReporting || < AttributeReportingConfiguration || direction: 0x00, attr_id: 0x0000, DataType: Uint16, minimum_reporting_interval: 0x003C, maximum_reporting_interval: 0x0384, reportable_change: 0x2F09 > > > >
Device Configuration response:
2021-12-21T16:39:42.936992473+00:00 INFO Zigbee Sensor <ZigbeeDevice: a683d001-effc-4048-9381-0d9d22476fdc [0xD7DD] (Sensor)> received Zigbee message: < ZigbeeMessageRx || type: 0x00, < AddressHeader || src_addr: 0xD7DD, src_endpoint: 0x08, dest_addr: 0x0000, dest_endpoint: 0x01, profile: 0x0104, cluster: IlluminanceMeasurement >, lqi: 0xFF, rssi: -24, body_length: 0x0004, < ZCLMessageBody || < ZCLHeader || frame_ctrl: 0x18, seqno: 0x75, ZCLCommandId: 0x07 >, < ConfigureReportingReponse || ZclStatus: SUCCESS > > >