birthdays.yaml 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Callback script when anyone presses «Send SMS».
  2. script:
  3. birthday_wishes:
  4. mode: parallel
  5. sequence:
  6. - variables:
  7. blang: "{{ lang if lang is defined else none }}"
  8. bname: "{{ name|lower|title }}"
  9. bage: "{{ age|string + {1: 'st', 2: 'nd', 3: 'rd'}.get(4 if 10 <= age|int % 100 < 20 else age|int % 10, 'th') }}"
  10. - service: "{{ notify_device }}"
  11. data:
  12. message: command_activity
  13. data:
  14. intent_action: "android.intent.action.SENDTO"
  15. intent_uri: >-
  16. sms:{{ number }}?body={{-
  17. "🎼 «Hurra for deg som fyller ditt år, ja - deg vil jeg gratulere» 🎶 🎉 \n\n"
  18. }}{%- if blang == 'no_nb' -%}
  19. Gratulere med {{ age|default('') }}årsdagen, {{ bname }}! 🎂🎁 Håpe du får en fin dag 🥳
  20. {%- else -%}
  21. Happy {{ bage ~ ' ' if age else '' }}birthday, {{ bname }}! 🎂🎁 Hope'll get a beautiful day 🥳
  22. {%- endif -%}
  23. # Require: https://github.com/jmgiaever/home-assistant-public-config/blob/master/General/notify_device.yaml
  24. automation:
  25. - alias: "Birthday: Notify on birthday!"
  26. trigger:
  27. - platform: calendar
  28. event: start
  29. entity_id: calendar.ical_birthdays
  30. offset:
  31. hours: 10
  32. action:
  33. - variables:
  34. birthdays: >-
  35. {% set birthday = namespace(calendars=[]) %}
  36. {% for ical in integration_entities('ical') if
  37. ical.startswith('sensor.ical_birthdays_event') and not is_state(ical, 'unavailable') and
  38. state_attr(ical, 'start').strftime('%d%m%y') == now().strftime('%d%m%y')
  39. %}
  40. {% set birthday.calendars = birthday.calendars + [ ical ] %}
  41. {% endfor %}
  42. {{ birthday.calendars }}
  43. # Looks data in «Description» of the calendar
  44. # Phone = phonenumber
  45. # Born = birthyear
  46. # Lang = language
  47. rexp_phone: '(?:(?<=phone)|(?<=\d,)):?\s?(\+?\d[ \d]+\d)'
  48. rexp_born: '.*?born:?\s*([\d]{2,4})'
  49. rexp_lang: '.*?lang:?\s*([a-zA-Z\_]+)'
  50. langs:
  51. no_nb: '+47' # If no lang, check phone number and set `no_nb` as lang if necessary
  52. notify_devices: >-
  53. {{ state_attr('group.owners_notify_devices', 'entity_id')|list }}
  54. - repeat: # Loop thorugh all the birthdays
  55. for_each: "{{ birthdays }}"
  56. sequence:
  57. - variables:
  58. name: "{{ (states(repeat.item).split('-')[:-1]|join('-')|trim).split(' ')[0]|lower|title }}"
  59. details: '{{ state_attr(repeat.item, "description")|striptags }}'
  60. phones: >-
  61. {{ details|regex_findall(rexp_phone, ignorecase=true) }}
  62. born: >-
  63. {{ details|regex_findall_index(rexp_born, ignorecase=true) if
  64. details is match(rexp_born, ignorecase=true) else
  65. false }}
  66. lang: >-
  67. {% set lang = namespace(lang=false) %}
  68. {% set lang.lang = details|regex_findall_index(rexp_lang, ignorecase=true) if
  69. details is match(rexp_lang, ignorecase=true) else
  70. False
  71. %}
  72. {% if lang.lang is false %}
  73. {% for klang in langs.keys() %}
  74. {% for phone in phones if phone.startswith(langs[klang]) %}
  75. {% set lang.lang = klang %}
  76. {% endfor %}
  77. {% endfor %}
  78. {% endif %}
  79. {{ lang.lang if lang.lang else 'eng' }}
  80. age: >-
  81. {{ false if born is false else
  82. 100 + now().strftime('%y')|int - born if born <= 100 else
  83. now().strftime('%Y')|int - born|int
  84. }}
  85. data:
  86. title: "It is {{ name }}'s birthday. 🎁"
  87. message: >-
  88. {% if age is not false %}
  89. {{ name }} is turning {{ age }} today.
  90. {% else %}
  91. {{ name }} is getting one year older today.
  92. {% endif %}
  93. Send your congratulations 🎉 to let him/her
  94. know that you appreciate them. 🥳
  95. data:
  96. actions:
  97. - action: SEND_SMS
  98. title: Send SMS 💌
  99. - action: DISMISS
  100. title: Dismiss 💩
  101. color: '#ebb1bb'
  102. group: "birthday-notifications"
  103. channel: "Birthdays"
  104. importance: high
  105. icon_url: /local/icons/notifications/cake.png
  106. notification_icon: "mdi:cake"
  107. - repeat: # Loop through all the devices that will be notified.
  108. for_each: "{{ notify_devices }}"
  109. sequence:
  110. service: script.turn_on
  111. target:
  112. entity_id: script.notify_device
  113. data:
  114. variables:
  115. notify_device: "{{ repeat.item }}"
  116. timeout: # The script waits 10 hours to send from midnight
  117. hours: 14 # Remove the notification when the date turn
  118. data: "{{ data }}"
  119. action_scripts: # Callback handles for «data.actions»
  120. SEND_SMS:
  121. script: script.birthday_wishes
  122. variables:
  123. lang: "{{ lang }}"
  124. name: "{{ name }}"
  125. age: "{{ age }}"
  126. notify_device: "{{ repeat.item }}"
  127. number: "{{ phones|join(',') }}"