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
Example code and projects for FeatherS2 and FeatherS2 Neo

FeatherS2 & FeatherS2 Neo This repo is a collection of code, firmware, and files

Unexpected Maker 5 Jan 01, 2023
A flexible data historian based on InfluxDB, Grafana, MQTT and more. Free, open, simple.

Kotori Telemetry data acquisition and sensor networks for humans. Documentation: https://getkotori.org/ Source Code: https://github.com/daq-tools/koto

83 Nov 26, 2022
🐱 Petkit feeder components for HomeAssistant

Petkit for HomeAssistant Installing Download and copy custom_components/xiaomi_miot folder to custom_components folder in your HomeAssistant config fo

62 Dec 29, 2022
A script and GUI for controlling stepper motors from an arduino

A script and GUI for controlling stepper motors from an arduino (nema 23 in my case but should work for others in general)

Pip 2 Aug 01, 2022
Robot Framework keyword library wrapper for atlassian-python-api

Robot Framework keyword library wrapper for atlassian-python-api

Marcin Koperski 3 Jul 29, 2022
Home Assistant custom component to help ev-chargers stay below peak hourly energy levels.

Peaqev ev-charging Peaqev ev-charging is an attempt of charging an ev without breaching a preset monthly max-peak energy level. In order for this inte

Magnus Eldén 35 Dec 24, 2022
A Python class for controlling the Pimoroni RGB Keypad for Raspberry Pi Pico

rgbkeypad A Python class for controlling the Pimoroni RGB Keypad for the Raspberry Pi Pico. Compatible with MicroPython and CircuitPython. keypad = RG

Martin O'Hanlon 43 Nov 11, 2022
Custom component for MPC-HC for home-assistant

mpc_hc The current mpchc integration in homeassistant violates ADR0004, so it will be deleted from core. This is just the existing integration copied

3 Dec 15, 2022
Get the AltAz coordinates for a given object using astropy and output on a OLED screen.

Star Coordinates Get the AltAz coordinates for a given object using astropy and output on a OLED screen. As a very very newcomer to the astronomy scen

Craig Cmehil 1 Jan 31, 2022
Example for Calculating Robot Dynamics Using Pinocchio Library

A Example for Calculating Robot Dynamics Using Pinocchio Library Developed by: Xinyang Tian. Platform: Linux + Pinocchio. In this work, i use Pinocchi

Rot_Tianers 33 Dec 28, 2022
CPU benchmark by calculating Pi, powered by Python3

cpu-benchmark Info: CPU benchmark by calculating Pi, powered by Python 3. Algorithm The program calculates pi with an accuracy of 10,000 decimal place

Alex Dedyura 20 Jan 03, 2023
Water quality integration for Home Assistant with data provided by Budapest FVM

Water Quality FVM (Budapest, HU) custom integration for Home Assistant This custom component integrates water quality information provided by Budapest

Atticus Maximus 3 Dec 23, 2021
Windhager myComfort custom component for Home Assistant

Windhager myComfort custom component for Home Assistant

5 Apr 27, 2022
Aqara Camera G3 integration for Home Assistant

Aqara Camera G3 integration for Home Assistant ATTENTION: The component only works after enabled telnet. Only supportd stream. Not support still image

14 Dec 18, 2022
Monitor Live USB Plug In & Plug Out Events

I/O - Live USB Monitoring Author: Jonathan Scott @jonathandata1 Date: 3/13/2021 CURRENT VERSION 1.0 This is just a simple bash script that calls a pyt

Jonathan Scott 17 Dec 03, 2022
Home assiatant Custom component: Camera Archiver

Camera archiver Archive your ftp camera meadia files on other ftp with files renaming and event creation. Event can be used for send information to el

1 Jan 06, 2022
Connect a TeslaMate instance to Home Assistant, using MQTT

TeslaBuddy Connect a TeslaMate instance to Home Assistant, using MQTT. It allows basic control of your Tesla vehicle via Home Assistant (currently, ju

4 May 23, 2022
Blender Camera Switcher

Blender Camera Switcher A simple camera switcher addon for blender. Useful when use reference image for camera. This addon will automatically fix the

Corgice 1 Jan 31, 2022
Rasberry Pie GPIO memory game. Press the corresponding key to the lit LED.

RPie-keyboard-game Rasberry Pie GPIO memory game. Press the corresponding key to the lit LED. Randem LED (general output) is lit up on rasberrypi rand

Shawn Dowling 1 Oct 24, 2021
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