Skip to content

Instantly share code, notes, and snippets.

@holblin
Created April 10, 2024 23:52
Show Gist options
  • Save holblin/085f8c36d8461a134ccb4c2166a761ab to your computer and use it in GitHub Desktop.
Save holblin/085f8c36d8461a134ccb4c2166a761ab to your computer and use it in GitHub Desktop.
IOS sleep data receiver
blueprint:
name: IOS Sleep data receiver
description: >-
A script that receive data for IOS about sleep bedtime.
domain: script
input:
next_alarm:
name: Next alarm input
description: Datetime input helper to save the next alarm
selector:
entity:
filter:
- domain: input_datetime
skip_alarm:
name: Skip alarm input
description: Datetime input helper to save the skip alarm if it exists
selector:
entity:
filter:
- domain: input_datetime
sequence:
- variables:
templateResult: >
{# Let's calculate the TZ diff #}
{# Extract TZ for IOS #}
{% set iosTZ = (IosNow | as_datetime).strftime('%z') | int %}
{% set iosMinutesTZ = iosTZ - ((iosTZ / 100) | int) * 40 %}
{# Extract TZ for HA #}
{% set haTZ = now().strftime('%z') | int %}
{% set haMinutesTZ = haTZ - ((haTZ / 100) | int) * 40 %}
{# Calculate the offset in minutes for IOS to HA #}
{% set diffTZ = iosMinutesTZ - haMinutesTZ %}
{# Let's set some vairable #} {% set alarmTimeStr = hours|string + ':' +
minutes|string %} {% set repeatDaysArray = (repeatDays2 +
repeatDays).split('\n')%} {% set ns = namespace(
alarmDay = today_at(alarmTimeStr) + timedelta(minutes = diffTZ),
skippedOnce = false,
foundDay = false,
skippedAlarm = "2000-01-01T00:00:00" | as_datetime,
) %}
{# Does the alarm could be for today? #} {% if (ns.alarmDay < now()) %}
{% set ns.alarmDay = ns.alarmDay + timedelta(days = 1) %}
{%endif%}
{# Let's found which day this alarm is #} {% for _ in range(0, 8) %}
{% if (not ns.foundDay) %}
{% if (ns.alarmDay.strftime('%A') not in repeatDaysArray)%}
{# This day is not in the list, let go to the next day #}
{% set ns.alarmDay = ns.alarmDay + timedelta(days = 1) %}
{% elif (not isEnabled and not ns.skippedOnce) %}
{# This day is in the list, but we need to skip it #}
{% set ns.skippedOnce = true %}
{% set ns.skippedAlarm = ns.alarmDay %}
{% set ns.alarmDay = ns.alarmDay + timedelta(days = 1) %}
{% else %}
{# This is the right day! #}
{% set ns.foundDay = true %}
{%endif%}
{%endif%}
{% endfor %}
{# If we didn't found a day, let's just set alarmDay to false #} {% if
(not ns.foundDay) %}
{% set ns.alarmDay = "2000-01-01T00:00:00" | as_datetime %}
{%endif%}
{# Results! #}
{{ns.foundDay}}@{{ns.alarmDay}}@{{ns.skippedAlarm}}@{{ns.skippedOnce}}
- variables:
foundDay: "{{bool(templateResult.split('@')[0]) }}"
alarmDay: "{{templateResult.split('@')[1] | as_datetime }}"
skippedAlarm: "{{templateResult.split('@')[2] | as_datetime }}"
skippedOnce: "{{bool(templateResult.split('@')[3]) }}"
- service: input_datetime.set_datetime
target:
entity_id: !input next_alarm
data:
datetime: "{{alarmDay}}"
- service: input_datetime.set_datetime
target:
entity_id: !input skip_alarm
data:
datetime: "{{skippedAlarm}}"
mode: single
fields:
allowsSnooze:
selector:
boolean: {}
name: allowsSnooze
required: true
repeats:
selector:
boolean: {}
name: repeats
required: true
isEnabled:
selector:
boolean: {}
name: isEnabled
required: true
hours:
selector:
number:
min: 0
max: 23
name: hours
required: true
minutes:
selector:
number:
min: 0
max: 57
name: minutes
required: true
repeatDays:
selector:
text:
multiline: true
name: repeatDays
required: true
IosNow:
selector:
text: null
name: IosNow
required: true
repeatdays2:
selector:
text:
multiline: true
name: repeatDays2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment