My wife is a shift worker with alternating rotations. There is no digital calendar (Germany - you know…) with a rotation schedule in it. There is only a piece of paper available…
So we have no data for the automation.
Idea: I would like to give her a small button that she can press before she goes to bed to activate the heating in the bathroom in time the next morning.
What I did so far…
alias: New Automation
description: ""
trigger:
- platform: event
event_type: hue_event
event_data:
unique_id: id....
condition:
- condition: or
conditions:
- condition: template
value_template: "{{ trigger.event.data.type == 'short_release' }}"
- condition: template
value_template: "{{ trigger.event.data.type == 'initial_press' }}"
action:
- service: climate.set_temperature
data:
temperature: 22
hvac_mode: heat
target:
device_id: id....
- delay:
hours: 0
minutes: 0
seconds: 15
milliseconds: 0
- service: climate.set_hvac_mode
data:
hvac_mode: auto
target:
device_id: id.....
mode: single
Now I still need to calculate a delay before the action runs. I have already implemented this in Python as follows
import datetime
current_date = datetime.datetime.today()
today_schedule = current_date.replace(hour=4, minute=30, second=0, microsecond=0)
if (current_date < today_schedule):
#today
delta = today_schedule - current_date
delta_m = int(delta.total_seconds()/60)
print(delta_m)
else:
#tomorrow
next_day_schedule = (datetime.datetime.today() + datetime.timedelta(days=1)).replace(hour=4, minute=30, second=0, microsecond=0)
delta = next_day_schedule - current_date
delta_m = int(delta.total_seconds()/60)
print (delta_m)
How do you like this approach?
How can I build this calculation into a delay?
Thank you for your help in advanced…
Cheers,
Marcel
Update:
This is my Solution in total:
alias: New Automation
description: ""
trigger:
- platform: event
event_type: hue_event
event_data:
unique_id: 23d97273-49f9-42c1-9821-80a8fd9a22ec
condition:
- condition: or
conditions:
- condition: template
value_template: "{{ trigger.event.data.type == 'short_release' }}"
- condition: template
value_template: "{{ trigger.event.data.type == 'initial_press' }}"
action:
- delay: >
{% set schedule = today_at("04:30") %}
{% if now() < schedule %}.
{## today ##}
{% set delta = schedule - now() %}
{%- else -%}
{## tomorrow ##}
{% set schedule_tomorrow = schedule + timedelta(days=1) %}
{% set delta = schedule_tomorrow - now() %}
{%- endif %}
{% set deltatmp = int(delta.total_seconds()/60) %}
{% set deltah = int(deltatmp / 60) %}
{% set deltam = deltatmp - (deltah * 60) %}
{{ deltah}}:{{deltam}}:00
- service: climate.set_temperature
data:
temperature: 22
hvac_mode: heat
target:
device_id: de7767598be7f0f1eaca882c00cb0f3f
- delay:
hours: 1
minutes: 30
- service: climate.set_hvac_mode
data:
hvac_mode: auto
target:
device_id: de7767598be7f0f1eaca882c00cb0f3f
mode: single