Skip to content

Commit

Permalink
TPLink/Tasmota: fix total energy (#2484)
Browse files Browse the repository at this point in the history
  • Loading branch information
thierolm authored Feb 4, 2022
1 parent b463488 commit 58b6e70
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
5 changes: 3 additions & 2 deletions charger/tasmota.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ func NewTasmota(uri, user, password string, standbypower float64) (*Tasmota, err
log := util.NewLogger("tasmota")
c := &Tasmota{
Helper: request.NewHelper(log),
uri: strings.TrimRight(uri, "/"),
uri: util.DefaultScheme(strings.TrimRight(uri, "/"), "http"),
user: user,
password: password,
standbypower: standbypower,
}

c.Client.Transport = request.NewTripper(log, transport.Insecure())

return c, nil
Expand Down Expand Up @@ -132,7 +133,7 @@ func (c *Tasmota) TotalEnergy() (float64, error) {
var resp tasmota.StatusSNSResponse
err := c.GetJSON(c.cmdUri("Status 8"), &resp)

return resp.StatusSNS.Energy.Today, err
return resp.StatusSNS.Energy.Total, err
}

// cmdUri creates the Tasmota command web request
Expand Down
14 changes: 6 additions & 8 deletions charger/tplink.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,18 @@ var _ api.MeterEnergy = (*TPLink)(nil)

// TotalEnergy implements the api.MeterEnergy interface
func (c *TPLink) TotalEnergy() (float64, error) {
var resp tplink.DayStatResponse
year, month, day := time.Now().Date()
cmd := fmt.Sprintf(`{"emeter":{"get_daystat":{"day":%v,"month":%v,"year":%v}}}`, day, int(month), year)
if err := c.execCmd(cmd, &resp); err != nil {
var resp tplink.EmeterResponse
if err := c.execCmd(`{"emeter":{"get_realtime":null}}`, &resp); err != nil {
return 0, err
}

if err := resp.Emeter.GetDaystat.ErrCode; err != 0 {
return 0, fmt.Errorf("get_daystat error %d", err)
if err := resp.Emeter.GetRealtime.ErrCode; err != 0 {
return 0, fmt.Errorf("get_realtime error %d", err)
}

energy := resp.Emeter.GetDaystat.DayList[len(resp.Emeter.GetDaystat.DayList)-1].EnergyWh / 1000
energy := resp.Emeter.GetRealtime.TotalWh / 1000
if energy == 0 {
energy = resp.Emeter.GetDaystat.DayList[len(resp.Emeter.GetDaystat.DayList)-1].Energy
energy = resp.Emeter.GetRealtime.Total
}

return energy, nil
Expand Down

0 comments on commit 58b6e70

Please sign in to comment.