You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
725 B
25 lines
725 B
// Package sensor reads Tasmota ENERGY.Power measurements in watts.
|
|
package sensor
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// ParsePower accepts numeric ENERGY.Power, including zero. Missing, null, or
|
|
// malformed measurements return an error so callers can preserve the last value.
|
|
func ParsePower(payload []byte) (float64, error) {
|
|
var message struct {
|
|
Energy *struct {
|
|
Power *float64 `json:"Power"`
|
|
} `json:"ENERGY"`
|
|
}
|
|
if err := json.Unmarshal(payload, &message); err != nil {
|
|
return 0, fmt.Errorf("decode sensor payload: %w", err)
|
|
}
|
|
if message.Energy == nil || message.Energy.Power == nil {
|
|
return 0, errors.New("sensor payload has no numeric ENERGY.Power")
|
|
}
|
|
return *message.Energy.Power, nil
|
|
}
|
|
|