birthdays.yaml 5.9 KB

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