2022-10-15 19:58:44 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HistoryResult [][]struct {
|
|
|
|
State string `json:"state"`
|
|
|
|
LastUpdated time.Time `json:"last_updated"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config Config) queryHistory(entityID string, startTime, endTime time.Time) (HistoryResult, error) {
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", config.HomeAssistant.BaseURL+
|
|
|
|
"/api/history/period/"+url.QueryEscape(startTime.Format(time.RFC3339))+
|
|
|
|
"?filter_entity_id="+entityID+
|
|
|
|
"&end_time="+url.QueryEscape(endTime.Format(time.RFC3339)), /*+
|
|
|
|
"&minimal_response",*/nil)
|
|
|
|
if err != nil {
|
|
|
|
return HistoryResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Authorization", "Bearer "+config.HomeAssistant.ApiKey)
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return HistoryResult{}, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return HistoryResult{}, errors.New("got a non-200 status code. Check the correctness of sensors IDs -" + resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return HistoryResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result HistoryResult
|
|
|
|
err = json.Unmarshal(body, &result)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type DayData struct {
|
|
|
|
DayNumber int
|
|
|
|
DayTime time.Time
|
|
|
|
Measurements int
|
|
|
|
Value float32
|
|
|
|
High float32
|
|
|
|
Low float32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config Config) historyAverageAndConvertToGreen(entityID string, startTime, endTime time.Time) ([]DayData, error) {
|
|
|
|
|
|
|
|
history, err := config.queryHistory(entityID, startTime, endTime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var days []DayData
|
|
|
|
|
|
|
|
for _, historyChange := range history[0] {
|
|
|
|
val, err := strconv.ParseFloat(historyChange.State, 32)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
value := float32(val)
|
|
|
|
var found bool
|
|
|
|
dayNo := historyChange.LastUpdated.Local().Day()
|
|
|
|
for key, day := range days {
|
|
|
|
if dayNo == day.DayNumber {
|
|
|
|
found = true
|
|
|
|
day.Value += value
|
|
|
|
day.Measurements++
|
|
|
|
days[key] = day
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
days = append(days, DayData{
|
|
|
|
DayNumber: dayNo,
|
|
|
|
DayTime: historyChange.LastUpdated.Local(),
|
|
|
|
Measurements: 1,
|
|
|
|
Value: value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, day := range days {
|
|
|
|
// by using 100 - value we get the percentage of green energy instead of the percentage of fossil-generated energy
|
|
|
|
day.Value = 100 - (day.Value / float32(day.Measurements))
|
|
|
|
days[key] = day
|
|
|
|
}
|
|
|
|
|
2022-12-04 12:50:11 +01:00
|
|
|
days = fillMissing(days, startTime, endTime)
|
2022-10-15 19:58:44 +02:00
|
|
|
|
|
|
|
return days, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config Config) historyDelta(entityID string, startTime, endTime time.Time) ([]DayData, error) {
|
|
|
|
|
|
|
|
history, err := config.queryHistory(entityID, startTime, endTime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var days []DayData
|
|
|
|
|
|
|
|
for _, historyChange := range history[0] {
|
|
|
|
if historyChange.State != "off" {
|
|
|
|
val, err := strconv.ParseFloat(historyChange.State, 32)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
value := float32(val)
|
|
|
|
var found bool
|
|
|
|
dayNo := historyChange.LastUpdated.Local().Day()
|
|
|
|
for key, day := range days {
|
|
|
|
if dayNo == day.DayNumber {
|
|
|
|
found = true
|
|
|
|
if value > day.High {
|
|
|
|
day.High = value
|
|
|
|
}
|
|
|
|
if value < day.Low || day.Low == 0 {
|
|
|
|
day.Low = value
|
|
|
|
}
|
|
|
|
days[key] = day
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
days = append(days, DayData{
|
|
|
|
DayNumber: dayNo,
|
|
|
|
DayTime: historyChange.LastUpdated.Local(),
|
|
|
|
Value: value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, day := range days {
|
|
|
|
day.Value = day.High - day.Low
|
|
|
|
days[key] = day
|
|
|
|
}
|
|
|
|
|
2022-12-04 12:50:11 +01:00
|
|
|
days = fillMissing(days, startTime, endTime)
|
2022-10-15 19:58:44 +02:00
|
|
|
|
|
|
|
return days, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-12-04 12:50:11 +01:00
|
|
|
func fillMissing(days []DayData, startTime, endTime time.Time) []DayData {
|
2022-10-15 19:58:44 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
previousDay time.Time
|
|
|
|
defaultDay time.Time
|
|
|
|
previousValue float32
|
|
|
|
ret []DayData
|
|
|
|
currentTime = time.Now()
|
|
|
|
)
|
|
|
|
|
2022-12-04 12:50:11 +01:00
|
|
|
expectedDaysDiff := int(math.Trunc(endTime.Sub(startTime).Hours()/24) + 1)
|
|
|
|
|
2022-10-15 19:58:44 +02:00
|
|
|
for key, day := range days {
|
|
|
|
|
|
|
|
if key != 0 {
|
|
|
|
|
|
|
|
if day.DayTime.Day() != previousDay.Add(24*time.Hour).Day() {
|
|
|
|
daysDiff := math.Trunc(day.DayTime.Sub(previousDay).Hours() / 24)
|
|
|
|
for i := 1; float64(i) < daysDiff; i++ {
|
|
|
|
fakeTime := previousDay.Add(time.Duration(24*i) * time.Hour)
|
|
|
|
ret = append(ret, DayData{
|
|
|
|
DayNumber: fakeTime.Day(),
|
|
|
|
DayTime: fakeTime,
|
|
|
|
Value: previousValue,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, day)
|
|
|
|
previousValue = day.Value
|
|
|
|
previousDay = day.DayTime
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// note that here previousDay is the last logged day
|
|
|
|
if previousDay == defaultDay {
|
|
|
|
return []DayData{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if previousDay.Day() != currentTime.Day() {
|
|
|
|
daysDiff := math.Trunc(currentTime.Sub(previousDay).Hours() / 24)
|
|
|
|
for i := 1; float64(i) < daysDiff; i++ {
|
|
|
|
fakeTime := previousDay.Add(time.Duration(24*i) * time.Hour)
|
|
|
|
ret = append(ret, DayData{
|
|
|
|
DayNumber: fakeTime.Day(),
|
|
|
|
DayTime: fakeTime,
|
|
|
|
Value: previousValue,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 12:50:11 +01:00
|
|
|
if len(ret) < expectedDaysDiff {
|
|
|
|
shouldAdd := expectedDaysDiff - len(ret)
|
2022-10-15 19:58:44 +02:00
|
|
|
startDay := currentTime.Add(-time.Duration(24*len(ret)) * time.Hour)
|
|
|
|
for i := 0; i < shouldAdd; i++ {
|
|
|
|
fakeTime := startDay.Add(-time.Duration(24*i) * time.Hour)
|
|
|
|
ret = append([]DayData{
|
|
|
|
{
|
|
|
|
DayNumber: fakeTime.Day(),
|
|
|
|
DayTime: fakeTime,
|
|
|
|
Value: 0,
|
|
|
|
},
|
|
|
|
}, ret...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 12:50:11 +01:00
|
|
|
if len(ret) != expectedDaysDiff {
|
2022-10-15 19:58:44 +02:00
|
|
|
// oh shit
|
|
|
|
log.Panicln("You've found a logic bug! Open a bug report ASAP.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
}
|