Add data_age=0 as events requirement.
All checks were successful
Build Docker image / build (push) Successful in 1m36s
Build Golang packages / release (push) Has been skipped

This commit is contained in:
Pieter Hollander 2024-07-01 14:12:40 +02:00
parent 40c81c0bb3
commit 0e491612dd
Signed by: pieter
SSH key fingerprint: SHA256:HbX+9cBXsop9SuvL+mELd29sK+7DehFfdVweFVDtMSg

View file

@ -212,7 +212,7 @@ func handleMessage(message []byte, db *sql.DB) {
// Check conditions for recording data
for _, inverter := range liveData.Inverters {
// Record events data
if inverter.Events > 0 && inverter.Events > getPreviousEventsCount(db, inverter.Serial) {
if inverter.DataAge == 0 && inverter.Events > 0 && inverter.Events > getPreviousEventsCount(db, inverter.Serial) {
// Query the endpoint for events
events, err := queryEventsEndpoint(inverter.Serial)
if err != nil {
@ -445,6 +445,7 @@ func queryEventsEndpoint(inverterSerial string) (*EventsResponse, error) {
}
// The events counter reported by OpenDTU resets every day.
// To account for possible time drifts, the first and last 10 minutes of the day are excluded.
// Longest day NL: sun up 4:16, sun down 22:50
// Shortest day NL: sun up 8:44, sun down 16:25
func getPreviousEventsCount(db *sql.DB, inverterSerial string) int {
@ -453,8 +454,8 @@ func getPreviousEventsCount(db *sql.DB, inverterSerial string) int {
SELECT COUNT(*)
FROM opendtu_events
WHERE inverter_serial = $1
AND timestamp >= CURRENT_DATE + INTERVAL '9 hours'
AND timestamp < CURRENT_DATE + INTERVAL '23 hours'
AND timestamp >= CURRENT_DATE + INTERVAL '10 minutes'
AND timestamp < CURRENT_DATE + INTERVAL '23 hours 50 minutes'
`, inverterSerial).Scan(&count)
if err != nil && err != sql.ErrNoRows {
logger.Error("Error querying previous events count", "error", err)