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
Ha-rpi gpio - Home Assistant Raspberry Pi GPIO Integration

Home Assistant Raspberry Pi GPIO custom integration This is a spin-off from the

Shay Levy 98 Dec 24, 2022
Smart Tech Automation Remote via Kinematics Gesture control for IoT devices

STARK Smart Tech Automation Remote via Kinematics Gesture control for IoT devices View Demo · Report Bug · Request Feature Table of Contents About The

Juseong (Joe) Kim 1 Jan 29, 2022
Point Density-Aware Voxels for LiDAR 3D Object Detection (CVPR 2022)

PDV PDV is LiDAR 3D object detection method. This repository is based off [OpenPCDet]. Point Density-Aware Voxels for LiDAR 3D Object Detection Jordan

Toronto Robotics and AI Laboratory 114 Dec 21, 2022
Control DJI Tello with Raspberry Pi and PS4 Controller

Control-DJI-Tello-with-Raspberry-Pi-and-PS4-Controller Demo of this project see

MohammadReza Sharifi 24 Aug 11, 2022
Open source home automation that puts local control and privacy first.

Home Assistant Open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiast

Home Assistant 57k Jan 01, 2023
Huawei Solar sensors for Home Assistant

Huawei Solar Sensors This integration splits out the various values that are fetched from your Huawei Solar inverter into separate HomeAssistant senso

Thijs Walcarius 151 Dec 31, 2022
Brogrammer-keyboard - FIrmware for the Brogrammer Keyboard v1.0

Brogrammer Keyboard Firmware The package contains the firmware that runs on the Brogrammer Keyboard v1.0 See https://imgur.com/a/oY5QZ14 This keyboard

Devin Hartleben 1 Apr 21, 2022
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
LifeSaver automatically, periodically saves USB flash drive data into the PC

LifeSaver automatically, periodically saves USB flash drive data into the PC. Theoriticaly it will work with any any connected drive ex - Hard Disk ,SSD ... But, can't handle Backing up multipatition

siddharth dhaka 4 Sep 26, 2021
Micro Displays for Raspberry Pi

micro-displays Micro Displays for Raspberry Pi Why? I'm super bored in lockdown. Add a Raspberry Pi 400 and a few tiny displays... The top half of the

ig 291 Jul 06, 2022
CircuitPython library for the CH559 USB to Serial chip

CH559 (USB to Serial) CircuitPython Library Why? Because you might want to get keyboard/mouse/gamepad/HID input into your CircuitPython projects witho

Guy Dupont 3 Nov 19, 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
This allows you to record keyboard and mouse input, and play it back using pynput.

Record and Play with Python! This allows you to record keyboard and mouse input, and play it back (with looping) using pynput. It allows for automatio

George Jensen 45 Jan 02, 2023
Control the lights of Alienware computers under GNU/Linux systems.

Before requesting support please consider that this software is not actively developed. I created it in 2014 for managing my Alienware M14X-R1 (where

rsm 111 Dec 05, 2022
Micropython-wifimanager-esp8266 - Simple Wifi Manager for ESP8266 using MicroPython

micropython-wifimanager-esp8266 Simple Wifi Manager for ESP8266 using MicroPytho

Abhinuv Nitin Pitale 1 Jan 04, 2022
Programming of Robotics Systems course at the University of Aveiro, Portugal, 2021-2022.

Programação de Sistemas Robóticos Miguel Riem Oliveira Universidade de Aveiro 2021-2022 Projeto AtlasCar Projecto RACE IROS 2014 AtlasCar2 ATOM IROS 2

Miguel Riem de Oliveira 22 Jul 13, 2022
Micropython automatic watering

micropython-automatic-watering micropython automatic watering his code was developed to be used with nodemcu esp8266, but can be modified to work with

1 Nov 24, 2021
Code for the onshape macropad.

Onshape_Macropad Code for the onshape macropad. This is a macropad built using the Pimoroni Keybow and the KPrepublic Enclosure. pimoroni_keybow kprep

Justin Cole 1 Nov 23, 2021
🏡 My Home Assistant Configs. Be sure to 🌟 my repo to follow the updates!

Home Assistant Configuration Here's my Home Assistant configuration. I have installed HA on a Lenovo ThinkCentre M93P Tiny with an Intel Dual-Core i5-

iLyas Bakouch 25 Dec 30, 2022
The ABR Control library is a python package for the control and path planning of robotic arms in real or simulated environments.

The ABR Control library is a python package for the control and path planning of robotic arms in real or simulated environments. ABR Control provides API's for the Mujoco, CoppeliaSim (formerly known

Applied Brain Research 277 Jan 05, 2023