diff --git a/meter/homeassistant.go b/meter/homeassistant.go index de884c4a3..5ee44d0e2 100644 --- a/meter/homeassistant.go +++ b/meter/homeassistant.go @@ -68,24 +68,24 @@ func NewHomeAssistantFromConfig(other map[string]any) (api.Meter, error) { } // phase currents (optional) - if phases, err := homeassistant.ValidatePhaseEntities(cc.Currents); len(phases) > 0 { - currentsG = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) } - } else if err != nil { + if phases, err := homeassistant.ValidatePhaseEntities(cc.Currents); err != nil { return nil, fmt.Errorf("currents: %w", err) + } else if len(phases) > 0 { + currentsG = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) } } // phase voltages (optional) - if phases, err := homeassistant.ValidatePhaseEntities(cc.Voltages); len(phases) > 0 { - voltagesG = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) } - } else if err != nil { + if phases, err := homeassistant.ValidatePhaseEntities(cc.Voltages); err != nil { return nil, fmt.Errorf("voltages: %w", err) + } else if len(phases) > 0 { + voltagesG = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) } } // phase powers (optional) - if phases, err := homeassistant.ValidatePhaseEntities(cc.Powers); len(phases) > 0 { - powersG = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) } - } else if err != nil { + if phases, err := homeassistant.ValidatePhaseEntities(cc.Powers); err != nil { return nil, fmt.Errorf("powers: %w", err) + } else if len(phases) > 0 { + powersG = func() (float64, float64, float64, error) { return conn.GetPhaseFloatStates(phases) } } if cc.Soc != "" { diff --git a/util/homeassistant/connection.go b/util/homeassistant/connection.go index 58a823317..484e7cf3b 100644 --- a/util/homeassistant/connection.go +++ b/util/homeassistant/connection.go @@ -103,6 +103,9 @@ func (c *Connection) GetIntState(entity string) (int64, error) { // GetFloatState retrieves the state of an entity as float64 func (c *Connection) GetFloatState(entity string) (float64, error) { + // leading minus sign? + entity, invert := strings.CutPrefix(entity, "-") + state, err := c.GetState(entity) if err != nil { return 0, err @@ -118,6 +121,10 @@ func (c *Connection) GetFloatState(entity string) (float64, error) { return 0, fmt.Errorf("%w for entity %s", err, entity) } + if invert { + value = -value + } + return scale * value, nil }