MatterWaterValve
About
The MatterWaterValve class provides a water valve endpoint for Matter networks (device type 0x0042). This endpoint implements the Matter Valve Configuration and Control cluster as a code-driven CHIP server object.
Features:
Open indefinitely or for a timed duration (automatic close when the countdown elapses).
DefaultOpenDurationconfigured inbegin().ValveFaultreporting.onOpen()/onClose()user callbacks bridged through a clusterDelegate.Failed
onOpen()is rolled back after CHIPOpenValve()returns so the remaining-duration timer does not keep the valve Open.Local
open()/close()afterMatter.begin()(same paths as a Matter controller).Open/Close only (no Level/percent control; Open commands that carry a level are ignored).
Integration with Home Assistant, Apple Home, Amazon Alexa, and Google Home.
Matter standard compliance.
Use Cases:
Irrigation valves.
Main water shutoff valves.
Appliance supply valves.
API Reference
Constructor
MatterWaterValve
Creates a new Matter water valve endpoint.
MatterWaterValve();
Initialization
begin
Creates the endpoint and allocates the valve delegate. Call before Matter.begin().
bool begin(uint32_t defaultOpenDurationSeconds = 0);
defaultOpenDurationSeconds- Reported asDefaultOpenDurationwhen non-zero; used byopen()with no argument.0omitsDefaultOpenDurationandopen()(no argument) opens indefinitely.
This function will return true if successful, false otherwise.
Typical usage:
WaterValve.begin();
WaterValve.onOpen(onValveOpen);
WaterValve.onClose(onValveClose);
Matter.begin();
open() / close() require Matter.begin() to have run so the live ValveConfigurationAndControlCluster exists and the delegate is attached in onStackStarted().
end
Stops processing Matter water valve events and releases the valve delegate.
void end();
Valve States and Faults
ValveState_t
Valve state enumeration (ValveStateEnum):
VALVE_STATE_CLOSED- Valve is closed.VALVE_STATE_OPEN- Valve is open.VALVE_STATE_TRANSITIONING- Matter spec value; this library reportsCLOSEDorOPENvia the delegate (transitioning is not set locally).
ValveFault_t
Valve fault bitmap (combine with bitwise OR):
VALVE_FAULT_GENERAL_FAULTVALVE_FAULT_BLOCKEDVALVE_FAULT_LEAKINGVALVE_FAULT_NOT_CONNECTEDVALVE_FAULT_SHORT_CIRCUITVALVE_FAULT_CURRENT_EXCEEDED
Callbacks
onOpen
Sets a callback for when the valve is commanded open (Matter controller or local open()). Perform the physical open here; return false if it could not be completed. If no callback is registered, Matter state still moves to Open. The callback runs with the CHIP stack lock held—keep work short. Return false keeps the valve reported as closed. A failed Open is rolled back after CHIP OpenValve() returns (the remaining-duration timer is started after the delegate callback), which may invoke onClose() to align the cluster. Local open() does not CloseValve a second time when that rollback is already scheduled.
void onOpen(EndPointOpenCB onOpenCB);
The callback signature is:
bool onOpenCallback();
onClose
Sets a callback for when the valve is commanded closed (controller, local close(), end of a timed open, or rollback after a failed onOpen()). The callback has no return value; the CHIP delegate does not accept a close failure result.
void onClose(EndPointCloseCB onCloseCB);
The callback signature is:
void onCloseCallback();
Valve Control
open
open() opens using defaultOpenDurationSeconds from begin() (0 = open indefinitely). open(durationSeconds) opens for that many seconds; pass 0 to open indefinitely regardless of the default.
bool open();
bool open(uint32_t durationSeconds);
Requires Matter.begin() so the live cluster exists. This function will return true if successful, false otherwise (including when onOpen() returns false).
close
Closes the valve. Requires Matter.begin() so the live cluster exists (same as open()).
bool close();
This function will return true if successful, false otherwise.
setValveFault
Reports or clears fault bits (ValveFault_t). Pass 0 to clear. Call after WaterValve.begin(); may be called before Matter.begin() (cached and applied in onStackStarted()). While any fault bit is set, the cluster rejects Open commands (Matter controller and local open()).
bool setValveFault(uint16_t fault);
This function will return true if successful, false otherwise.
State Query
getCurrentState / isOpen
ValveState_t getCurrentState();
ValveState_t getTargetState();
bool isOpen();
getRemainingDuration
uint32_t getOpenDuration();
uint32_t getDefaultOpenDuration();
uint32_t getRemainingDuration();
uint16_t getValveFault();
Cached state is updated by the delegate (open/close paths and duration ticks).
Example
Water Valve
// Copyright 2025 Espressif Systems (Shanghai) PTE LTD
//
// 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.
// Matter Manager
#include <Arduino.h>
#include <Matter.h>
// List of Matter Endpoints for this Node
// Water Valve Endpoint
MatterWaterValve WaterValve;
// Wi-Fi credentials for this sketch. Fill these in when the board cannot
// commission over BLE (Arduino prebuild on ESP32 / ESP32-S2): the sketch
// joins the AP itself. When Matter commissions over BLE (CHIPoBLE), the
// hub sends SSID and password — leave the placeholders; they are unused.
#define WIFI_SSID "your-ssid"
#define WIFI_PASSWORD "your-password"
// set your board LED pin here
#ifdef LED_BUILTIN
const uint8_t ledPin = LED_BUILTIN;
#else
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
#warning "Do not forget to set the LED pin"
#endif
// set your board USER BUTTON pin here
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
MatterButton button;
// Open the valve for 10 seconds whenever it is commanded open - replace with your own irrigation/valve timing
const uint32_t openDurationSeconds = 10;
// Matter Protocol Endpoint Callbacks - replace with your real valve actuator control (relay, solenoid driver, etc.)
bool onValveOpen() {
Serial.println("User Callback :: Opening the water valve");
digitalWrite(ledPin, HIGH);
return true;
}
void onValveClose() {
Serial.println("User Callback :: Closing the water valve");
digitalWrite(ledPin, LOW);
}
void setup() {
button.begin(buttonPin);
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
// CONFIG_ENABLE_CHIPOBLE=n: sketch starts Wi-Fi here; with CHIPoBLE the hub delivers credentials.
#if !CONFIG_ENABLE_CHIPOBLE
matterConnectWiFi(WIFI_SSID, WIFI_PASSWORD);
#endif
// Starts closed. begin() default 0: open() with no arg is indefinite; this sketch uses open(openDurationSeconds) on button click.
WaterValve.begin();
WaterValve.onOpen(onValveOpen);
WaterValve.onClose(onValveClose);
// WaterValve.setValveFault(MatterWaterValve::VALVE_FAULT_LEAKING); // blocks open() until cleared with setValveFault(0)
matterSetExampleIdentity("WaterValve");
Matter.begin();
matterWaitUntilReady();
}
void loop() {
matterRestartIfNoFabric();
matterButtonEvent_t ev;
while ((ev = button.poll()) != MATTER_BUTTON_NONE) {
if (ev == MATTER_BUTTON_CLICK) {
if (WaterValve.isOpen()) {
Serial.println("User button released. Closing the water valve!");
WaterValve.close();
} else {
Serial.println("User button released. Opening the water valve!");
WaterValve.open(openDurationSeconds);
}
} else if (ev == MATTER_BUTTON_LONG_HOLD) {
Serial.println("Decommissioning the Water Valve Matter Accessory. It shall be commissioned again.");
WaterValve.close();
Matter.decommission();
}
}
static uint32_t remainingDurationPrev = 0;
uint32_t remainingDuration = WaterValve.getRemainingDuration();
if (remainingDurationPrev != remainingDuration) {
if (remainingDuration > 0) {
Serial.printf("Water valve remaining duration: %lu s\r\n", remainingDuration);
}
remainingDurationPrev = remainingDuration;
}
}