123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- # Callback script when anyone presses «Send SMS».
- script:
- birthday_wishes:
- mode: parallel
- sequence:
- - variables:
- blang: "{{ lang if lang is defined else none }}"
- bname: "{{ name|lower|title }}"
- bage: "{{ age|string + {1: 'st', 2: 'nd', 3: 'rd'}.get(4 if 10 <= age|int % 100 < 20 else age|int % 10, 'th') }}"
-
- - service: "{{ notify_device }}"
- data:
- message: command_activity
- data:
- intent_action: "android.intent.action.SENDTO"
- intent_uri: >-
- sms:{{ number }}?body={{-
- "🎼 «Hurra for deg som fyller ditt år, ja - deg vil jeg gratulere» 🎶 🎉 \n\n"
- }}{%- if blang == 'no_nb' -%}
- Gratulere med {{ age|default('') }}årsdagen, {{ bname }}! 🎂🎁 Håpe du får en fin dag 🥳
- {%- else -%}
- Happy {{ bage ~ ' ' if age else '' }}birthday, {{ bname }}! 🎂🎁 Hope'll get a beautiful day 🥳
- {%- endif -%}
- # Require: https://github.com/jmgiaever/home-assistant-public-config/blob/master/General/notify_device.yaml
- # Uses: https://github.com/tybritten/ical-sensor-homeassistant
- automation:
- - id: "birthday_notify"
- alias: "Birthday: Notify on birthdays"
- trigger:
- - platform: calendar
- event: start
- entity_id: calendar.ical_birthdays
- offset:
- hours: 10
- action:
- - variables:
- birthdays: >-
- {% set birthday = namespace(calendars=[]) %}
- {% for ical in integration_entities('ical') if
- ical.startswith('sensor.ical_birthdays_event') and not is_state(ical, 'unavailable') and
- state_attr(ical, 'start').strftime('%d%m%y') == now().strftime('%d%m%y')
- %}
- {% set birthday.calendars = birthday.calendars + [ ical ] %}
- {% endfor %}
- {{ birthday.calendars }}
- # Looks in «Description» of the calendar
- # Phone = phonenumber
- # Born = birthyear
- # Lang = language
- rexp_phone: '(?:(?<=phone)|(?<=\d,)):?\s?(\+?\d[ \d]+\d)'
- rexp_born: '.*?born:?\s*([\d]{2,4})'
- rexp_lang: '.*?lang:?\s*([a-zA-Z\_]+)'
- langs:
- no_nb: '+47' # If no lang, check phone number and set `no_nb` as lang if necessary
- notify_devices: >- #joachims_notify_devices
- {{ state_attr('group.notify_user_devices', 'entity_id')|list }}
- - repeat: # Loop thorugh all the birthdays
- for_each: "{{ birthdays }}"
- sequence:
- - variables:
- name: "{{ (states(repeat.item).split('-')[:-1]|join('-')|trim).split(' ')[0]|lower|title }}"
- full_name: "{{ states(repeat.item).split('-')[:-1]|join('-')|trim }}"
- details: '{{ state_attr(repeat.item, "description")|striptags }}'
- phones: >-
- {{ details|regex_findall(rexp_phone, ignorecase=true) }}
- born: >-
- {{ details|regex_findall_index(rexp_born, ignorecase=true) if
- details is match(rexp_born, ignorecase=true) else
- false }}
- lang: >-
- {% set lang = namespace(lang=false) %}
- {% set lang.lang = details|regex_findall_index(rexp_lang, ignorecase=true) if
- details is match(rexp_lang, ignorecase=true) else
- False
- %}
- {% if lang.lang is false %}
- {% for klang in langs.keys() %}
- {% for phone in phones if phone.startswith(langs[klang]) %}
- {% set lang.lang = klang %}
- {% endfor %}
- {% endfor %}
- {% endif %}
- {{ lang.lang if lang.lang else 'eng' }}
- age: >-
- {{ false if born is false else
- 100 + now().strftime('%y')|int - born if born <= 100 else
- now().strftime('%Y')|int - born|int
- }}
- data: # Setup the notify data
- title: "It is {{ name }}'s birthday. 🎁"
- message: >-
- {% if age is not false %}
- {{ full_name }} is turning {{ age }} today.
- {% else %}
- {{ full_name }} is getting one year older today.
- {% endif %}
- Send your congratulations 🎉 to let him/her
- know that you appreciate them. 🥳
- data:
- actions:
- - action: SEND_SMS
- title: Send SMS 💌
- - action: DISMISS
- title: Dismiss 💩
- color: '#ebb1bb'
- persistent: true
- group: "birthday-notifications"
- tag: "birthday-notification-{{ full_name|regex_replace(find='([^a-z]+)', replace='-', ignorecase=true)|lower }}"
- channel: "Birthdays"
- importance: high
- icon_url: /local/icons/notifications/cake.png
- notification_icon: "mdi:cake"
- - repeat: # Loop through all the devices that will be notified.
- for_each: "{{ notify_devices }}"
- sequence:
- service: script.turn_on
- target:
- entity_id: script.notify_device
- data:
- variables:
- notify_device: "{{ repeat.item }}"
- timeout: # The script waits 10 hours to send from midnight
- hours: 14 # Remove the notification when the date turn
- data: "{{ data }}"
- action_scripts: # Callback handles for «data.actions»
- SEND_SMS:
- script: script.birthday_wishes
- variables:
- lang: "{{ lang }}"
- name: "{{ name }}"
- age: "{{ age }}"
- notify_device: "{{ repeat.item }}"
- number: "{{ phones|join(',') }}"
|