Hi, I was wondering if I could have some help with an automation that will take sensor data and subtract it from a value.
My idea is to have a time input, for when I want to wake up (ex: 6:00AM) then have another time input for how long I want to sleep (ex: 7hr). Then send a notification to my phone for when I should go to sleep to meet the criteria of 7hrs and wake up at 6
Create an Input Datetime to store the wake up time.
then have another time input for how long I want to sleep (ex: 7hr).
Create an Input Number to store the sleep duration (in hours).
Create a Template Sensor that subtracts the value of the Input Number from the Input Datetime and report it as a time (and the current date). Set its device_class
to timestamp
so it can be used in a Time Trigger.
Then send a notification to my phone for when I should go to sleep to meet the criteria of 7hrs and wake up at 6
Create an automation with a Time Trigger that references the Template Sensor. The automation’s action is a notification to your phone.
EDIT
Example of the Template Sensor:
template:
- sensor:
- name: Notification Time
device_class: timestamp
state: >
{{ (today_at(states('input_datetime.wakeup_time'))
- timedelta(hours = states('input_number.sleep_duration') | int(0))).isoformat() }}
Example of the automation:
alias: Sleep Notification
trigger:
- platform: time
at: sensor.notification_time
condition: []
action:
- service: notify.your_phone
data:
message: Go to sleep now.
123Taras was faster
To add something of value:
I use a similar concept that creates a sensor that tells me in how many hours my Alarm is going off.
Maybe its helpful.
Please see below:
- platform: template # Show in Humanreadable language how much time until the alarm goes off
sensors:
nextalarm:
friendly_name: "Next Alarm"
value_template: >-
{% if is_state("input_boolean.alarm", "off") -%}
{% set time= 0 -%}
{% elif as_timestamp( now().replace(hour=(states.input_datetime.alarm.attributes.hour)).replace(minute=(states.input_datetime.alarm.attributes.minute)).replace(second=0)) - as_timestamp(states('sensor.date_time').replace(',', '')) < 0
and
is_state("input_boolean.alarm", "on") -%}
{% set time = as_timestamp( now().replace(hour=(states.input_datetime.alarm.attributes.hour)).replace(minute=(states.input_datetime.alarm.attributes.minute)).replace(second=0)) + 86400 - as_timestamp(states('sensor.date_time').replace(',', ''))%}
{% elif as_timestamp( now().replace(hour=(states.input_datetime.alarm.attributes.hour)).replace(minute=(states.input_datetime.alarm.attributes.minute)).replace(second=0)) - as_timestamp(states('sensor.date_time').replace(',', '')) > 0
and
is_state("input_boolean.alarm", "on") -%}
{% set time = as_timestamp( now().replace(hour=(states.input_datetime.alarm.attributes.hour)).replace(minute=(states.input_datetime.alarm.attributes.minute)).replace(second=0)) - as_timestamp(states('sensor.date_time').replace(',', ''))%}
{%- endif %}
{% set minutes = ((time % 3600) / 60) | int %}
{% set hours = ((time % 86400) / 3600) | int %}
{% set days = (time / 86400) | int %}
{%- if time == "0" -%}
/
{%- elif time < 60 -%}
<1m
{%- else -%}
{%- if days > 0 -%}
{%- if days == 1 -%}
1d
{%- else -%}
{{ days }}d
{%- endif -%}
{%- endif -%}
{%- if hours > 0 -%}
{%- if days > 0 -%}
{{ ':' }}
{%- endif -%}
{%- if hours == 1 -%}
1h
{%- else -%}
{{ hours }}h
{%- endif -%}
{%- endif -%}
{%- if minutes > 0 -%}
{%- if days > 0 or hours > 0 -%}
{{ ':' }}
{%- endif -%}
{%- if minutes == 1 -%}
1m
{%- else -%}
{{ minutes }}m
{%- endif -%}
{%- endif -%}
{%- endif -%}
Use this version that includes an attribute named sleep_time
.
template:
- sensor:
- name: Notification Time
device_class: timestamp
state: >
{{ (today_at(states('input_datetime.wakeup_time'))
- timedelta(hours = states('input_number.sleep_duration') | int(0))).isoformat() }}
attributes:
sleep_time: >
{{ (today_at(states('input_datetime.wakeup_time'))
- timedelta(hours = states('input_number.sleep_duration') | int(0))).strftime('%-H:%M') }}
In the Entity card, set the Attribute field to sleep_time
.