Matter Generic Switch not working on reset

Just starting out developing for Nordic Devices and I've got a very basic generic switch working on the nrf52840 DK. I've commissioned it with Apple Home Kit / Apple TV serving as the thread border router. I'm able to assign actions to the device in the Apple Home App and it's working fine. The problem comes when I unplug or reset the board; when the device restarts the actions do not work. Any help would be much appreciated!

Using: nrf Connect SDK 2.9.0

Here is my code from app_task:

/*
 * Copyright (c) 2021 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#include "app_task.h"

#include "app/matter_init.h"
#include "app/task_executor.h"
#include "board/board.h"
#include "lib/core/CHIPError.h"
#include "lib/support/CodeUtils.h"

#ifdef CONFIG_CHIP_OTA_REQUESTOR
#include "dfu/ota/ota_util.h"
#endif

#include <app/clusters/identify-server/identify-server.h>
#include <app/server/OnboardingCodesUtil.h>
#include <app/clusters/switch-server/switch-server.h>
 #include <app-common/zap-generated/attributes/Accessors.h>

#include <zephyr/logging/log.h>

LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);

using namespace ::chip;
using namespace ::chip::app;
using namespace ::chip::DeviceLayer;

namespace 
{
constexpr EndpointId kSwitchEndpointId = 1;

Identify sIdentify = { kSwitchEndpointId, AppTask::IdentifyStartHandler, AppTask::IdentifyStopHandler,
		Clusters::Identify::IdentifyTypeEnum::kVisibleIndicator };


#define APPLICATION_BUTTON_MASK DK_BTN2_MSK

}

void AppTask::IdentifyStartHandler(Identify *)
{
	Nrf::PostTask(
		[] { Nrf::GetBoard().GetLED(Nrf::DeviceLeds::LED2).Blink(Nrf::LedConsts::kIdentifyBlinkRate_ms); });
}

void AppTask::IdentifyStopHandler(Identify *)
{
	Nrf::PostTask([] { Nrf::GetBoard().GetLED(Nrf::DeviceLeds::LED2).Set(false); });
}

void AppTask::ButtonEventHandler(Nrf::ButtonState state, Nrf::ButtonMask hasChanged)
{
	uint8_t currentPosition = 1;
    // Check if the application button (e.g., DK_BTN2) has been released.
    // The button is considered released when its bit is cleared in the current state.
    if ((APPLICATION_BUTTON_MASK & hasChanged) && !(state & APPLICATION_BUTTON_MASK))
    {
		Clusters::SwitchServer::Instance().OnInitialPress(kSwitchEndpointId, currentPosition);

		DeviceLayer::SystemLayer().ScheduleLambda([currentPosition] {
			Clusters::Switch::Attributes::CurrentPosition::Set(kSwitchEndpointId, currentPosition);
		}); 
    }
}

CHIP_ERROR AppTask::Init()
{
	ReturnErrorOnFailure(Nrf::Matter::PrepareServer());

	if (!Nrf::GetBoard().Init(ButtonEventHandler)) {
		LOG_ERR("User interface initialization failed.");
		return CHIP_ERROR_INCORRECT_STATE;
	}

	ReturnErrorOnFailure(Nrf::Matter::RegisterEventHandler(Nrf::Board::DefaultMatterEventHandler, 0));

	return Nrf::Matter::StartServer();
}

CHIP_ERROR AppTask::StartApp()
{
	ReturnErrorOnFailure(Init());

	while (true) {
		Nrf::DispatchNextTask();
	}

	return CHIP_NO_ERROR;
}

When the board reboots this is what I'm seeing in the serial output:

*** Using nRF Connect SDK v2.9.0-7787b2649840 ***
*** Using Zephyr OS v3.7.99-1f8f3dc29142 ***
I: 52 [DL]BLE address: C2:10:93:DA:01:9B
I: 61 [DL]CHIP task running
I: Init CHIP stack
I: 101 [DL]OpenThread ifconfig up and thread start
I: 106 [DL]OpenThread started: OK
I: 112 [DL]Setting OpenThread device type to SLEEPY END DEVICE
D: Registered new test event: 0xffffffff00000000
D: Registered new test event: 0xffffffff10000000
I: 127 [SVR]Subscription persistence not supported
I: 132 [SVR]Server initializing...
I: 136 [TS]Last Known Good Time: 2025-03-23T13:20:03
I: 145 [FP]Fabric index 0x1 was retrieved from storage. Compressed FabricId 0xBF3EABE8BCEC00C9, FabricId 0x0000000027BEB782, NodeId 0x00000000D604F08D, VendorId 0x1349
I: 163 [FP]Fabric index 0x2 was retrieved from storage. Compressed FabricId 0xE5C463F8148D9844, FabricId 0x00000000BD2EB1F1, NodeId 0x00000000CB07E45D, VendorId 0x1384
I: 179 [DMG]AccessControl: initializing
I: 182 [DMG]Examples::AccessControlDelegate::Init
I: 187 [DMG]AccessControl: setting
I: 190 [DMG]DefaultAclStorage: initializing
I: 198 [DMG]DefaultAclStorage: 2 entries loaded
D: 203 [IN]UDP::Init bind&listen port=5540
E: 207 [IN]IPV6_PKTINFO failed: 109
D: 211 [IN]UDP::Init bound to port=5540
D: 221 [IN]BLEBase::Init - setting/overriding transport
D: 227 [IN]TransportMgr initialized
D: 237 [DL]Using Thread extended MAC for hostname.
I: 244 [ZCL]Using ZAP configuration...
I: 250 [DMG]AccessControlCluster: initializing
D: 254 [DL]Boot reason: 1
I: 256 [ZCL]Initiating Admin Commissioning cluster.
I: 261 [SVR]Fabric already commissioned. Disabling BLE advertisement
D: 267 [DL]CHIPoBLE advertising set to off
I: 271 [DIS]Updating services using commissioning mode 0
E: 276 [DIS]Failed to remove advertised services: 3
D: 281 [DL]Using Thread extended MAC for hostname.
I: 285 [DIS]Advertise operational node BF3EABE8BCEC00C9-00000000D604F08D
E: 292 [DIS]Failed to advertise operational node: 3
E: 296 [DIS]Failed to finalize service update: 3
I: 301 [IN]CASE Server enabling CASE session setups
D: 307 [IN]SecureSession[0x200036a8]: Allocated Type:2 LSID:57680
I: 334 [DL]SRP Client was started, detected server: fdfa:ccdd:eab3:0000:0000:00ff:fe00:fc11
I: 343 [SVR]Joining Multicast groups
I: 348 [SVR]Server Listening...
I: 351 [DL]Device Configuration:
I: 354 [DL] Serial Number:
I: 358 [DL] Vendor Id: 65521 (0xFFF1)
I: 361 [DL] Product Id: 32768 (0x8000)
I: 365 [DL] Product Name: not-specified
I: 369 [DL] Hardware Version: 0
I: 372 [DL] Setup Pin Code (0 for UNKNOWN/ERROR):
I: 377 [DL] Setup Discriminator (0xFFFF for UNKNOWN/ERROR): 3840 (0xF00)
I: 384 [DL] Manufacturing Date: 2022-01-01
I: 387 [DL] Device Type: 65535 (0xFFFF)
I: 391 [SVR]SetupQRCode: [MT:]
I: 396 [SVR]Copy/paste the below URL in a browser to see the QR Code:
I: 402 [SVR]project-chip.github.io/.../qrcode.html
I: 411 [SVR]Manual pairing code: []
E: 415 [DL]Long dispatch time: 351 ms, for event type 2
D: 421 [DL]OpenThread State Changed (Flags: 0x00001000)
D: 429 [DL]OpenThread State Changed (Flags: 0x00001000)
D: 437 [DL]OpenThread State Changed (Flags: 0x00001000)
D: 447 [DL]OpenThread State Changed (Flags: 0x00000001)
D: 452 [DL] Thread Unicast Addresses:
D: 456 [DL] fd13:a8f:dd50:0:a177:eab1:ae4b:c4f/64 valid preferred
D: 462 [DL] fdfa:ccdd:eab3::ff:fe00:f434/64 valid preferred rloc
D: 468 [DL] fdfa:ccdd:eab3:0:fbdd:3f43:dcc6:c032/64 valid preferred
D: 475 [DL] fe80::f4ca:be41:9ce7:dc6f/64 valid preferred
D: 484 [DL]OpenThread State Changed (Flags: 0x00000008)
D: 491 [DL]OpenThread State Changed (Flags: 0x01000000)
D: 499 [DL]OpenThread State Changed (Flags: 0x00000004)
D: 504 [DL] Device Role: CHILD
D: 509 [DL]OpenThread State Changed (Flags: 0x00000001)
D: 514 [DL] Thread Unicast Addresses:
D: 518 [DL] fd13:a8f:dd50:0:a177:eab1:ae4b:c4f/64 valid preferred
D: 525 [DL] fdfa:ccdd:eab3::ff:fe00:f434/64 valid preferred rloc
D: 531 [DL] fdfa:ccdd:eab3:0:fbdd:3f43:dcc6:c032/64 valid preferred
D: 538 [DL] fe80::f4ca:be41:9ce7:dc6f/64 valid preferred
D: 547 [DL]OpenThread State Changed (Flags: 0x00001000)
D: 557 [DL]OpenThread State Changed (Flags: 0x00001000)
D: 565 [DL]OpenThread State Changed (Flags: 0x00000020)
D: 573 [DL]OpenThread State Changed (Flags: 0x00000084)
D: 578 [DL] Device Role: CHILD
D: 581 [DL] Partition Id: 0x657B5209
D: 587 [DL]OpenThread State Changed (Flags: 0x00000200)
D: 594 [DL]OpenThread State Changed (Flags: 0x00000001)
D: 599 [DL] Thread Unicast Addresses:
D: 603 [DL] fd13:a8f:dd50:0:a177:eab1:ae4b:c4f/64 valid preferred
D: 609 [DL] fdfa:ccdd:eab3::ff:fe00:f434/64 valid preferred rloc
D: 616 [DL] fdfa:ccdd:eab3:0:fbdd:3f43:dcc6:c032/64 valid preferred
D: 622 [DL] fe80::f4ca:be41:9ce7:dc6f/64 valid preferred
D: 1070 [DL]SRP update succeeded
I: 1073 [SVR]Server initialization complete
I: 1077 [DIS]Updating services using commissioning mode 0
D: 1082 [DL]Using Thread extended MAC for hostname.
I: 1087 [DIS]Advertise operational node BF3EABE8BCEC00C9-00000000D604F08D
I: 1094 [DL]advertising srp service: BF3EABE8BCEC00C9-00000000D604F08D._matter._tcp
D: 1101 [DL]Using Thread extended MAC for hostname.
I: 1106 [DIS]Advertise operational node E5C463F8148D9844-00000000CB07E45D
I: 1113 [DL]advertising srp service: E5C463F8148D9844-00000000CB07E45D._matter._tcp
I: 1125 [SWU]Stopping the watchdog timer
I: 1129 [SWU]Starting the periodic query timer, timeout: 86400 seconds
D: 1658 [DL]SRP update succeeded
I: 7923 [ZCL]SwitchServer: OnInitialPress

Related