Repository: GladysAssistant/Gladys (2026-03-23T14:32:01Z)

Issue:
Zigbee2mqtt : Error on discover page when energy monitoring has a missing energy meter id
<img width="2912" height="770" alt="Image" src="https://github.com/user-attachments/assets/c7bc2939-f76d-44ac-b6db-8cd072e56632" />


Discussion:

Patch:
diff --git a/server/lib/energy-price/price.getDefaultElectricMeterFeatureId.js b/server/lib/energy-price/price.getDefaultElectricMeterFeatureId.js
index 3bacb0a950..c1efa938f4 100644
--- a/server/lib/energy-price/price.getDefaultElectricMeterFeatureId.js
+++ b/server/lib/energy-price/price.getDefaultElectricMeterFeatureId.js
@@ -18,8 +18,11 @@ async function getDefaultElectricMeterFeatureId() {
   }
 
   const device = this.stateManager.get('deviceById', energyPrice[0].electric_meter_device_id);
+  if (!device) {
+    return null;
+  }
   const feature = device.features.find((f) => f.category === DEVICE_FEATURE_CATEGORIES.ENERGY_SENSOR);
-  return feature.id;
+  return feature ? feature.id : null;
 }
 
 module.exports = { getDefaultElectricMeterFeatureId };


Test patch:
diff --git a/server/test/lib/energy-price/price.getDefaultElectricMeterFeatureId.test.js b/server/test/lib/energy-price/price.getDefaultElectricMeterFeatureId.test.js
index 4129044f0d..33ef6a94ed 100644
--- a/server/test/lib/energy-price/price.getDefaultElectricMeterFeatureId.test.js
+++ b/server/test/lib/energy-price/price.getDefaultElectricMeterFeatureId.test.js
@@ -61,4 +61,59 @@ describe('EnergyPrice.getDefaultElectricMeterFeatureId', () => {
     // Verify the result is null
     expect(featureId).to.equal(null);
   });
+
+  it('should return null when device does not exist in state manager', async () => {
+    // Create an energy price entry with an electric meter device ID
+    const electricMeterDeviceId = '7f85c2f8-86cc-4600-84db-6c074dadb4e8';
+    await db.EnergyPrice.create({
+      contract: 'base',
+      price_type: 'consumption',
+      currency: 'euro',
+      start_date: '2025-01-01',
+      price: 20,
+      electric_meter_device_id: electricMeterDeviceId,
+    });
+
+    // Don't set up the device in state manager (simulating deleted device)
+
+    // Call the function
+    const featureId = await energyPrice.getDefaultElectricMeterFeatureId();
+
+    // Verify the result is null
+    expect(featureId).to.equal(null);
+  });
+
+  it('should return null when device has no energy sensor feature', async () => {
+    // Create an energy price entry with an electric meter device ID
+    const electricMeterDeviceId = '7f85c2f8-86cc-4600-84db-6c074dadb4e8';
+    await db.EnergyPrice.create({
+      contract: 'base',
+      price_type: 'consumption',
+      currency: 'euro',
+      start_date: '2025-01-01',
+      price: 20,
+      electric_meter_device_id: electricMeterDeviceId,
+    });
+
+    // Set up the device in state manager without an energy sensor feature
+    const mockDevice = {
+      id: electricMeterDeviceId,
+      name: 'Electric Meter',
+      selector: 'electric-meter',
+      features: [
+        {
+          id: 'some-other-feature-id',
+          category: 'temperature-sensor',
+          type: 'decimal',
+        },
+      ],
+    };
+    stateManager.setState('deviceById', electricMeterDeviceId, mockDevice);
+
+    // Call the function
+    const featureId = await energyPrice.getDefaultElectricMeterFeatureId();
+
+    // Verify the result is null
+    expect(featureId).to.equal(null);
+  });
 });