From a4bd6b9d50c0051e4830adb70af946dd43475150 Mon Sep 17 00:00:00 2001 From: Jeffrey Stone Date: Sun, 6 Oct 2019 12:56:33 -0400 Subject: [PATCH] Python script powering the special event / countdown sensors --- python_scripts/special_events.py | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 python_scripts/special_events.py diff --git a/python_scripts/special_events.py b/python_scripts/special_events.py new file mode 100644 index 0000000..d9657db --- /dev/null +++ b/python_scripts/special_events.py @@ -0,0 +1,39 @@ +today = datetime.datetime.now().date() + +name = data.get('name') +type = data.get('type') +sensorName = "sensor.{}_{}".format(type , name.replace(" " , "_")) + +dateStr = data.get('date') +dateSplit = dateStr.split("/") + +dateDay = int(dateSplit[0]) +dateMonth = int(dateSplit[1]) +dateYear = int(dateSplit[2]) +date = datetime.date(dateYear,dateMonth,dateDay) + +thisYear = today.year +nextOccur = datetime.date(thisYear , dateMonth , dateDay) + +numberOfDays = 0 +years = int(thisYear) - dateYear + + +if today < nextOccur: + numberOfDays = (nextOccur - today).days + +elif today > nextOccur: + nextOccur = datetime.date(thisYear+1 , dateMonth , dateDay) + numberOfDays = int((nextOccur - today).days) + years = years+1 + + +hass.states.set(sensorName , numberOfDays , + { + "icon" : "mdi:calendar-star" , + "unit_of_measurement" : "days" , + "friendly_name" : "{}'s {}".format(name, type) , + "years" : years + } +) +