import datetime
@time_trigger("cron(1 * * * *)")
def update_saving_hours():
today_prices = state.getattr(sensor.nordpool_kwh_oslo_nok_3_00_025).get("today")
hour_price_pairs = zip(range(24), today_prices)
hour_price_pairs = sorted(hour_price_pairs, key=lambda v: v[1])
most_expensive_today = {}
for i,(hour, price) in enumerate(hour_price_pairs):
most_expensive_today[hour] = i
tomorrow_prices = state.getattr(sensor.nordpool_kwh_oslo_nok_3_00_025).get("tomorrow")
hour_price_pairs = zip(range(24), tomorrow_prices)
hour_price_pairs = sorted(hour_price_pairs, key=lambda v: v[1])
most_expensive_tomorrow = {}
for i,(hour, price) in enumerate(hour_price_pairs):
most_expensive_tomorrow[hour] = i
now = datetime.datetime.now()
current_hour = now.hour
log.info("Ran power saving script at %s" % now)
for i in range(1,24):
least_n_expensive = [{v: most_expensive_today[v] < i} for v in range(0,24)]
least_n_expensive_tomorrow = [{v: most_expensive_tomorrow[v] < i} for v in range(0,24)]
state.set("sensor.powersaving_%s_least_expensive" %i, most_expensive_today[current_hour] < i, new_attributes={"today_prices": today_prices, "today": least_n_expensive, "tomorrow_prices": tomorrow_prices, "tomorrow": least_n_expensive_tomorrow})#, new_attributes={"hours": })
n_most_expensive = [{v: most_expensive_today[v] > 23-i} for v in range(0,24)]
n_most_expensive_tomorrow = [{v: most_expensive_tomorrow[v] > 23-i} for v in range(0,24)]
state.set("sensor.powersaving_%s_most_expensive" %i, most_expensive_today[current_hour] > 23-i, new_attributes={"today_prices": today_prices, "today": n_most_expensive, "tomorrow_prices": tomorrow_prices, "tomorrow": n_most_expensive_tomorrow})#, new_attributes={"hours:": map(lambda v: most_expensive[current_hour] >= i, range(0,24))})
Jeg syntes det ble litt tungvindt med template for hver time så jeg skrev dette snippet som kan brukes med pyscript (som finnes under HACS). Denne lager 23 sensorer med billigste og dyreste time. Husk å bytt ut "nordpool_kwh_oslo_nok_3_00_025" med din sensor.