Help with birthday automation (optimize code)

I have the following automation which sends me a notification when a person has birthday:

  • id: birthday-notification
    alias: ‘Birthday Notification’
    trigger:
    platform: time
    at: ‘07:00:00’
    condition:
    • condition: template
      value_template: >-
      {% if states.birthday.NAME.state == “0” %}
      true
      {% endif %}
      action:
    • service: notify.telegram
      data:
      message: ‘INFO: Today is the birthday of NAME - Age: {{ states.birthday.NAME.attributes.age_at_next_birthday }}’

The code works without problems.

states.birthday.NAME.state == “0”
(0: zero days left == today)
(NAME: name of the birthday person, for example “tom”)

I can also get the name of the person by an attribute: states.birthday.NAME.attributes.friendly_name

Now, I have round about 20 persons in my birthday list and I dont want to write an automation for each person. Is there a way to handle it in one automation by setting a variable or something else? I dont want to have redundant code.

1 Like

Take a look at this automation and associated sensor. Works a treat. @CCOSTAN is the original source.

Automation with the notification

Sensor which checks the JSON file

This has worked well for me and it’s easy to add additional items to the list at any time.

2 Likes

Veeery nice & simple!

Thank you for the code. Very beautiful.
So, should I add all birthdays in a json file?

At the moment, all dates are in a yaml file (using component https://github.com/Miicroo/homeassistant-custom-components/tree/master/birthdays):

birthdays:

  • name: ‘Frodo Baggins’
    date_of_birth: 1921-09-22
  • name: ‘Bilbo Baggins’
    date_of_birth: 1843-09-22
  • name: Elvis
    date_of_birth: 1935-01-08
    icon: ‘mdi:music’

If you choose to use my method than yes. Its up to you.

I started to use this and it works very well for me.
A couple of questions. Is there a database of holidays that you used or a d/l file?
I have attempted to make a card in lovelace but am failing.
Any ideas on how to display a birthday and holiday on my main lovelace page.
Thanks
cwb

I started from someone else’s file and added from there. Most of the holidays I want are in the file. Till 2025 at least.

Thank you for the response.
Would you know or give me a hint on to displaying this in Lovelace?
BTW from your github page looks like you are going to be a father soon.
Congratulations.
cwb

You can add the sensor that is created just the same as any other sensor. On days where there is no birthday, you won’t have anything displayed.

If you are wanting more of a countdown type card, this may be up your alley.

Thanks! We have our second on the way.

Great, worked flawlessly the first time, which says a lot for me and my verrrry poor indentation.
Thanks all
cwb

I followed all the instructions. Created sensor, automation and json file. But sensor.holiday is showing value of unknown. I am running my HassIO under docker of Ubuntu OS VM. Please help

Can you post up your sensor config? and your .json file?


image

What does your error log say?

Im guessing that the file path may be causing the issue.

For hassio /config/data/days.json' works. This means in the main folder where your configuration.yaml file resides, create a data folder and place the days.json file in there.

@silvrr thanks for your help.

Issue was i need to keep it the path as /config/data/days.json and it worked. Now I can see sensor.value = Test Day

Great! Glad you go it working.

Hi all - I have had this up and running for awhile now thanks to @silvrr Github page. My question is can I use {% else %} in some way to have it display None on non holiday/birthday days? I been messing around and cant seem to get it to work. Thank you in advance!

post what you have that’s not working.

I want to do something like this

  - platform: command_line
    name: Holiday
    command: 'cat /config/data/days.json'
    value_template: >
      {% set today = now().month  ~ '/' ~ now().day  %}
      {% set holiday =  value_json.MAJOR_US.static[ today ] %}
      {% if holiday | trim == "" %}
        {% set today = now().month  ~ '/' ~ now().day ~ '/' ~ now().year %}
        {% set holiday =  value_json.MAJOR_US.dynamic[ today ] %}
      {% else %} None
      {% endif %}
      {{ holiday }}
    scan_interval: 14400

Everything but the

      {% else %} None

part is working. Working part of the code is from @silvrr.

try:

value_template: >
  {% set today = now().month  ~ '/' ~ now().day  %}
  {% set holiday =  value_json.MAJOR_US.static[ today ] %}
  {% if holiday | trim == "" %}
    {% set today = now().month  ~ '/' ~ now().day ~ '/' ~ now().year %}
    {% set holiday =  value_json.MAJOR_US.dynamic[ today ] %}
    {% if holiday | trim == "" %}
      {% set holiday =  None %}
    {% endif %}
  {% endif %}
  {{ holiday }}

You might be able to clean up the nested if’s but I have to go do something else right now and can’t do any more with it right now. But this will get you what you want…I think…