Skip to content

Data gets duplicated 60x in influxdb? #11

Open
@doctarrwho

Description

@doctarrwho

After importing the data, I noticed that the raw data included the same values for each minute of the hour. See screenshot:
image

The data returned from SmartHub (for me) seems to be hourly, see sample debug output:

DEBUG: Parsed data from poll endpoint:
&{Status:COMPLETE Data:map[ELECTRIC:[{Type:USAGE Series:[{Name:336288847 Data:[{UnixMillis:1719532800000 Value:2.26} {UnixMillis:1719536400000 Value:2.24} {UnixMillis:1719540000000 Value:0.97} 

I modified the local code to change the WriteMetrics function to write one point per hour which fixed the problem for me, and now I have single row for each hour with hourly value.

func WriteMetrics(records []ElectricUsage, config InfluxConfig) error {
	opts := influxdb2.DefaultOptions()
	if config.Insecure {
		opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: true})
	}
	client := influxdb2.NewClientWithOptions(config.Host, config.AuthToken, opts)
	writeApi := client.WriteAPIBlocking(config.Org, config.Database)
	for _, record := range records {
		// Calculate the duration in hours, assuming the record spans an hour
		hours := record.EndTime.Sub(record.StartTime).Hours()
		if hours == 0 {
			hours = 1 // Avoid division by zero, assume at least one hour
		}

		// Create a point for the record
		point := influxdb2.NewPointWithMeasurement("electric").
			SetTime(record.StartTime).
			AddField("watts", float64(record.WattHours)/hours)

		if record.CostInCents != nil {
			point.AddField("cost", float64(*record.CostInCents)/hours)
		}
		if record.MeterName != nil {
			point.AddTag("name", *record.MeterName)
		}

		// Write the point to the database
		err := writeApi.WritePoint(context.Background(), point)
		if err != nil {
			return err
		}
	}
	return nil
}

I'm not sure if my utility provider returns different data vs. yours, but some customization here would be nice to control the point interval.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions