From 44709dd7591fe332a9556dc6d251da3de97abd51 Mon Sep 17 00:00:00 2001
From: Tony Stork
Date: Tue, 2 May 2023 00:55:50 -0400
Subject: [PATCH] Decided to use a jinja template instead of a sensor for E's
sleep info
---
configuration.yaml | 2 --
custom_templates/formatting.jinja | 4 +++
custom_templates/status.jinja | 23 +++++++++++++++
packages/emmalynn.yaml | 40 --------------------------
templates/speech/daily_briefing.yaml | 4 ++-
templates/speech/nightly_briefing.yaml | 4 ++-
templates/speech/welcome_home.yaml | 4 ++-
7 files changed, 36 insertions(+), 45 deletions(-)
create mode 100644 custom_templates/formatting.jinja
create mode 100644 custom_templates/status.jinja
diff --git a/configuration.yaml b/configuration.yaml
index 6e4b6b6..52e1083 100644
--- a/configuration.yaml
+++ b/configuration.yaml
@@ -112,7 +112,6 @@ recorder:
- sensor.time_utc
- sensor.hacs
- sensor.mariadb_database_size
- - sensor.emma_nap_info
include:
entities:
- media_player.living_room_tv
@@ -167,7 +166,6 @@ influxdb:
- sensor.xr500_gateway_kib_s_sent
- light.all_lights
- fan.all_fans
- - sensor.emma_nap_info
include:
domains:
- sun
diff --git a/custom_templates/formatting.jinja b/custom_templates/formatting.jinja
new file mode 100644
index 0000000..e6086f3
--- /dev/null
+++ b/custom_templates/formatting.jinja
@@ -0,0 +1,4 @@
+{%- macro cleanup(data) -%}
+ {%- for item in data.split("\n") if item | trim != "" -%}
+ {{ item | trim }} {% endfor -%}
+{%- endmacro -%}
\ No newline at end of file
diff --git a/custom_templates/status.jinja b/custom_templates/status.jinja
new file mode 100644
index 0000000..70ba779
--- /dev/null
+++ b/custom_templates/status.jinja
@@ -0,0 +1,23 @@
+{% macro emma_sleep() %}
+{% from 'time.jinja' import input_datetime_12hr_with_date %}
+{% set asleep = state_attr('input_datetime.emma_down_for_nap','timestamp') | int %}
+{% set wakeup = state_attr('input_datetime.emma_up_from_nap','timestamp') | int %}
+{% set current = as_timestamp(now()) | int %}
+{% set diff = (wakeup - asleep) | int %}
+{% set nowHour = (current - asleep) | timestamp_custom("%-H", false) %}
+{% set nowMin = (current - asleep) | timestamp_custom("%-M", false) %}
+{% set hour = diff | timestamp_custom('%-H', false) | int %}
+{% set minute = diff | timestamp_custom('%-M', false) | int %}
+{% set day = now().strftime("%-d") %}
+{% set asleep_day = asleep | timestamp_custom("%-d") %}
+{% set wakeup_day = wakeup | timestamp_custom("%-d") %}
+{% if is_state('input_boolean.emma_has_napped','on') or ((asleep_day == day) and (wakeup_day == day)) %}
+ Emma napped today for {{ hour }} hours and {{ minute }} minutes. She was retrieved from her room at {{ input_datetime_12hr_with_date('input_datetime.emma_up_from_nap') }} approximately.
+{% elif is_state('input_boolean.emma_has_napped','off') and ((asleep_day == day) and (wakeup_day != day)) and is_state('input_boolean.emma_sleeping','on') %}
+ Emma is down for nap. She was put down at {{ input_datetime_12hr_with_date('input_datetime.emma_down_for_nap') }} approximately. She has been asleep for {{ nowHour }} hours and {{ nowMin }} minutes.
+{% elif is_state('input_boolean.emma_sleeping','on') %}
+ Emma is asleep for the night.
+{% else %}
+ Emma is awake, and does not appear to have napped yet today.
+{% endif %}
+{% endmacro %}
\ No newline at end of file
diff --git a/packages/emmalynn.yaml b/packages/emmalynn.yaml
index e7378db..097267f 100644
--- a/packages/emmalynn.yaml
+++ b/packages/emmalynn.yaml
@@ -30,46 +30,6 @@ input_datetime:
has_time: true
icon: mdi:eye
-sensor:
- - platform: template
- sensors:
- emma_nap_info:
- friendly_name: Emma Nap Info
- unique_id: 60664690-0eb6-400a-83d5-21e56f029910
- value_template: >
- {% macro getReport() %}
- {% from 'time.jinja' import input_datetime_12hr_with_date %}
- {% set asleep = state_attr('input_datetime.emma_down_for_nap','timestamp') | int %}
- {% set wakeup = state_attr('input_datetime.emma_up_from_nap','timestamp') | int %}
- {% set current = as_timestamp(now()) | int %}
- {% set diff = (wakeup - asleep) | int %}
- {% set nowHour = (current - asleep) | timestamp_custom("%-H", false) %}
- {% set nowMin = (current - asleep) | timestamp_custom("%-M", false) %}
- {% set hour = diff | timestamp_custom('%-H', false) | int %}
- {% set minute = diff | timestamp_custom('%-M', false) | int %}
- {% set day = now().strftime("%-d") %}
- {% set asleep_day = asleep | timestamp_custom("%-d") %}
- {% set wakeup_day = wakeup | timestamp_custom("%-d") %}
- {% if is_state('input_boolean.emma_has_napped','on') or ((asleep_day == day) and (wakeup_day == day)) %}
- Emma napped today for {{ hour }} hours and {{ minute }} minutes.
- {% elif is_state('input_boolean.emma_has_napped','off') and ((asleep_day == day) and (wakeup_day != day)) %}
- Emma appears to be napping currently. She was put down at {{ input_datetime_12hr_with_date('input_datetime.emma_down_for_nap') }} approximately. She has been asleep for {{ nowHour }} hours and {{ nowMin }} minutes.
- {% else %}
- Emma does not appear to have napped yet today.
- {% endif %}
- {% endmacro %}
- {%- macro cleanup(data) -%}
- {%- for item in data.split("\n") if item | trim != "" -%}
- {{ item | trim }} {% endfor -%}
- {%- endmacro -%}
-
- {%- macro mother_of_all_macros() -%}
- {{ getReport() }}
- {%- endmacro -%}
-
- {{- cleanup(mother_of_all_macros()) -}}
- icon_template: mdi:sleep
-
automation:
- id: 4f01dff7-be22-4850-a05e-1906e3151441
alias: 'Emma Sleeping'
diff --git a/templates/speech/daily_briefing.yaml b/templates/speech/daily_briefing.yaml
index 62c1aa8..68e601a 100644
--- a/templates/speech/daily_briefing.yaml
+++ b/templates/speech/daily_briefing.yaml
@@ -3,6 +3,8 @@
{%- macro getReport() -%}
{% from 'speech.jinja' import greeting, dadjoke, inspirational_quote %}
{% from 'time.jinja' import input_datetime_12hr %}
+ {% from 'status.jinja' import emma_sleep %}
+ {% from 'formatting.jinja' import cleanup %}
{{ greeting() }}
@@ -359,7 +361,7 @@
- {{ states('sensor.emma_nap_info') }}
+ {{ cleanup(emma_sleep()) }}
diff --git a/templates/speech/nightly_briefing.yaml b/templates/speech/nightly_briefing.yaml
index d99cd30..c1084e4 100644
--- a/templates/speech/nightly_briefing.yaml
+++ b/templates/speech/nightly_briefing.yaml
@@ -3,6 +3,8 @@
{%- macro getReport() -%}
{% from 'speech.jinja' import greeting, dadjoke, inspirational_quote %}
{% from 'time.jinja' import input_datetime_12hr, read_time_from_calendar %}
+ {% from 'status.jinja' import emma_sleep %}
+ {% from 'formatting.jinja' import cleanup %}
{{ greeting() }}
@@ -222,7 +224,7 @@
- {{ states('sensor.emma_nap_info') }}
+ {{ cleanup(emma_sleep()) }}
diff --git a/templates/speech/welcome_home.yaml b/templates/speech/welcome_home.yaml
index 7024c09..10be022 100644
--- a/templates/speech/welcome_home.yaml
+++ b/templates/speech/welcome_home.yaml
@@ -3,6 +3,8 @@
{%- macro getReport() -%}
{% from 'speech.jinja' import dadjoke %}
{% from 'time.jinja' import input_datetime_12hr %}
+ {% from 'status.jinja' import emma_sleep %}
+ {% from 'formatting.jinja' import cleanup %}
"Welcome home, "
{% if is_state('person.tony_stork','home') and is_state('person.christina_stork','home') %}
@@ -149,7 +151,7 @@
- {{ states('sensor.emma_nap_info') }}
+ {{ cleanup(emma_sleep()) }}