Skip to content

Commit

Permalink
Set daily measure to 0 if no previous day data
Browse files Browse the repository at this point in the history
  • Loading branch information
madchap committed Sep 2, 2023
1 parent 641066b commit 0b2ddb3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions apps/energiapro_gas_consumption/energiapro_gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
import pandas as pd
import tempfile
from datetime import datetime
from datetime import datetime, timedelta
import requests
from bs4 import BeautifulSoup
import re
Expand Down Expand Up @@ -63,6 +63,16 @@ def _post_daily_consumption():
headers = {"Authorization": token, "Content-Type": "application/json"}

last_daily_measure = df["QUANTITE EN M3"].iloc[-1]
last_daily_date = df["DATE"].iloc[-1]
last_data_date = datetime.strptime(last_daily_date, "%d/%m/%Y")

# if last measure is older than yesterday, zero it out
# Remember we process 1 day old data anyways
# naively account for time component with <2d
if not (datetime.now() - last_data_date < timedelta(days=2)):
self.log(f"Last measure is from {last_data_date}, so setting to 0.")
last_daily_measure = 0

daily_payload = {
"state": last_daily_measure,
"attributes": {
Expand All @@ -71,7 +81,7 @@ def _post_daily_consumption():
"state_class": "total",
},
}
r = requests.post(entity_url, json=daily_payload, headers=headers)
requests.post(entity_url, json=daily_payload, headers=headers)
self.log(f"POST'ed {last_daily_measure} to {entity_url}")

def _post_total_consumption():
Expand Down

0 comments on commit 0b2ddb3

Please sign in to comment.