Skip to content

Commit

Permalink
Add generic support for clamping sensor readings to positive values
Browse files Browse the repository at this point in the history
Closes #56
  • Loading branch information
Jalle19 committed May 10, 2024
1 parent b86486e commit 73375ff
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions examples/config.sample.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion src/eachwatt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/sensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type CharacteristicsSensorPollFunction = (
export interface PowerSensor {
type: SensorType
pollFunc: PowerSensorPollFunction
clamp?: 'positive'
}

export interface CharacteristicsSensor {
Expand Down
2 changes: 1 addition & 1 deletion src/sensor/unmetered.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

0 comments on commit 73375ff

Please sign in to comment.