2121
2222MatterWaterHeater matter_water_heater;
2323
24+ const float COLD_WATER_TEMP = 20 .0f ; // Assumed incoming cold water temperature in Celsius
25+
26+ // Estimates the tank's remaining hot water level from the current and target temperatures,
27+ // assuming incoming cold water enters the tank at COLD_WATER_TEMP.
28+ void update_tank_percentage ()
29+ {
30+ float current_temperature = matter_water_heater.get_local_temperature ();
31+ float target_temperature = matter_water_heater.get_heating_setpoint ();
32+
33+ int tank_percentage = (int )(((current_temperature - COLD_WATER_TEMP ) / (target_temperature - COLD_WATER_TEMP )) * 100 .0f );
34+ if (tank_percentage < 0 ) {
35+ tank_percentage = 0 ;
36+ }
37+ if (tank_percentage > 100 ) {
38+ tank_percentage = 100 ;
39+ }
40+ matter_water_heater.set_tank_percentage (tank_percentage);
41+ }
42+
2443void setup ()
2544{
2645 pinMode (LED_BUILTIN , OUTPUT );
@@ -56,13 +75,14 @@ void setup()
5675 // Describe the physical tank - a single immersion element heating a 100 liter tank
5776 matter_water_heater.set_heater_types (MatterWaterHeater::heater_type_t ::IMMERSION_ELEMENT_1 );
5877 matter_water_heater.set_tank_volume (100 );
59- matter_water_heater.set_tank_percentage (100 );
6078
6179 // Set the measured water temperature to a fixed value
6280 matter_water_heater.set_local_temperature (45 .0f );
6381
6482 // Set the initial target water temperature
6583 matter_water_heater.set_heating_setpoint (55 .0f );
84+
85+ update_tank_percentage ();
6686}
6787
6888void loop ()
@@ -73,6 +93,7 @@ void loop()
7393 if (setpoint_prev != setpoint) {
7494 Serial.printf (" Water heater setpoint: %.01f C\n " , matter_water_heater.get_heating_setpoint ());
7595 setpoint_prev = setpoint;
96+ update_tank_percentage ();
7697 }
7798
7899 // Print the current mode if it changes
0 commit comments