Birthday count down notify everyday below 20

Can anyone help ! been trying to work out how to receive a notification everyday when below a number. I use the birthday reminder script. The automation below reminds me of who birthday it is when there are 20 days left. How do i get a notification every day below 20 ?

- alias: Reminder - Refresh date countdown sensors
  initial_state: on
  trigger:
    - platform: time
      at: '00:00:01'
    - platform: homeassistant
      event: start
  action:
    - service: python_script.date_countdown
      data:
        name: Tina
        type: Birthday
        date: 20/08/1971
    - service: python_script.date_countdown
      data:
        name: Adam
        type: Birthday
        date: 22/06/2003
    - service: python_script.date_countdown
      data:
        name: Chloe
        type: Birthday
        date: 25/06/2006
    - service: python_script.date_countdown
      data:
        name: Mum
        type: Birthday
        date: 10/05/1943
- alias: Message Reminder Birthday
  initial_state: on
  trigger:
    - platform: numeric_state
      entity_id:
         - sensor.birthday_tina
         - sensor.birthday_adam
         - sensor.birthday_chloes
         - sensor.birthday_mum
      below: 20
  action:
    - service: notify.mobile_app_iphone
      data_template:
        title: "Reminder"
        message: "Don't forget it's {{ trigger.to_state.name }} birthday in {{ trigger.to_state.state }}."

I think your trigger is incorrect, currently it triggers if all of the sensors are below 20, which is never the case. You should add one trigger per birthday you want to track.

Hi The triggers work fine (It triggers if only one is below 20 one message if 2 are below 20 i receive 2 notifications) when i restart home assistant. I have just changed below 20 to below 400 to make all birthdays within 400 . When i restart home assistant i get a notification for each person telling me how many day are left . The issue is how do I tell home assistant that when the day is one less then the day before then notify me

That means it doesn’t work as you want it to. If you set the value back to 20 and restart, you should get a notification for Tina only but you don’t get one right? Separate the triggers.

Yes i do get one for tina if she is the only one that is 20 days or less

This means it works on the first day it goes below 20 and then stops working?

Yes as you say it works on the first day then stops working. I am and trying to get it to send a message when 19, 18, 17 etc days left. I could set triggers up for below 20, below 19 , below 18 but i wounder if it was possible without setting individual triggers for each day .

Maybe you could try a template trigger for each birthday that checks if the sensor is below the value.

Could you post an example please

I found a better approach, but I hadn’t time to test it. Try this:

  trigger:
    platform: state
    entity_id:
      - sensor.birthday_tina
      - sensor.birthday_adam
      - sensor.birthday_chloes
      - sensor.birthday_mum
  condition:
    condition: template
    value_template: "{{ states(trigger.entity_id) < 20 }}"
  action:
    service: notify.mobile_app_iphone
    data_template:
      title: "Reminder"
      message: "Don't forget it's {{ trigger.to_state.name }} birthday in {{ trigger.to_state.state }}."

This triggers on any state change of any of the birthday sensors. The condition checks whether the triggering sensor is below 20 and if true the notification is sent.

1 Like

That’s due to how the Numeric State Trigger works. It triggers only when the value crosses the threshold and not afterwards. So it triggers when the value first crosses the threshold of 20 but not for subsequent lower values. To reset the trigger, the value must first rise above the threshold.

Burningstone’s example, where a condition performs the test, is better suited for what you want.

@Burningstone will give it a try thank you !

Need to change the message.

message: "Don't forget it's {{ trigger.to_state.name }} birthday in {{ trigger.to_state.state }}."

Does not pick up the {{ trigger.to_state.name }} or {{ trigger.to_state.state }}." seams to work other wise how do I get the person and how many days left in the message?

Thank Again

How are you triggering it to test?

Hi change the birthday date and waited until after 00.00 pm so Tina Days left went to 12 did not receive notification. If I remove {{ trigger.to_state.name }} and {{ trigger.to_state.state }} from the message it works.

I would realy like to see that python script @bhozar.
to find out wich triggers in a automation are you yould use something like this with a file notify
https://github.com/skalavala/mysmarthome/blob/0f735e39c74da9b911359cb600caee73cb137262/jinja_helpers/readme.md#6a-here-is-another-version-of-the-same-from-dale3h

  - service: notify.logfile
    data_template:
      message: |-
        {%- for prop in trigger|sort if prop not in ['to_...

otherwise, use an time trigger and template the *"§x out of it :smiley:

    {% if (states("sensor.birthday_tina")| float < 20)  %}
      Person1 Don't forget it's tinas birthday in {{ states("sensor.birthday_tina") }}.
    {% elif (states("sensor.birthday_adam")| float < 20)  %}
      Person2 Don't forget it's adams birthday in {{ states("sensor.birthday_adam") }}.
    {% elif (states("sensor.birthday_chloes")| float < 20)  %}
      Person3 ... 
    {% else %}
    {% endif %}

Downside, that ll only show you the first true if statement.

The issue with a message state for each sensor (i have over 30 reminders for car to birthdays, insurance etc. ) Looking to see if there is another way around it with out all the code. Thank anyway!

get me the python file and I see what I can do :wink:

Hmm, not sure, can’t test right now. Did you try separating the triggers and see if this works?

I am using 2 python script first one is for reminders that is not a year like birthdays

"""
Script to create a sensor displaying the number of days until some event.
Automate to update every day.
"""


name = data.get('name')
type = data.get('type')
sensorName = "sensor.{}_{}".format(type.replace(" " , "_") , name.replace(" " , "_"))

dateStr = data.get('date')
dateSplit = dateStr.split("/")

dateDay = int(dateSplit[0])
dateMonth = int(dateSplit[1])
dateYear =  int(dateSplit[2])
date = datetime.date(dateYear,dateMonth,dateDay)

day_of_interest = datetime.datetime(dateYear, dateMonth, dateDay)
now = datetime.datetime.now()
diff = day_of_interest - now

hass.states.set(sensorName , diff.days,{
    "icon" : "mdi:calendar-star" ,
    'unit_of_measurement': 'days',
    "friendly_name" : "{}'s {}".format(name.replace("_" , " "), type.replace("_" , " ")) ,
    "eventdate" : "{}/{}/{}".format(dateDay,dateMonth,dateYear) ,
})


The second for birthdays

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


"""       Requires python_script: to be enabled in you configuration       """


""" Usage:                                                                 """
"""                                                                        """
""" automation:                                                            """
"""   alias: Refresh John's birthday sensor                                """
"""   trigger:                                                             """
"""     platform: time                                                     """
"""     at: '00:00:01'                                                     """
"""   action:                                                              """
"""     service: python_script.date_countdown                              """
"""     data:                                                              """
"""       name: John                                                       """
"""       type: birthday                                                   """
"""       date: 17/08/1971   #DD/MM/YYYY                                   """


"""  This will create a sensor with entity_id sensor.birthday_john and a   """
"""  friendly name of 'John's birthday' .  The sensors value will be the   """
"""   number of days until the birthday, and the attribute 'years' will    """
"""          show how old John will be on his next birthday                """



today = datetime.datetime.now().date()

name = data.get('name')
type = data.get('type')
sensorName = "sensor.{}_{}".format(type.replace(" " , "_") , name.replace(" " , "_"))

dateStr = data.get('date')
dateSplit = dateStr.split("/")

dateDay = int(dateSplit[0])
dateMonth = int(dateSplit[1])
dateYear =  int(dateSplit[2])
date = datetime.date(dateYear,dateMonth,dateDay)

thisYear = today.year
nextOccur = datetime.date(thisYear , dateMonth , dateDay)
 

numberOfDays = 0
years = int(thisYear) - dateYear


if today < nextOccur:
  numberOfDays = (nextOccur - today).days

elif today > nextOccur:
  nextOccur = datetime.date(thisYear+1 , dateMonth , dateDay)
  numberOfDays = int((nextOccur - today).days)
  years = years+1


hass.states.set(sensorName , numberOfDays ,
  {
    "icon" : "mdi:calendar-star" ,
    "unit_of_measurement" : "days" ,
    "friendly_name" : "{}'s {}".format(name.replace("_" , " "), type.replace("_" , " ")) ,
    "nextoccur" : "{}/{}/{}".format(nextOccur.day,nextOccur.month,nextOccur.year) ,
    "birthday" : "{}/{}/{}".format(dateDay,dateMonth,dateYear) ,
    "years" : years
  }
)

Thanks :smiley: