birthdays.yaml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. # Uses: https://github.com/tybritten/ical-sensor-homeassistant
  25. automation:
  26. - id: "birthday_notify"
  27. alias: "Birthday: Notify on birthdays"
  28. trigger:
  29. - platform: calendar
  30. event: start
  31. entity_id: calendar.ical_birthdays
  32. offset:
  33. hours: 10
  34. action:
  35. - variables:
  36. birthdays: >-
  37. {% set birthday = namespace(calendars=[]) %}
  38. {% for ical in integration_entities('ical') if
  39. ical.startswith('sensor.ical_birthdays_event') and not is_state(ical, 'unavailable') and
  40. state_attr(ical, 'start').strftime('%d%m%y') == now().strftime('%d%m%y')
  41. %}
  42. {% set birthday.calendars = birthday.calendars + [ ical ] %}
  43. {% endfor %}
  44. {{ birthday.calendars }}
  45. # Looks in «Description» of the calendar
  46. # Phone = phonenumber
  47. # Born = birthyear
  48. # Lang = language
  49. rexp_phone: '(?:(?<=phone)|(?<=\d,)):?\s?(\+?\d[ \d]+\d)'
  50. rexp_born: '.*?born:?\s*([\d]{2,4})'
  51. rexp_lang: '.*?lang:?\s*([a-zA-Z\_]+)'
  52. langs:
  53. no_nb: '+47' # If no lang, check phone number and set `no_nb` as lang if necessary
  54. notify_devices: >- #joachims_notify_devices
  55. {{ state_attr('group.notify_user_devices', 'entity_id')|list }}
  56. - repeat: # Loop thorugh all the birthdays
  57. for_each: "{{ birthdays }}"
  58. sequence:
  59. - variables:
  60. name: "{{ (states(repeat.item).split('-')[:-1]|join('-')|trim).split(' ')[0]|lower|title }}"
  61. full_name: "{{ states(repeat.item).split('-')[:-1]|join('-')|trim }}"
  62. details: '{{ state_attr(repeat.item, "description")|striptags }}'
  63. phones: >-
  64. {{ details|regex_findall(rexp_phone, ignorecase=true) }}
  65. born: >-
  66. {{ details|regex_findall_index(rexp_born, ignorecase=true) if
  67. details is match(rexp_born, ignorecase=true) else
  68. false }}
  69. lang: >-
  70. {% set lang = namespace(lang=false) %}
  71. {% set lang.lang = details|regex_findall_index(rexp_lang, ignorecase=true) if
  72. details is match(rexp_lang, ignorecase=true) else
  73. False
  74. %}
  75. {% if lang.lang is false %}
  76. {% for klang in langs.keys() %}
  77. {% for phone in phones if phone.startswith(langs[klang]) %}
  78. {% set lang.lang = klang %}
  79. {% endfor %}
  80. {% endfor %}
  81. {% endif %}
  82. {{ lang.lang if lang.lang else 'eng' }}
  83. age: >-
  84. {{ false if born is false else
  85. 100 + now().strftime('%y')|int - born if born <= 100 else
  86. now().strftime('%Y')|int - born|int
  87. }}
  88. data: # Setup the notify data
  89. title: "It is {{ name }}'s birthday. 🎁"
  90. message: >-
  91. {% if age is not false %}
  92. {{ full_name }} is turning {{ age }} today.
  93. {% else %}
  94. {{ full_name }} is getting one year older today.
  95. {% endif %}
  96. Send your congratulations 🎉 to let him/her
  97. know that you appreciate them. 🥳
  98. data:
  99. actions:
  100. - action: SEND_SMS
  101. title: Send SMS 💌
  102. - action: DISMISS
  103. title: Dismiss 💩
  104. color: '#ebb1bb'
  105. persistent: true
  106. group: "birthday-notifications"
  107. tag: "birthday-notification-{{ full_name|regex_replace(find='([^a-z]+)', replace='-', ignorecase=true)|lower }}"
  108. channel: "Birthdays"
  109. importance: high
  110. icon_url: /local/icons/notifications/cake.png
  111. notification_icon: "mdi:cake"
  112. - repeat: # Loop through all the devices that will be notified.
  113. for_each: "{{ notify_devices }}"
  114. sequence:
  115. service: script.turn_on
  116. target:
  117. entity_id: script.notify_device
  118. data:
  119. variables:
  120. notify_device: "{{ repeat.item }}"
  121. timeout: # The script waits 10 hours to send from midnight
  122. hours: 14 # Remove the notification when the date turn
  123. data: "{{ data }}"
  124. action_scripts: # Callback handles for «data.actions»
  125. SEND_SMS:
  126. script: script.birthday_wishes
  127. variables:
  128. lang: "{{ lang }}"
  129. name: "{{ name }}"
  130. age: "{{ age }}"
  131. notify_device: "{{ repeat.item }}"
  132. number: "{{ phones|join(',') }}"