My kids challenged me to get a percentage progress of the year complete (similar to the twitter account @ProgressBar201X). It’s my first appdaemon sensor, but thought I’d share here.
import appdaemon.plugins.hass.hassapi as hass
from datetime import datetime, time, timedelta
#
# Simple sensor to capture the percentage of the year complete
#
class YearComplete(hass.Hass):
def initialize(self):
# update the sensor every day at 00:01
runtime = time(0, 1, 0)
self.run_daily(self.update_sensor, runtime)
# update immediately on launch
self.update_sensor()
def update_sensor(self, **kwargs):
# get the current year
y = datetime.now().year
# work out the last day of the year (go to 1/1/current_year + 1) and take a day off
ny = datetime(y+1, 1, 1) - timedelta(days=1)
num_days = ny.timetuple().tm_yday
# get current day of year
current_day = datetime.now().timetuple().tm_yday
day_percent = int(float(100.0 / num_days) * current_day)
# set sensor.year_complete
self.set_state('sensor.year_complete', state=day_percent, attributes={'unit_of_measurement':'%'})
self.log('Year is ' + str(day_percent) + '% complete')
Looks like this as a Lovelace entity, will change to a bar chart eventually!