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
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
3d printable macropad

Pico Mpad A 3D printable macropad for automating frequently repeated actions. Hardware To build this project you need access to a 3d printer. The mode

Dmytro Panin 94 Jan 07, 2023
A simple program to make MSI Modern 15 speaker and microphone mute led work.

MSI Modern 15 sound led fixup for linux A simple program to fix the MSI Modern 15 speaker and microphone mute LEDs. Installation Requirements pulsectl

Seyed Danial Movahed 4 Oct 18, 2022
Adafruit IO connected smart thermostat based on CircuitPython.

Adafruit IO Thermostat Adafruit IO connected smart thermostat based on CircuitPython. Background and Motivation I have a 24 V Heat-only system with a

Shubham Chaudhary 1 Jan 18, 2022
Poupool is an overflow swimming pool control software

Poupool - The swimming pool controller Poupool is a swimming pool control software. It is based on Transitions, Pykka and Paho MQTT. The user interfac

Cyril Jaquier 8 Jul 18, 2022
Keystroke logging, often referred to as keylogging or keyboard capturing

Keystroke logging, often referred to as keylogging or keyboard capturing, is the action of recording the keys struck on a keyboard, typically covertly, so that a person using the keyboard is unaware

Bhumika R 2 Jan 11, 2022
Playing diabolo with two robot arms in ROS + Gazebo

Playing diabolo with robots This repository holds the ROS packages for playing diabolo with two UR5e robot arms on ROS Melodic (Ubuntu 18.04). Read ou

23 Dec 18, 2022
Automatically draw a KiCad schematic for a circuit prototyped on a breadboard.

Schematic-o-matic Schematic-o-matic automatically draws a KiCad schematic for a circuit prototyped on a breadboard. How It Works The first step in the

Nick Bild 22 Oct 11, 2022
DOS-like OS for RP2040 basic microcontroller boards

Micropython DOS-like OS for RP2040 microcontroller boards. Check out the demo video at https://www.youtube.com/watch?v=Az_oiq8GE4Y To start the OS typ

RetiredWizard 58 Dec 27, 2022
Uses the Duke Energy Gateway to import near real time energy usage into Home Assistant

Duke Energy Gateway This is a custom integration for Home Assistant. It pulls near-real-time energy usage from Duke Energy via the Duke Energy Gateway

Michael Meli 28 Dec 23, 2022
Pi-hole with Inky pHAT ePaper display

Pi-hole with Inky pHAT ePaper display This is my Pi-hole with an ePaper display.

11 Sep 13, 2022
Detic ros - A simple ROS wrapper for Detic instance segmentation using pre-trained dataset

Detic ros - A simple ROS wrapper for Detic instance segmentation using pre-trained dataset

Hirokazu Ishida 12 Nov 19, 2022
CO2Ampel - This RaspberryPi project uses weather data to estimate the share of renewable energy in the power grid

CO2Ampel This RaspberryPi project uses weather data to estimate the share of ren

Felix 4 Jan 19, 2022
Windhager myComfort custom component for Home Assistant

Windhager myComfort custom component for Home Assistant

5 Apr 27, 2022
Small Robot, with LIDAR and DepthCamera. Using ROS for Maping and Navigation

🤖 RoboCop 🤖 Small Robot, with LIDAR and DepthCamera. Using ROS for Maping and Navigation Made by Clemente Donoso, 📍 Chile 🇨🇱 RoboCop Lateral Fron

Clemente Donoso Krauss 2 Jan 04, 2022
Kwcpu - An unobtrusive CPU meter that fits in the default Windows 11 taskbar. Supports up to 32 cores.

kwcpu An unobtrusive CPU meter that fits in the default Windows 11 taskbar. Supports up to 32 cores. kwcpu is provided as a Rainmeter skin. By default

Jay Oster 2 Nov 07, 2022
Provide Unifi device info via api to Home Assistant that will give ap sensors

Unifi AP Device info Provide Unifi device info via api to Home Assistant that will give ap sensors

12 Jan 07, 2023
A simple non-official manager interface I'm using for my Raspberry Pis.

My Raspberry Pi Manager Overview I have two Raspberry Pi 4 Model B devices that I hooked up to my two TVs (one in my bedroom and the other in my new g

Christian Deacon 21 Jan 04, 2023
A custom mechanical keyboard inspired by the CFTKB Mysterium

Env-KB A custom mechanical keyboard inspired by the CFTKB Mysterium Build Guide and Parts List What is to do? Right now for the first 5 PCBs I have, i

EnviousData 203 Jan 04, 2023
BoneIO is a compact IO controller for home automation.

Project description BoneIO is a compact IO controller for home automation. Main features of this controller are Compact size (27x11x6)cm - 15 DIN modu

Maciej Krasuski 120 Nov 30, 2022