diff --git a/examples/config.sample.full.yml b/examples/config.sample.full.yml index 3dd6325..e913571 100644 --- a/examples/config.sample.full.yml +++ b/examples/config.sample.full.yml @@ -31,6 +31,7 @@ circuits: - Vägg vardagsrum - Vägg kök - Vägg arbetsrum + clamp: positive # Don't allow negative values # A IotaWatt main circuit, phase A/L1 - name: Main L1 type: main @@ -144,6 +145,7 @@ circuits: unit: 100 register: 866 type: int16 + clamp: positive # # Characteristics. Characteristics mean voltage and frequency, and potentially other non-power related readings. diff --git a/src/eachwatt.ts b/src/eachwatt.ts index e7c03ba..6428b68 100644 --- a/src/eachwatt.ts +++ b/src/eachwatt.ts @@ -53,10 +53,16 @@ const mainPollerFunc = async (config: Config) => { // Poll characteristics sensors const characteristicsSensorData = await pollCharacteristicsSensors(now, config.characteristics) - // Round all numbers to one decimal point + // Post-process power sensor data for (const data of powerSensorData) { if (data.power !== undefined) { + // Round all numbers to one decimal point data.power = Number(data.power.toFixed(1)) + + // Optionally clamp values + if (data.circuit.sensor.clamp === 'positive') { + data.power = Math.max(0, data.power) + } } } diff --git a/src/sensor.ts b/src/sensor.ts index 11d3551..084533c 100644 --- a/src/sensor.ts +++ b/src/sensor.ts @@ -36,6 +36,7 @@ export type CharacteristicsSensorPollFunction = ( export interface PowerSensor { type: SensorType pollFunc: PowerSensorPollFunction + clamp?: 'positive' } export interface CharacteristicsSensor { diff --git a/src/sensor/unmetered.ts b/src/sensor/unmetered.ts index f5cc58b..bae05fb 100644 --- a/src/sensor/unmetered.ts +++ b/src/sensor/unmetered.ts @@ -19,6 +19,6 @@ export const getSensorData: PowerSensorPollFunction = async ( return { timestamp, circuit, - power: Math.max(parentWatts - unmeteredWatts, 0), // Don't allow negative values + power: parentWatts - unmeteredWatts, } }