Nullable types in Matter API

I found a workaround, but wanted to point out that some generated cluster definitions do not take into account the nullability of attributes, for instance OnLevel:

local request = clusters.LevelControl.attributes.OnLevel:write(device, endpoint_id, value)

If value is an integer it sends the command fine, if it’s nil it throws a validation error:

[string “st/matter/data_types/init.lua”]:164: Error creating Uint8: [string “st/matter/data_types/base_defs/UintABC.lua”]:95: Uint8 value must be an integer"

Also tried to build a Null type and pass it as value, just in case.

local null_value = data_types.validate_or_build_type(nil, data_types.Null)

But will complain again:

“[string “st/matter/generated/zap_clusters/LevelControl…”]:68: Expecting Uint8 (0x04) for “Uint8” received Null: Null”

OnLevel is a nullable attribute, has the X qualifier in Matter specification and the nullable field is true in the XML cluster definition.

The workaround

If someone needs to write a nullable attribute and the generated cluster won’t let them, just manually create the whole request for the null value, in this case it would be:

local data_types = require "st.matter.data_types"
local cluster_base = require "st.matter.cluster_base"
local null_value = data_types.validate_or_build_type(nil, data_types.Null)  
local request = cluster_base.write(device, endpoint_id, 0x0008, 0x0011, nil, null_value)

Hey @mocelet ,

Thanks for bringing this up! We’ve noticed that some generated cluster definitions don’t handle nullable attributes correctly, like OnLevel. We’re currently checking this issue with the team.

In the meantime, your workaround is a great solution for writing nullable attributes manually. If there are any updates or further findings, we’ll make sure to share them here.

Thanks for your patience and for sharing your workaround!