Skip to content

Commit

Permalink
add gauge metrics for cost/reward to support negative prices
Browse files Browse the repository at this point in the history
  • Loading branch information
terjesannum committed Sep 16, 2023
1 parent d67039d commit 947ce69
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Import the [dashboard](grafana/dashboard.json) or use id `16804` and import from
| tibber_power_consumption_previous_hour | Total power consumption previous hour | Gauge |
| tibber_power_consumption_previous_day | Total power consumption yesterday | Gauge |
| tibber_power_consumption_reactive | Current reactive consumption | Gauge |
| tibber_power_cost_day | Total power cost today | Gauge |
| tibber_power_cost_day_total | Total power cost today | Counter |
| tibber_power_cost_previous_hour | Total power cost previous hour | Gauge |
| tibber_power_cost_previous_day | Total power cost yesterday | Gauge |
Expand All @@ -139,6 +140,7 @@ Import the [dashboard](grafana/dashboard.json) or use id `16804` and import from
| tibber_power_production_day_total | Total power production today | Counter |
| tibber_power_production_previous_hour | Total power production previous hour | Gauge |
| tibber_power_production_previous_day | Total power production yesterday | Gauge |
| tibber_power_production_reward_day | Total power production reward today | Gauge |
| tibber_power_production_reward_day_total | Total power production reward today | Counter |
| tibber_power_production_reactive | Current net reactive production | Gauge |
| tibber_power_factor | Current power factor (active power / apparent power) | Gauge |
Expand Down
2 changes: 1 addition & 1 deletion charts/tibber-exporter/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apiVersion: v2
name: tibber-exporter
version: 3.2.0
version: 3.3.0
description: Tibber exporter
type: application
keywords:
Expand Down
8 changes: 8 additions & 0 deletions internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ type Measurements struct {
LiveMeasurement tibber.LiveMeasurement `graphql:"liveMeasurement(homeId: $id)"`
}

type GaugeValues struct {
CostToday float64
RewardToday *float64
}

type Home struct {
Id graphql.ID
Prices tibber.Prices `graphql:"home(id: $id)"`
PreviousHour tibber.PreviousPower
PreviousDay tibber.PreviousPower
Measurements Measurements
TimestampedValues tibber.TimestampedValues
GaugeValues GaugeValues
}

func New(id graphql.ID) *Home {
Expand Down Expand Up @@ -186,6 +192,7 @@ func (h *Home) SubscribeMeasurements(ctx context.Context, hc *http.Client, wsUrl
log.Printf("Accumulated cost lower than stored value: %f(%s) < %f(%s)\n",
m.LiveMeasurement.AccumulatedCost, m.LiveMeasurement.Timestamp, h.Measurements.LiveMeasurement.AccumulatedCost, h.Measurements.LiveMeasurement.Timestamp)
}
h.GaugeValues.CostToday = m.LiveMeasurement.AccumulatedCost
if m.LiveMeasurement.AccumulatedProduction >= h.Measurements.LiveMeasurement.AccumulatedProduction ||
m.LiveMeasurement.Timestamp.YearDay() != h.Measurements.LiveMeasurement.Timestamp.YearDay() {
h.Measurements.LiveMeasurement.AccumulatedProduction = m.LiveMeasurement.AccumulatedProduction
Expand All @@ -202,6 +209,7 @@ func (h *Home) SubscribeMeasurements(ctx context.Context, hc *http.Client, wsUrl
*m.LiveMeasurement.AccumulatedReward, m.LiveMeasurement.Timestamp, *h.Measurements.LiveMeasurement.AccumulatedReward, h.Measurements.LiveMeasurement.Timestamp)
}
}
h.GaugeValues.RewardToday = m.LiveMeasurement.AccumulatedReward
if m.LiveMeasurement.CurrentL1 != nil {
h.TimestampedValues.CurrentL1.Timestamp = m.LiveMeasurement.Timestamp
h.TimestampedValues.CurrentL1.Value = *m.LiveMeasurement.CurrentL1
Expand Down
38 changes: 37 additions & 1 deletion internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ const maxAge = 5
type MeasurementCollector struct {
measurements *tibber.LiveMeasurement
timestampedValues *tibber.TimestampedValues
gaugeValues *home.GaugeValues
consumption *prometheus.Desc
consumptionMin *prometheus.Desc
consumptionMax *prometheus.Desc
consumptionAvg *prometheus.Desc
consumptionTotal *prometheus.Desc
costToday *prometheus.Desc
costTotal *prometheus.Desc
current *prometheus.Desc
voltage *prometheus.Desc
Expand All @@ -60,16 +62,18 @@ type MeasurementCollector struct {
productionMin *prometheus.Desc
productionMax *prometheus.Desc
productionTotal *prometheus.Desc
rewardToday *prometheus.Desc
rewardTotal *prometheus.Desc
consumptionReactive *prometheus.Desc
productionReactive *prometheus.Desc
powerFactor *prometheus.Desc
}

func NewMeasurementCollector(homeId string, m *tibber.LiveMeasurement, tv *tibber.TimestampedValues) *MeasurementCollector {
func NewMeasurementCollector(homeId string, m *tibber.LiveMeasurement, tv *tibber.TimestampedValues, g *home.GaugeValues) *MeasurementCollector {
return &MeasurementCollector{
measurements: m,
timestampedValues: tv,
gaugeValues: g,
consumption: prometheus.NewDesc(
"tibber_power_consumption",
"Power consumption",
Expand Down Expand Up @@ -100,6 +104,12 @@ func NewMeasurementCollector(homeId string, m *tibber.LiveMeasurement, tv *tibbe
nil,
prometheus.Labels{"home_id": homeId},
),
costToday: prometheus.NewDesc(
"tibber_power_cost_day",
"Total power cost since midnight",
nil,
prometheus.Labels{"home_id": homeId},
),
costTotal: prometheus.NewDesc(
"tibber_power_cost_day_total",
"Total power cost since midnight",
Expand Down Expand Up @@ -148,6 +158,12 @@ func NewMeasurementCollector(homeId string, m *tibber.LiveMeasurement, tv *tibbe
nil,
prometheus.Labels{"home_id": homeId},
),
rewardToday: prometheus.NewDesc(
"tibber_power_production_reward_day",
"Total power production reward since midnight",
nil,
prometheus.Labels{"home_id": homeId},
),
rewardTotal: prometheus.NewDesc(
"tibber_power_production_reward_day_total",
"Total power production reward since midnight",
Expand Down Expand Up @@ -181,6 +197,7 @@ func (c *MeasurementCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.consumptionMax
ch <- c.consumptionAvg
ch <- c.consumptionTotal
ch <- c.costToday
ch <- c.costTotal
ch <- c.current
ch <- c.voltage
Expand All @@ -189,6 +206,7 @@ func (c *MeasurementCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.productionMin
ch <- c.productionMax
ch <- c.productionTotal
ch <- c.rewardToday
ch <- c.rewardTotal
ch <- c.consumptionReactive
ch <- c.productionReactive
Expand Down Expand Up @@ -241,6 +259,14 @@ func (c *MeasurementCollector) Collect(ch chan<- prometheus.Metric) {
c.measurements.AccumulatedConsumption,
),
)
ch <- prometheus.NewMetricWithTimestamp(
c.measurements.Timestamp,
prometheus.MustNewConstMetric(
c.costToday,
prometheus.GaugeValue,
c.gaugeValues.CostToday,
),
)
ch <- prometheus.NewMetricWithTimestamp(
c.measurements.Timestamp,
prometheus.MustNewConstMetric(
Expand Down Expand Up @@ -364,6 +390,16 @@ func (c *MeasurementCollector) Collect(ch chan<- prometheus.Metric) {
c.measurements.AccumulatedProduction,
),
)
if c.gaugeValues.RewardToday != nil {
ch <- prometheus.NewMetricWithTimestamp(
c.measurements.Timestamp,
prometheus.MustNewConstMetric(
c.rewardToday,
prometheus.GaugeValue,
*c.gaugeValues.RewardToday,
),
)
}
if c.measurements.AccumulatedReward != nil {
ch <- prometheus.NewMetricWithTimestamp(
c.measurements.Timestamp,
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func main() {
if (s.Features.RealTimeConsumptionEnabled || slices.Contains(liveMeasurements, string(s.Id))) && !slices.Contains(disableLiveMeasurements, string(s.Id)) {
log.Printf("Starting live measurements monitoring of home %v\n", s.Id)
go h.SubscribeMeasurements(ctx, hc, wsUrl, token)
prometheus.MustRegister(metrics.NewMeasurementCollector(string(s.Id), &h.Measurements.LiveMeasurement, &h.TimestampedValues))
prometheus.MustRegister(metrics.NewMeasurementCollector(string(s.Id), &h.Measurements.LiveMeasurement, &h.TimestampedValues, &h.GaugeValues))
started = append(started, string(s.Id))
} else {
log.Printf("Live measurements not available for home %v\n", s.Id)
Expand Down

0 comments on commit 947ce69

Please sign in to comment.