python-matter-server icon indicating copy to clipboard operation
python-matter-server copied to clipboard

create groups

Open goossensbas opened this issue 11 months ago • 13 comments

Hello all,

I am using the python-matter-server docker container as a stand-alone application. I wrote an interface in python to test the capabilities of Matter.

The documentation of the server is somewhat limited. Basic functions like onoff and commissioning work, but other features like binding a switch to a light is not documented at all. (i managed to do this by writing the values in the correct cluster attributes)

I want to create a group for 2 or more light devices, but i can't assemble the correct command.

my code:

async def add_node_to_group(client, group_id, node_id):
    res = await client.read_attribute(node_id,f"1/4/0")
    print(f"{res}")
    current_membership = res.get("1/4/0", 0)  # Extracting the attribute value
    print(f"Current Membership: {current_membership}")

    # Define the AddGroup command
    command = clusters.Groups.Commands.AddGroup(
        groupId=group_id,  # Replace with your desired group ID
        groupName=f"group{group_id}"  # Replace with your desired group name
    )
    payload = dataclass_to_dict(command)

    add_group_command = json.dumps({
        "message_id": "example",
        "command": "device_command",
        "args": {
            "endpoint_id": 1,
            "node_id": node_id,
            "payload": payload,
            "cluster_id": command.cluster_id,
            "command_name": "AddGroup"
        }
    })
    # Write the updated group membership attribute
    write_res = await client.send_device_command(node_id, 1, add_group_command)
    print(f"Write Attribute Response: {write_res}")

gives the error:

Error during Matter client operation: Groups.Commands.AddGroup.__init__() got an unexpected keyword argument 'GroupId'
Disconnected from Matter server.

I also tried GroupId and GroupName with capital letters, but it gives the same error.

command = clusters.Groups.Commands.AddGroup(
        GroupId=group_id,  # Replace with your desired group ID
        GroupName=f"group{group_id}"  # Replace with your desired group name
    )

How can i create a group and add nodes to it? How do i send group commands?

Thanks in advance for the help!

goossensbas avatar Feb 09 '25 10:02 goossensbas

UPDATE

I read through the matter documentation, and i figured out i need a group key first, so i wrote one and bound it to a group ID.

Adding the key and binding it was successful, but i still can't add an endpoint to the group.


async def write_group_key(client, node_id, group_id):
    group_key_set_id = group_id
    group_key_security_policy = 0
    # Randomly generate a 16-byte (128-bit) key
    # group_key = secrets.token_bytes(16)
    group_key=bytes.fromhex("d0d1d2d3d4d5d6d7d8d9dadbdcdddedf")
    group_start_time = 2220000

    group_key_set = clusters.GroupKeyManagement.Structs.GroupKeySetStruct(
        groupKeySetID=group_key_set_id,
        groupKeySecurityPolicy=group_key_security_policy,
        epochKey0=group_key,
        epochStartTime0=group_start_time
    )

    # Add key to device
    command = clusters.GroupKeyManagement.Commands.KeySetWrite(group_key_set)
    res = await send_command_to_node(client, node_id, 0, command)
    print(f"Response: {res}")  

    # bind key to group
    binding_entry = [{"groupId": group_id, "groupKeySetID": group_key_set_id}]
    res = await client.write_attribute(node_id,f"0/63/0", binding_entry)
    print(f"Response: {res}")

result:

Command GroupKeyManagement.Commands.KeySetWrite(groupKeySet=GroupKeyManagement.Structs.GroupKeySetStruct(groupKeySetID=2052, groupKeySecurityPolicy=0, epochKey0=b'\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf', epochStartTime0=2220000, epochKey1=Null, epochStartTime1=Null, epochKey2=Null, epochStartTime2=Null)) sent to node 1, endpoint 0, cluster 63
Response: None
Response: [{'Path': {'EndpointId': 0, 'ClusterId': 63, 'AttributeId': 0}, 'Status': 0}]

Then i wanted to write the group ID to an endpoint, but that doesn't work.


async def add_node_to_group(client, node_id, group_id):    
    # Define the AddGroup command
    command = clusters.Groups.Commands.AddGroup(
        groupID=int(group_id),  # Replace with your desired group ID
        groupName=f"group{group_id}"  # Replace with your desired group name
    )
    try:
        res = await send_command_to_node(client, node_id, 1, command)
        print(f"Write Attribute Response: {res}")
    except Exception as e:
        print(f"Error during AddGroup command: {e}")

result:

Command Groups.Commands.AddGroup(groupID=2052, groupName='group2052') sent to node 1, endpoint 1, cluster 4
Write Attribute Response: None

When i read the group info with this function:

async def read_group(client, node_id, group_id):
    
    try:
        # Define the AddGroup command
        command = clusters.Groups.Commands.GetGroupMembership()
        res = await send_command_to_node(client, node_id, 1, command)
        print(f"Write Attribute Response: {res}")
    except Exception as e:
        print(f"Error during GetGroupMembership command: {e}")

    try:    
        command = clusters.Groups.Commands.ViewGroup(groupID=group_id)
        res = await send_command_to_node(client, node_id, 1, command)
        print(f"Write Attribute Response: {res}")
    except Exception as e:
        print(f"Error during ViewGroup command: {e}")

I get the result:

Command Groups.Commands.GetGroupMembership(groupList=[]) sent to node 1, endpoint 1, cluster 4
Write Attribute Response: None
Command Groups.Commands.ViewGroup(groupID=2052) sent to node 1, endpoint 1, cluster 4
Write Attribute Response: None

goossensbas avatar Feb 16 '25 09:02 goossensbas

group management is not yet supported, its on the list to add to the features.

marcelveldt avatar Feb 17 '25 10:02 marcelveldt

@marcelveldt Thank you for your feedback. now i know that my script works :-)

Would it be possible to do some adjustments myself so it works? Where could i find the source code of what's running in the container?

thanks in advance, and have a nice weekend!

goossensbas avatar Feb 21 '25 13:02 goossensbas

Currently, I have some Nanoleaf downlights (Matter over thread) in a Home Assistant group. Popcorning effect is very evident. Will the future group management feature address this? (If so, when is this anticipated to be available?)

joe-sydney avatar Jul 06 '25 10:07 joe-sydney

Will the future group management feature address this?

That's a good question. Hopefully yes, but, especially when on thread and more ecosystems paired with the devices (so other beside Home Assistant like Apple, Google, ...) then this might become problematic when "dimming" or "color changes" join the game. Just On/Off should work well ...

Apollon77 avatar Jul 07 '25 15:07 Apollon77

Groups would be great! I have Nanoleaf downlights in a large room. I added twenty of them to a Home Assistant group and used them with an Inovelli White switch in smart bulb mode. The whole network crashes if I turn them on and then turn on another smaller group of four that are nearby with a different switch, it's just to much traffic for the network. Everything including all of my Inovelli switches get marked offline in the Matter Server.

Bindings have help significantly with delays in other areas though!

blue-hound avatar Aug 22 '25 15:08 blue-hound

What blue-hound said. Mixed group with 2 thread, 3 wifi devices crashes some bulbs, the new 5-light thread/matter group also fairly reliably disconnects 2 to 5 lights.

Is it somewhat foreseeable when work on this may or will start?

jfietz avatar Aug 26 '25 01:08 jfietz

Ok some facts about groups, so anyone experimenting with it should be aware:

  • turning on and off (so pure on/off/toggle) should work for groups in all sort of combinations
  • Dimming or color changes on groups are a problem IF you have devices which are on a "pre 1.4" matter firmware and have another controller than just the Python Matter server paired (like Apple, Google, SmartThings, Alex, ...) because there devices tend to over-report changes which can easiely kill the Thread network!

And there are some more that become relevant when you go deeper, but all above sound like those

So basically for the two issue posts above please provide more details about your use cases and if they match with dimming or color change?

Apollon77 avatar Aug 26 '25 06:08 Apollon77

  • Dimming or color changes on groups are a problem IF you have devices which are on a "pre 1.4" matter firmware and have another controller than just the Python Matter server paired (like Apple, Google, SmartThings, Alex, ...) because there devices tend to over-report changes which can easiely kill the Thread network!

So is the inference from this statement for those that use Matter multi-admin that once (Apple, Google, Amazon, ....) move their Matter controllers to Matter 1.4 (which also specifies Thread 1.4), the severe over-reporting issue could be mitigated? Obviously, individual Switches/light firmware and each ecosystem’s actual implementation of the same Matter spec has differed in the past, so the proof will be in the execution.

Thankfully Inovelli is on the verge of releasing a Matter 1.4 compliant Firmware for their dimmers. Apple,Amazon and maybe SmartThings seem to be getting with the program soon on Matter/thread 1.4 for their controllers. Google remains here, there and nowhere all at once.

A-vesalius avatar Aug 26 '25 11:08 A-vesalius

move their Matter controllers to Matter 1.4

No, I talk about the light bulb devices that need to be Matter 1.4+ ... Only Home assistant and matter.js based controllers can also prevent the issue but only when used "alone" without any other ecosystem. On device side 9x% of the devices base on the official Matter C++ SDK and will so behave the same. Also the mitigation that ultimately prevents overreporting is part of the Matter spec since 1.4 (called "Quieter"), so any device which is 1.4+ is also tested against this.

And again: Innovelly as a Dimmer switch is also not the cause of the over reporting. It is all about "the devices that actually DO the dimming!

Apollon77 avatar Aug 27 '25 06:08 Apollon77

I see this and many other issues are now tagged with revisit-with-matter.js. Out of curiosity, what does this mean, i.e., how will python-matter-server relate to matter.js. I noticed not long ago that matter.js now supports groups...

joe-sydney avatar Sep 06 '25 03:09 joe-sydney

The python-matter server will be reimplemented soon using matter.js as the engine behind it. Additionally the python client library to access the websocket interface will be split out into an own repo and also updated to all changes, so it still can be used as a client.

Apollon77 avatar Sep 08 '25 10:09 Apollon77

The python-matter server will be reimplemented soon using matter.js as the engine behind it. Additionally the python client library to access the websocket interface will be split out into an own repo and also updated to all changes, so it still can be used as a client.

Thanks for letting me know!

joe-sydney avatar Sep 08 '25 10:09 joe-sydney