Skip to content

Commit

Permalink
Merge pull request #9 from uphillbattle/utc_time
Browse files Browse the repository at this point in the history
Request data from API in UTC
  • Loading branch information
uphillbattle authored Jan 10, 2023
2 parents 8f19268 + 1a7564d commit ccf53da
Showing 1 changed file with 57 additions and 48 deletions.
105 changes: 57 additions & 48 deletions nettleie_elvia.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ class NettleieElvia(hass.Hass):
def initialize(self):
self.log_progress = (self.args["log_progress"])
self.set_request_data()
self.set_correction_data()
self.run_in(self.hourly_call, 1)


def hourly_call(self, kwargs):
self.set_times()
self.fetch_data(self.hourly_call, 120)
self.set_correction()
self.set_states(self.hourly_call, 120)

self.next_call = self.next_hour_datetime.replace(second=5, microsecond=0, minute=0)
Expand All @@ -38,37 +36,44 @@ def set_request_data(self):
self.url_maxhours = "https://elvia.azure-api.net/customer/metervalues/api/v2/maxhours?meteringPointIds=" + self.args["meterid"]


def set_correction_data(self):
self.end_correction_period = datetime.datetime(2022, 4, 1)
self.correction_amount = (0.1669 - 0.0891)*1.25


def set_times(self):
localtime = time.localtime()
if localtime.tm_isdst > 0:
zoneadjust = "+02:00"
dtnow = datetime.datetime.now()
tnow = time.localtime()
dstnow = tnow.tm_isdst
if dstnow > 0:
zoneadjust = "+02:00"
else:
zoneadjust = "+01:00"
dtlast = datetime.datetime(dtnow.year, dtnow.month, dtnow.day, 0, 0, 0)
tlast = time.localtime(dtlast.timestamp())
dstlast = tlast.tm_isdst
if dstlast > 0:
dtlast = dtlast - datetime.timedelta(hours=2)
else:
dtlast = dtlast - datetime.timedelta(hours=1)
dt48 = dtnow + datetime.timedelta(days=2)
dtnext = datetime.datetime(dt48.year, dt48.month, dt48.day, 0, 0, 0)
tnext = time.localtime(dtnext.timestamp())
dstnext = tnext.tm_isdst
if dstnext > 0:
dtnext = dtnext - datetime.timedelta(hours=2)
else:
zoneadjust = "+01:00"
dtnext = dtnext - datetime.timedelta(hours=1)

self.current_datetime = datetime.datetime.now()
self.next_hour_datetime = self.current_datetime + datetime.timedelta(hours=1)
self.tomorrow_datetime = self.current_datetime + datetime.timedelta(hours=24)
self.next_day_datetime = self.current_datetime + datetime.timedelta(hours=48)
self.pretty_last_night = str(self.current_datetime.year) + "-" + str(self.current_datetime.month).zfill(2) + "-" + \
str(self.current_datetime.day).zfill(2) + "T" + "00:00:00" + zoneadjust
self.pretty_next_night = str(self.next_day_datetime.year) + "-" + str(self.next_day_datetime.month).zfill(2) + "-" + \
str(self.next_day_datetime.day).zfill(2) + "T" + "00:00:00" + zoneadjust
self.pretty_now = str(self.current_datetime.year) + "-" + str(self.current_datetime.month).zfill(2) + "-" + \
str(self.current_datetime.day).zfill(2) + "T" + str(self.current_datetime.hour).zfill(2) + ":" + \
str(self.current_datetime.minute).zfill(2) + ":" + str(self.current_datetime.second).zfill(2) + \
self.pretty_last_night = str(dtlast.year) + "-" + str(dtlast.month).zfill(2) + "-" + \
str(dtlast.day).zfill(2) + "T" + str(dtlast.hour).zfill(2) + ":00:00+00:00"
self.pretty_next_night = str(dtnext.year) + "-" + str(dtnext.month).zfill(2) + "-" + \
str(dtnext.day).zfill(2) + "T" + str(dtnext.hour).zfill(2) + ":00:00+00:00"
self.pretty_now = str(dtnow.year) + "-" + str(dtnow.month).zfill(2) + "-" + \
str(dtnow.day).zfill(2) + "T" + str(dtnow.hour).zfill(2) + ":" + \
str(dtnow.minute).zfill(2) + ":" + str(dtnow.second).zfill(2) + \
zoneadjust

self.body_tariff["startTime"] = self.pretty_last_night
self.body_tariff["endTime"] = self.pretty_next_night

self.current_hour = self.current_datetime.hour

self.todayString = str(self.current_datetime.year) + "-" + str(self.current_datetime.month).zfill(2) + "-" + \
str(self.current_datetime.day).zfill(2)
self.todayString = str(dtnow.year) + "-" + str(dtnow.month).zfill(2) + "-" + \
str(dtnow.day).zfill(2)


def fetch_data(self, retry_function, wait_period):
Expand All @@ -84,17 +89,6 @@ def fetch_data(self, retry_function, wait_period):
self.run_in(retry_function, wait_period)


def set_correction(self):
if (self.current_datetime < self.end_correction_period):
self.correction_today = self.correction_amount
else:
self.correction_today = 0.0
if (self.tomorrow_datetime < self.end_correction_period):
self.correction_tomorrow = self.correction_amount
else:
self.correction_tomorrow = 0.0


def set_states(self, retry_function, wait_period):
try:
self.tariff_response = json.loads(self.tariff_response_json.text)
Expand All @@ -112,10 +106,10 @@ def set_states(self, retry_function, wait_period):
endTime = element["expiredAt"]
value = element["energyPrice"]["total"]
if startTime[0:10] == self.todayString:
self.variable_price_per_hour_array_today_raw.append({"start": startTime, "end": endTime, "value": value - self.correction_today})
self.variable_price_per_hour_array_today.append(value - self.correction_today)
self.variable_price_per_hour_array_today_raw.append({"start": startTime, "end": endTime, "value": value})
self.variable_price_per_hour_array_today.append(value)
if ((self.pretty_now >= startTime) and (self.pretty_now < endTime)):
self.variable_price_per_hour = value - self.correction_today
self.variable_price_per_hour = value
forLoopBreak = False
fixedPriceLevelId = self.tariff_response["gridTariffCollections"][0]["meteringPointsAndPriceLevels"][0]["currentFixedPriceLevel"]["levelId"]
for fixedPriceElement in self.priceInfo["priceInfo"]["fixedPrices"]:
Expand All @@ -131,8 +125,8 @@ def set_states(self, retry_function, wait_period):
if (forLoopBreak == True):
break
else:
self.variable_price_per_hour_array_tomorrow_raw.append({"start": startTime, "end": endTime, "value": value - self.correction_tomorrow})
self.variable_price_per_hour_array_tomorrow.append(value - self.correction_tomorrow)
self.variable_price_per_hour_array_tomorrow_raw.append({"start": startTime, "end": endTime, "value": value})
self.variable_price_per_hour_array_tomorrow.append(value)

try:
self.maxhours_response = json.loads(self.maxhours_response_json.text)
Expand All @@ -142,8 +136,6 @@ def set_states(self, retry_function, wait_period):

self.maxhours_max_consumption_this_month = []
self.maxhours_max_consumption_last_month = []
self.maxhours_average_consumption_this_month = 0
self.maxhours_average_consumption_last_month = 0
for element in self.maxhours_response["meteringpoints"][0]["maxHoursAggregate"]:
if element["noOfMonthsBack"] == 0:
self.maxhours_average_consumption_this_month = element["averageValue"]
Expand All @@ -168,17 +160,35 @@ def set_states(self, retry_function, wait_period):
self.set_state(self.args["sensorname"] + '_kapasitetsledd_trinn',\
state=self.fixed_price_levelInfo, \
attributes={'friendly_name': self.args["sensoralias"] + ' kapasitetsledd trinn', \
'icon': 'mdi:stairs'})
'icon': 'mdi:stairs', \
'level_info': self.fixed_price_levelInfo, \
'monthly_total': self.fixed_price_per_month, \
'average_max_consumption_this_month': self.maxhours_average_consumption_this_month, \
'max_hourly_consumptions_this_month': self.maxhours_max_consumption_this_month, \
'average_max_consumption_last_month': self.maxhours_average_consumption_last_month, \
'max_hourly_consumptions_last_month': self.maxhours_max_consumption_last_month})
self.set_state(self.args["sensorname"] + '_kapasitetsledd_mnd',\
state=self.fixed_price_per_month, \
attributes={'friendly_name': self.args["sensoralias"] + ' kapasitetsledd per måned', \
'unit_of_measurement': 'NOK/mnd', \
'icon': 'mdi:currency-usd'})
'icon': 'mdi:currency-usd', \
'level_info': self.fixed_price_levelInfo, \
'monthly_total': self.fixed_price_per_month, \
'average_max_consumption_this_month': self.maxhours_average_consumption_this_month, \
'max_hourly_consumptions_this_month': self.maxhours_max_consumption_this_month, \
'average_max_consumption_last_month': self.maxhours_average_consumption_last_month, \
'max_hourly_consumptions_last_month': self.maxhours_max_consumption_last_month})
self.set_state(self.args["sensorname"] + '_effekt',\
state=self.maxhours_average_consumption_this_month, \
attributes={'friendly_name': self.args["sensoralias"] + ' kapasitetsleddsbestemmende effekt', \
'unit_of_measurement': 'kWh/h', \
'icon': 'mdi:lightning-bolt'})
'icon': 'mdi:lightning-bolt', \
'level_info': self.fixed_price_levelInfo, \
'monthly_total': self.fixed_price_per_month, \
'average_max_consumption_this_month': self.maxhours_average_consumption_this_month, \
'max_hourly_consumptions_this_month': self.maxhours_max_consumption_this_month, \
'average_max_consumption_last_month': self.maxhours_average_consumption_last_month, \
'max_hourly_consumptions_last_month': self.maxhours_max_consumption_last_month})

self.set_state(self.args["sensorname"] + '_forbruksledd', \
state=self.variable_price_per_hour, \
Expand All @@ -193,7 +203,6 @@ def set_states(self, retry_function, wait_period):

def output_log(self):
self.log("__function__: Time now = %s" % self.current_datetime, log="main_log", level="INFO")
self.log("__function__: Pretty time now = %s" % self.pretty_now, log="main_log", level="INFO")
self.log("__function__: Pretty Last night = %s" % self.pretty_last_night, log="main_log", level="INFO")
self.log("__function__: Pretty Next night = %s" % self.pretty_next_night, log="main_log", level="INFO")
self.log("__function__: kapasitetsledd (NOK/h)= %f" % self.fixed_price_per_hour, log="main_log", level="INFO")
Expand Down

0 comments on commit ccf53da

Please sign in to comment.