birthdays.yaml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. rexp_phone: '(?:(?<=phone)|(?<=\d,)):?\s?(\+?\d[ \d]+\d)'
  44. rexp_born: '.*?born:?\s*([\d]{2,4})'
  45. rexp_lang: '.*?lang:?\s*([a-zA-Z\_]+)'
  46. langs:
  47. no_nb: '+47'
  48. notify_devices: >-
  49. {{ state_attr('group.owners_notify_devices', 'entity_id')|list }}
  50. - repeat:
  51. for_each: "{{ birthdays }}"
  52. sequence:
  53. - variables:
  54. name: "{{ (states(repeat.item).split('-')[:-1]|join('-')|trim).split(' ')[0]|lower|title }}"
  55. details: '{{ state_attr(repeat.item, "description")|striptags }}'
  56. phones: >-
  57. {{ details|regex_findall(rexp_phone, ignorecase=true) }}
  58. born: >-
  59. {{ details|regex_findall_index(rexp_born, ignorecase=true) if
  60. details is match(rexp_born, ignorecase=true) else
  61. false }}
  62. lang: >-
  63. {% set lang = namespace(lang=false) %}
  64. {% set lang.lang = details|regex_findall_index(rexp_lang, ignorecase=true) if
  65. details is match(rexp_lang, ignorecase=true) else
  66. False
  67. %}
  68. {% if lang.lang is false %}
  69. {% for klang in langs.keys() %}
  70. {% for phone in phones if phone.startswith(langs[klang]) %}
  71. {% set lang.lang = klang %}
  72. {% endfor %}
  73. {% endfor %}
  74. {% endif %}
  75. {{ lang.lang if lang.lang else 'eng' }}
  76. age: >-
  77. {{ false if born is false else
  78. 100 + now().strftime('%y')|int - born if born <= 100 else
  79. now().strftime('%Y')|int - born|int
  80. }}
  81. index: "{{ repeat.index }}"
  82. data:
  83. title: "It is {{ name }}'s birthday. 🎁"
  84. message: >-
  85. {% if age is not false %}
  86. {{ name }} is turning {{ age }} today.
  87. {% else %}
  88. {{ name }} is getting one year older today.
  89. {% endif %}
  90. Send your congratulations 🎉 to let him/her
  91. know that you appreciate them. 🥳
  92. data:
  93. actions:
  94. - action: SEND_SMS
  95. title: Send SMS 💌
  96. - action: DISMISS
  97. title: Dismiss 💩
  98. color: '#ebb1bb'
  99. group: "birthday-notifications"
  100. channel: "Birthdays"
  101. importance: high
  102. icon_url: /local/icons/notifications/cake.png
  103. notification_icon: "mdi:cake"
  104. - repeat:
  105. for_each: "{{ notify_devices }}"
  106. sequence:
  107. service: script.turn_on
  108. target:
  109. entity_id: script.notify_device
  110. data:
  111. variables:
  112. notify_device: "{{ repeat.item }}"
  113. index: "{{ index }}-{{ repeat.index }}"
  114. timeout:
  115. hours: 14
  116. data: "{{ data }}"
  117. action_scripts: # Callback handles for «data.actions»
  118. SEND_SMS:
  119. script: script.birthday_wishes
  120. variables:
  121. lang: "{{ lang }}"
  122. name: "{{ name }}"
  123. age: "{{ age }}"
  124. notify_device: "{{ repeat.item }}"
  125. number: "{{ phones|join(',') }}"