I dag er den Store Dagen, så i morges lagde jeg noen funksjoner for å følge med på snittet av de tre timene med høyest strømforbruk i inneværende måned:
@state_trigger('sensor.accumulated_consumption_current_hour < sensor.accumulated_consumption_current_hour.old')
def update_last_hour(**kwargs):
try: pyscript.electricity_consumption_last_hour = float(kwargs['old_value'])
except ValueError: pass # kwargs['old_value'] = 'unavailable' at HA startup, ignore value
@state_trigger('pyscript.electricity_consumption_last_hour')
def update_daily_peak():
new = float(pyscript.electricity_consumption_last_hour)
if new > float(pyscript.electricity_daily_peak):
pyscript.electricity_daily_peak = new
@time_trigger("cron(@daily)")
def reset_daily_peak():
pyscript.electricity_daily_peak = 0.0
pyscript.electricity_monthly_peak_average.today_used = False
pyscript.electricity_monthly_peak_average.today_used_value = 0.0
@state_trigger('pyscript.electricity_daily_peak')
def update_monthly_peak_avg():
new = float(pyscript.electricity_daily_peak)
top_3 = pyscript.electricity_monthly_peak_average.top_3_kwh
today_used = pyscript.electricity_monthly_peak_average.today_used
today_used_value = pyscript.electricity_monthly_peak_average.today_used_value
if new > top_3[-1]:
if not today_used:
top_3[-1] = new
else: # Update todays value
top_3[top_3.index(today_used_value)] = new
top_3.sort(reverse=True)
pyscript.electricity_monthly_peak_average.top_3_kwh = top_3
pyscript.electricity_monthly_peak_average.today_used_value = new
pyscript.electricity_monthly_peak_average.today_used = True
top_3_avg = sum(top_3)/count_nonzero(top_3) # Beware of div0, consider try/except
pyscript.electricity_monthly_peak_average = round(top_3_avg , 3)
@time_trigger("cron(@monthly)")
def reset_monthly_peak_avg():
pyscript.electricity_monthly_peak_average = 0.0
pyscript.electricity_monthly_peak_average.top_3_kwh = [0.0, 0.0, 0.0]
Gjenstår å se hvor den evt. feiler, men som et tips til alle som bruker Pyscript kan det anbefales å sette opp varsling for alle system_log_event med level: error fra Pyscript.