Birthday count down notify everyday below 20

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:

So these python_scripts appear to be stripped-down versions of mf_social’s comprehensive date_countdown.

@bhozar
Burningstone got it right.
That works in testing.

- alias: countdown_py_Notify
  initial_state: on
  trigger:
    - platform: state
      entity_id:
        - sensor.birthday_tina
        - sensor.birthday_adam
        - sensor.birthday_chloes
        - sensor.birthday_mum
  condition:
    condition: template
    value_template: "{{ trigger.to_state.state | int < 30 }}"
  action:
    # - service: notify.logfile2
    #   data_template:
    #     message: |-
    #       {%- for prop in trigger|sort if prop not in ['to_state', 'from_state'] -%}
    #         trigger.{{ prop }}: {{ trigger[prop] }}{{ '\n' }}
    #       {%- endfor -%}
          
    #       {%- for state in ['to_state', 'from_state'] if trigger[state] is defined -%}
    #         {{- '-' * 20 -}}{{ '\n' }}
    #         {%- for prop in ['state', 'entity_id', 'domain', 'object_id', 'name', 'last_updated', 'last_changed']|sort -%}
    #           trigger.{{ state }}.{{ prop }}: {{ trigger[state][prop] }}{{ '\n' }}
    #         {%- endfor -%}
    #         {%- for attr in trigger[state].attributes -%}
    #           trigger.{{ state }}.attributes.{{ attr }}: {{ trigger[state].attributes[attr] }}{{ '\n' }}
    #         {%- endfor -%}
    #       {%- endfor -%}
    - service: notify.logfile2
      data_template:
        message: "Don't forget {{ trigger.to_state.name }} in {{ trigger.to_state.state }} days. She ll be {{trigger.to_state.attributes.years | int +1 }} years old"

fyi, Thats the stuff you can work with

2020-08-07T17:23:43.727919+00:00 trigger.entity_id: sensor.birthday_mum
trigger.for: None
trigger.platform: state
--------------------
trigger.to_state.domain: sensor
trigger.to_state.entity_id: sensor.birthday_mum
trigger.to_state.last_changed: 2020-08-07 17:23:43.700812+00:00
trigger.to_state.last_updated: 2020-08-07 17:23:43.700812+00:00
trigger.to_state.name: Mum's Birthday
trigger.to_state.object_id: birthday_mum
trigger.to_state.state: 19
trigger.to_state.attributes.icon: mdi:calendar-star
trigger.to_state.attributes.unit_of_measurement: days
trigger.to_state.attributes.friendly_name: Mum's Birthday
trigger.to_state.attributes.nextoccur: 26/8/2020
trigger.to_state.attributes.birthday: 26/8/1950
trigger.to_state.attributes.years: 70
--------------------
trigger.from_state.domain: sensor
trigger.from_state.entity_id: sensor.birthday_mum
trigger.from_state.last_changed: 2020-08-07 17:23:03.154697+00:00
trigger.from_state.last_updated: 2020-08-07 17:23:03.154697+00:00
trigger.from_state.name: Mum's Birthday
trigger.from_state.object_id: birthday_mum
trigger.from_state.state: 18
trigger.from_state.attributes.icon: mdi:calendar-star
trigger.from_state.attributes.unit_of_measurement: days
trigger.from_state.attributes.friendly_name: Mum's Birthday
trigger.from_state.attributes.nextoccur: 25/8/2020
trigger.from_state.attributes.birthday: 25/8/1950
trigger.from_state.attributes.years: 70
2020-08-07T17:23:43.748680+00:00 Don't forget Mum's Birthday  in 19 days. She ll be 71 years old

lol, sry 123. wrong reply button

1 Like

@Burningstone and @123 Lol yes you are right my apologises forgot about the trigger time set to trigger:
trigger:
- platform: time
at: ‘00:00:01’

Changed that to test worked like a charm . Thank You both

@Burningstone and @123 been test this for a few days and it only triggers on the first sensor that is below 30 . So if i set tina and mum to be less then 30 days left I only get a message for Tina because she is first in the list. Any ideas?

1 Like

@Underknowledge Been test this for a few days and it only triggers on the first sensor that is below 30 . So if i set tina and mum to be less then 30 days left I only get a message for Tina because she is first in the list. Any ideas?. I see you started to write a solution ! did this work ?

When more than one entity is less than 30 days, and only the first entity is used for the notification, is there a warning message in the log (saying something about the fact that the automation’s action is already executing)?

If so, you want to try adding mode: queued to the automation.

Yes it says: Message Reminder Birthday: Already running 13:24:00 – Automation (WARNING). Hows the best way to add Queued Never used it before Thank!

So what’s happening is that the automation is triggered twice at virtually the same time. The first trigger for Tina executes the action but the second trigger for mum cannot because the action is busy performing the notification for tina. That’s because the default is mode: single.

By changing the mode to queued, mum’s notification will occur right after Tina’s notification is completed.

2 Likes

Cheers got it just tried it out seems to work. Thank you for that. Just reading about Queued and max number of runs . If i am reading this right I set this to the amount of birthdays I have . Thanks again

You can also try setting mode to parallel. This would attempt to notify Tina and mum concurrently (i.e. in parallel) as opposed to one after the other.

2 Likes