A python script for Homeassistant that counts down the days to birthdays, anniversaries etc

Overview

Date Countdown

A python script for Homeassistant that counts down the days to birthdays, anniversaries etc

hacs_badge

Important note

I no longer use homeassistant so if this script stops being compatible with homeassistant that will be the end of the line. That said, it hasn't needed any maintenence in over a year and was working well so it will probably be fine to use for a long while yet.

How it works

This script creates a sensor that a counts down to the next occurrence of a date, like a birthday or anniversary and gives the number of years as an attribute.

You can optionally reverse the counter so it counts up from the last occurrence of a date, such as when you quit smoking.

Requires python_script: to be enabled in your configuration

Installation

Copy the Python script in to your /config/python_scripts directory or install via HACS.

Script arguments

key required type description
name: True string Name of the date (eg. John)
type: True string Type of date (eg. Birthday)
date: True string Date, in format DD/MM/YYYY
friendly_name: False string Display name of the sensor
append_year: False boolean Appends the number of years to the friendly name
icon: False string Icon of the sensor, defaults to 'mdi:calendar-star'
reverse: False boolean Reverses the sensor to count up instead of down. (Defaults to False)

The date can be in the future if you want to countdown to the date itself, and then the anniversaries thereafter.

Usage

Each sensor requires:

name: NAME_OF_DATE
type: TYPE_OF_DATE
date: DD/MM/YYYY_OF DATE

You can also customize the sensor icon and friendly names :

icon: "mdi:ICON_OF_DATE"
friendly_name: FRIENDLY_NAME_OF_DATE
append_year: True

And you can reverse the sensor so it counts up from a date:

reverse: True

examples:

name: John
type: birthday
date: 17/08/1971

or

name: Our wedding
type: anniversary
date: 14/02/1994
icon: "mdi:ring"
append_year: True

or

name: Quit Smoking
type: celebration
date: 01/10/2008
reverse: True

Generated sensors

Each sensor is given the following automatically:

entity_id: sensor.<type>_<name>
friendly_name: <name> <type>
state: <Days to the date from today>
nextoccur: <Date of next occurance>
years: <Number of years it will be>

So, the three sensors we created above would come out as:

sensor.birthday_john
friendly_name: John’s birthday
state: However many days it is until 17th August
nextoccur: 17/08/YYYY (either this year or next year as appropriate)
years: However old John will be on his next birthday

sensor.anniversary_our_wedding
friendly_name: Our wedding anniversary (20)
state: However many days to 14th February
nextoccur: 14/02/YYYY (either this year or next year as appropriate)
years: How many years you will have been married on that day

sensor.celebration_quit_smoking
friendly_name: Quit smoking celebration
state: However many days SINCE 1st October
nextOccur: 01/10/YYYY (either this year or next year as appropriate)
years: How many years SINCE you quit smoking

Note that if the type is 'birthday' the sensor will automatically add an apostrophe.

Example configuration.yaml entry

An example automation to create and refresh the above three sensors daily would be:

automation:
  - alias: Reminder - Refresh date countdown sensors
    trigger:
      - platform: time
        at: '00:00:01'
      - platform: homeassistant
        event: start
    action:
      - service: python_script.date_countdown
        data:
          name: John
          type: birthday
          date: 17/08/1971
      - service: python_script.date_countdown
        data:
          name: Our wedding
          type: anniversary
          date: 14/02/1994
          icon: "mdi:ring"
          append_year: True
      - service: python_script.date_countdown
        data:
          name: Quit smoking
          type: celebration
          date: 01/10/2008
          reverse: True

Example automation

An example automation to remind you of an event 7 days before it occurs would be:

automation:
  - alias: Reminder - John's birthday is coming up
    trigger:
      - platform: state
        entity_id: sensor.birthday_john
        to: '7'
    action:
      - wait_template: "{{ states('sensor.time') == '10:00' }}"
      - service: notify.notify
        data:
          message: "John's birthday is only a week away!"

Example Lovelace representation

Utilising the attributes provided and the custom lovelace card for adding secondary info to an entity row.

type: entities
show_header_toggle: false
title: Our Events
entities:
  - entity: sensor.anniversary_our_wedding
    secondary_info: '[[ {entity}.attributes.nextoccur ]]  ( [[ {entity}.attributes.years ]] Years )'
    type: 'custom:secondaryinfo-entity-row'

Will provide the following lovelace representation:

Lovelace example

Credits

Thanks to @myle for the idea for the extra attributes and the lovelace presentation.

Thanks to AJax2012 for the PR that caters for future dates, and makes the apostrophes in the friendly names only append to birthday sensors.

Thanks to Cyrosy for adding customization options and fixing some bugs.

Comments
  • Æ/Ø/Å breaks sensors

    Æ/Ø/Å breaks sensors

    Thanks for a awesome script.

    When using Danish unique letters (Æ, Ø & Å) the sensor breaks. I'm not sure if there exists a official way to handle that issue regarding writing codes, but Home Assistant transform them to: æ: ae ø: o å: aa

    Config

    ---
    # https://github.com/mf-social/ps-date-countdown
    alias: "[System] Date Countdown"
    trigger:
      - platform: time
        at: '00:00:01'
      - platform: homeassistant
        event: start
    action:
      - service: python_script.date_countdown
        data:
          name: Juleaften
          type: anniversary
          date: 24/12/1981
          icon: "mdi:pine-tree"
      - service: python_script.date_countdown
        data:
          name: 'Nytårsaften'
          type: anniversary
          date: 31/12/1981
          icon: "mdi:pine-tree"
    

    Log

    Log Details (ERROR)
    Logger: homeassistant.components.python_script.date_countdown.py
    Source: core.py:736
    Integration: Python Scripts (documentation, issues)
    First occurred: 4:01:51 PM (1 occurrences)
    Last logged: 4:01:51 PM
    
    Error executing script: Invalid entity id encountered: sensor.anniversary_nytårsaften. Format should be <domain>.<object_id>
    Traceback (most recent call last):
      File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 205, in execute
        exec(compiled.code, restricted_globals)
      File "date_countdown.py", line 50, in <module>
      File "/usr/src/homeassistant/homeassistant/core.py", line 954, in set
        context,
      File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 435, in result
        return self.__get_result()
      File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
        raise self._exception
      File "/usr/src/homeassistant/homeassistant/util/async_.py", line 53, in run_callback
        future.set_result(callback(*args))
      File "/usr/src/homeassistant/homeassistant/core.py", line 994, in async_set
        state = State(entity_id, new_state, attributes, last_changed, None, context)
      File "/usr/src/homeassistant/homeassistant/core.py", line 736, in __init__
        f"Invalid entity id encountered: {entity_id}. "
    homeassistant.exceptions.InvalidEntityFormatError: Invalid entity id encountered: sensor.anniversary_nytårsaften. Format should be <domain>.<object_id>
    

    I'm aware of the use of friendly_name, but many names in Denmark fx have special letters.

    opened by allanpersson 18
  • Negative number of years

    Negative number of years

    Hi there,

    since last update for some reason the "years" attribute became a negative value. image

    I have checked now the configuration and documentation to see if there was something new I wasn't aware of but it doesn't look like.

    The config it's fairly simple, the only difference is that I am using the secret file to store the dates:

      - service: python_script.date_countdown
        data:
          name: Andrea
          type: birthday
          date: !secret andrea_birthday
    

    Any clue?

    Thanks :) Andrea

    opened by SeLLeRoNe 12
  • Please provide more detailed example

    Please provide more detailed example

    It’s not clear to me exactly how I define the birthdays... I see from the readme which fields you expect but what’s not clear to me is where I define these... Do they go in the configuration.yaml ?

    opened by johntdyer 12
  • Error python 3.9.0

    Error python 3.9.0

    HA 0.116.4 with python 3.9.0 Reminder - Refresh date countdown sensors: Error executing script. Unexpected error for call_service at pos 1: Unknown slice type: <ast.Constant object at 0x51578490> While executing automation automation.reminder_refresh_date_countdown_sensors

    opened by MARIOP69 7
  • Feature Request: Ability to add 'years' to sensors friendly_name

    Feature Request: Ability to add 'years' to sensors friendly_name

    Hello, I think it would be great to allow appending 'years' to a sensors friendly_name, so for example... John's Birthday - 122 days would become... John's Birthday (50) - 122 days This way it's easier to spot milestone events

    Cheers, James

    opened by jimbob1001 6
  • Small Updates

    Small Updates

    Just made some small updates:

    • If the date of event is more than 1 year in the future (like 2021 if current year is 2019), use the event date to calculate distance to event.
    • add apostrophe to the friendly name of the sensor only if the event type is birthday. easy to change if desired.
    opened by AJax2012 2
  • Example automation is confusing...

    Example automation is confusing...

    Having

          - platform: time
            at: '00:00:01'
    

    In the example automation means the sensor isn't created until that time. Best to remove that and have the sensor created on HA restart. Helpful for noobs like me.

    opened by rv-kip 1
  • fix: past events won't get negative year anymore

    fix: past events won't get negative year anymore

    With this commit, years will never be negative again.

    • If the date is in the past, the diffrence between the next occurence year and the initial year is the number of years
    • If the date is in the future, it has never happened so years will always be 0

    Should fix #8

    opened by cyrosy 0
Releases(v6.0)
Owner
Marc Forth
Marc Forth
Turn your Raspberry Pi Pico into a USB Rubber Ducky

pico-ducky Turn your Raspberry Pi Pico into a USB Rubber Ducky Install Requirements CircuitPython for the Raspberry Pi Pico adafruit-circuitpython-bun

Konstantinos 5 Nov 08, 2022
Volta: A Virtual Assistant which increases your productivity with time as you use it…

Volta Official Documentation Overview & Purpose Volta: A Virtual Assistant which increases your productivity with time as you use it… Volta, developed

Abeer Joshi 1 Jan 14, 2022
A simple Python script for toggling Philips Hue Lights by clapping

LightsClap A simple Python script for toggling Philips Hue Lights by clapping Usage pip3 install -r requirements.txt python3 main.py and press the Ent

Flux Industries 2 Nov 16, 2021
A script for performing OTA update over BLE on ESP32

A script for performing OTA update over BLE on ESP32

Felix Biego 18 Dec 15, 2022
A DUCO (Duino-Coin) miner for GigaDevice ARM boards.

GD32 Duino-Coin Miner Description Contains the firmware and miner software for mining DUCO (Duino-Coin) on GigaDevice GD32 chips. Supported boards GD3

Maximilian Gerhardt 2 Feb 20, 2022
a library for using WS2812b leds (aka neopixels) with Raspberry Pi Pico

pico_ws2812b a library for using WS2812b leds (aka neopixels) with Raspberry Pi Pico You'll first need to save the ws2812b.py file to your device (for

76 Nov 25, 2022
AERO 421: Spacecraft Attitude, Dynamics, and Control Final Project.

AERO - 421 Final Project Redevelopment Spacecraft Attitude, Dynamics, and Control: Simulation to determine and control a satellite's attitude in LEO.

Gagandeep Thapar 3 Dec 16, 2022
Play a song with a 3D printer.

MIDI to GCODE Play a song with a FDM 3D printer. SLA printers don't have motors, so they cannot play music. Warning: Be ready to turn off the 3D print

Patrick 6 Apr 11, 2022
Fener ROS2 package version 2

Fener's ROS2 codes that runs on the vehicle. This node contains basic sensing and actuation nodes for vehicle control. Also example applications will be added.

Muhammed Sezer 1 Jan 18, 2022
Hardware: CTWingSKIT_BC28 Development Toolkit

IoT Portal Monitor Tools hardware: CTWingSKIT_BC28 Development Toolkit serial port driver: ST-LINK hardware development environment: Keli 5 MDK IoT pl

Fengming Zhang 1 Nov 07, 2021
Robo Arm :: Rigging is a rigging addon for Blender that helps animating industrial robotic arms.

Robo Arm :: Rigging Robo Arm :: Rigging is a rigging addon for Blender that helps animating industrial robotic arms. It construct serial links(a kind

2 Nov 18, 2021
A python library written for the raspberry pi.

A python package for using certain components on the raspberry pi.

Builder212 1 Nov 09, 2021
Estimation of whether or not the persons given information will have diabetes.

Diabetes Business Problem : It is desired to develop a machine learning model that can predict whether people have diabetes when their characteristics

Barış TOKATLIOĞLU 0 Jan 20, 2022
Simples Keylogger para Windows com um autoboot implementado no sistema

MKW Keylogger Keylogger simples para Windos com um autoboot implementado no sistema, o malware irá capturar pressionamentos de tecla e armazená-lo em

3 Jul 03, 2021
Designed a system that can efficiently sort recyclables and transfer them to corresponding bins using Python, a Raspberry Pi, and Quanser Labs.

System for Sorting and Recycling Containers - Project 3 Table of contents Overview The challenge Screenshot My process Built with Code snippets What I

Mit Patel 2 Dec 02, 2022
Raspberry Pi Power Button - Wake/Power Off/Restart(Double Press)

Control Raspberry pi with physically attached button. Wake, Power Off, and Restart (Double Press) . Python3 script runs as a service with easy installation.

Stas Yakobov 16 Oct 22, 2022
raspberry pi servo control using pca9685

RPi_servo-control_pca9685 raspberry pi 180° servo control using pca9685 Requirements Requires you to have the adafruit servokit library installed You

1 Jan 10, 2022
Sticklog2heatmap - Draw a heatmap of RC sticks from OpenTX logs or USB HID device

sticklog2heatmap Draw a heatmap of RC sticks from OpenTX logs or USB HID device

2 Feb 02, 2022
This tool emulates an EMV-CAP device, to illustrate the article "Banque en ligne : à la decouverte d'EMV-CAP" published in MISC

About This tool emulates an EMV-CAP device, to illustrate the article "Banque en ligne : à la decouverte d'EMV-CAP" published in MISC, issue #56 and f

Philippe Teuwen 28 Nov 21, 2022
Modi2-firmware-updater - MODI+ Firmware Updater With Python

MODI+ Firmware Updater 실행 준비 python3(파이썬3.9 혹은 그 이상의 버전)를 컴퓨터에 설치 python3 -m pip

LUXROBO 1 Feb 04, 2022