From 7901264a828bbd80a642910591a808337046a6e4 Mon Sep 17 00:00:00 2001 From: Tony Stork Date: Fri, 14 Apr 2023 17:29:41 -0400 Subject: [PATCH] Make more use of script variables --- packages/emmalynn.yaml | 27 ++++++----- packages/scheduling.yaml | 102 ++++++++++++++++++++++++--------------- 2 files changed, 77 insertions(+), 52 deletions(-) diff --git a/packages/emmalynn.yaml b/packages/emmalynn.yaml index 2c00393..2b24ca6 100644 --- a/packages/emmalynn.yaml +++ b/packages/emmalynn.yaml @@ -120,6 +120,9 @@ script: icon: mdi:lightbulb-night mode: restart sequence: + - variables: + time: "{{ states('sensor.time') }}" + bedtime: "{{ state_attr('input_datetime.emma_bedtime','timestamp') | timestamp_custom('%H:%M', false) }}" - choose: - conditions: - condition: state @@ -131,7 +134,7 @@ script: - condition: and conditions: - condition: template - value_template: "{{ states('sensor.time') < state_attr('input_datetime.emma_bedtime','timestamp') | timestamp_custom('%H:%M', false) }}" + value_template: "{{ time < bedtime }}" - condition: state entity_id: input_boolean.give_me_darkness state: 'on' @@ -152,7 +155,7 @@ script: - condition: and conditions: - condition: template - value_template: "{{ states('sensor.time') < state_attr('input_datetime.emma_bedtime','timestamp') | timestamp_custom('%H:%M', false) }}" + value_template: "{{ time < bedtime }}" - condition: state entity_id: input_boolean.give_me_darkness state: 'off' @@ -185,10 +188,7 @@ script: - choose: - conditions: - condition: template - value_template: > - {% set time = states('sensor.time') %} - {% set bedtime = state_attr('input_datetime.emma_bedtime','timestamp') | timestamp_custom('%H:%M', false) %} - {{ time >= bedtime }} + value_template: "{{ time >= bedtime }}" sequence: - choose: - conditions: @@ -231,10 +231,7 @@ script: tag: emma-climate - conditions: - condition: template - value_template: > - {% set time = states('sensor.time') %} - {% set bedtime = state_attr('input_datetime.emma_bedtime','timestamp') | timestamp_custom('%H:%M', false) %} - {{ time < bedtime }} + value_template: "{{ time < bedtime }}" sequence: - choose: - conditions: @@ -259,6 +256,13 @@ script: icon: mdi:weather-sunset-up mode: restart sequence: + - variables: + timestamp: "{{ ((now().hour * 60 + now().minute) * 60 ) }}" + wakeup: "{{ state_attr('input_datetime.emma_wakeup','timestamp') }}" + bedtime: "{{ state_attr('input_datetime.emma_bedtime','timestamp') }}" + - service: script.turn_on + target: + entity_id: script.emma_bedroom_scheduling_evening - choose: - conditions: - condition: state @@ -308,8 +312,7 @@ script: - if: - condition: template value_template: >- - {% set ct = ((now().hour * 60 + now().minute) * 60 ) %} - {{ is_state('fan.emma_air_conditioner','on') and is_state('input_boolean.hot_day','off') and ct < state_attr('input_datetime.emma_bedtime','timestamp') and ct >= state_attr('input_datetime.emma_wakeup','timestamp') }} + {{ is_state('fan.emma_air_conditioner','on') and is_state('input_boolean.hot_day','off') and timestamp < bedtime }} then: - service: script.turn_on target: diff --git a/packages/scheduling.yaml b/packages/scheduling.yaml index 2b35db4..7e1daf3 100644 --- a/packages/scheduling.yaml +++ b/packages/scheduling.yaml @@ -299,6 +299,10 @@ script: {% endif %} - delay: seconds: 1 + - variables: + bedtime: "{{ state_attr('input_datetime.kallen_bedtime','timestamp') }}" + low: "{{ states('sensor.overnight_lowest_temperature') | int }}" + threshold: "{{ states('input_number.kallen_fan_threshold') | int }}" - if: - condition: state entity_id: input_boolean.kallen_briefing_times_override @@ -307,7 +311,7 @@ script: - service: input_datetime.set_datetime entity_id: input_datetime.kallen_nightly_briefing data: - time: "{{ (state_attr('input_datetime.kallen_bedtime','timestamp') - 1200) | timestamp_custom('%H:%M', false) }}" + time: "{{ (bedtime - 1200) | timestamp_custom('%H:%M', false) }}" - if: - condition: state entity_id: input_boolean.kallen_bedroom_climate_override @@ -320,7 +324,7 @@ script: option: > {% if is_state('input_boolean.kallen_overnight','on') %} N/A - {% elif (states('sensor.overnight_lowest_temperature') | int) >= (states('input_number.kallen_fan_threshold') | int) %} + {% elif low >= threshold %} Fan {% else %} White Noise @@ -331,9 +335,9 @@ script: data: time: > {% if is_state('input_boolean.hot_day','on') %} - {{ (state_attr('input_datetime.kallen_bedtime','timestamp') - 3600) | timestamp_custom('%H:%M', false) }} + {{ bedtime - 3600 | timestamp_custom('%H:%M', false) }} {% else %} - {{ state_attr('input_datetime.kallen_bedtime','timestamp') | timestamp_custom('%H:%M', false) }} + {{ bedtime | timestamp_custom('%H:%M', false) }} {% endif %} - if: - condition: state @@ -344,7 +348,7 @@ script: target: entity_id: input_datetime.kallen_bedtime data: - time: "{{ (state_attr('input_datetime.kallen_bedtime','timestamp') + 3600) | timestamp_custom('%H:%M', false) }}" + time: "{{ bedtime + 3600 | timestamp_custom('%H:%M', false) }}" - service: input_datetime.set_datetime target: entity_id: input_datetime.kallen_nightly_briefing @@ -367,15 +371,22 @@ script: house_scheduling_morning: alias: 'House Scheduling Morning' sequence: + - variables: + kallen_school_start: "{{ state_attr('input_datetime.kallen_school_day_start','timestamp') }}" + tina_work_start: "{{ state_attr('input_datetime.tina_workday_start','timestamp') }}" + tina_work_end: "{{ state_attr('input_datetime.tina_workday_end','timestamp') }}" + high: "{{ states('sensor.todays_high_temp') | int }}" - service: input_datetime.set_datetime entity_id: input_datetime.audible_notification_on data: time: > {% if is_state('input_boolean.kallen_school_today','on') %} - {{ (state_attr('input_datetime.kallen_school_day_start','timestamp') - 5400) | timestamp_custom('%H:%M', false) }} + {{ kallen_school_start - 5400 | timestamp_custom('%H:%M', false) }} {% else %} 09:00 {% endif %} + - variables: + audible_on: "{{ state_attr('input_datetime.audible_notification_on','timestamp') }}" - if: - condition: state entity_id: input_boolean.house_briefing_times_override @@ -389,13 +400,13 @@ script: {% if is_state('input_boolean.two_hour_delay','on') %} 10:20 {% else %} - {{ (state_attr('input_datetime.kallen_school_day_start','timestamp') - 2400) | timestamp_custom('%H:%M', false) }} + {{ kallen_school_start - 2400 | timestamp_custom('%H:%M', false) }} {% endif %} {% elif is_state('input_boolean.work_today','on') and is_state('input_boolean.kallen_school_today','off') %} - {% if (state_attr('input_datetime.tina_workday_start','timestamp')) > 43200 %} + {% if tina_work_start > 43200 %} 12:00 - {% elif (state_attr('input_datetime.tina_workday_start','timestamp') - 1500 ) > (state_attr('input_datetime.audible_notification_on','timestamp')) %} - {{ (state_attr('input_datetime.tina_workday_start', 'timestamp') - 1500) | timestamp_custom('%H:%M', false) }} + {% elif (tina_work_start - 1500) > audible_on %} + {{ tina_work_start - 1500 | timestamp_custom('%H:%M', false) }} {% else %} 10:00 {% endif %} @@ -415,15 +426,15 @@ script: {% elif is_state('input_boolean.kallen_school_today','off') and is_state('input_boolean.work_today','off') %} 15:00 {% else %} - {% if (state_attr('input_datetime.tina_workday_end','timestamp')) > 61200 %} + {% if tina_work_end > 61200 %} 16:00 {% else %} - {{ (state_attr('input_datetime.tina_workday_end', 'timestamp') + 3600) | timestamp_custom('%H:%M', false) }} + {{ tina_work_end + 3600 | timestamp_custom('%H:%M', false) }} {% endif %} {% endif %} - if: - condition: template - value_template: "{{ states('sensor.todays_high_temp') | int >= states('input_number.hot_day_threshold') | int }}" + value_template: "{{ high >= states('input_number.hot_day_threshold') | int }}" then: - service: input_boolean.turn_on target: @@ -434,7 +445,7 @@ script: entity_id: input_boolean.hot_day - if: - condition: template - value_template: "{{ states('sensor.todays_high_temp') | int <= states('input_number.cold_day_threshold') | int }}" + value_template: "{{ high <= states('input_number.cold_day_threshold') | int }}" then: - service: input_boolean.turn_on target: @@ -450,6 +461,8 @@ script: house_scheduling_evening: alias: 'House Scheduling Evening' sequence: + - variables: + kallen_bedtime: "{{ state_attr('input_datetime.kallen_bedtime','timestamp') }}" - if: - condition: state entity_id: input_boolean.house_briefing_times_override @@ -460,9 +473,9 @@ script: data: time: > {% if is_state('input_boolean.kallen_late_bedtime','on') %} - {{ (state_attr('input_datetime.kallen_bedtime','timestamp') - 7200) | timestamp_custom('%H:%M', false) }} + {{ kallen_bedtime - 7200 | timestamp_custom('%H:%M', false) }} {% else %} - {{ (state_attr('input_datetime.kallen_bedtime','timestamp') - 3600) | timestamp_custom('%H:%M', false) }} + {{ kallen_bedtime - 3600 | timestamp_custom('%H:%M', false) }} {% endif %} - service: input_datetime.set_datetime entity_id: input_datetime.audible_notification_off @@ -505,7 +518,10 @@ script: master_bedroom_scheduling_evening: alias: 'Master Bedroom Scheduling Evening' sequence: - - if: + - variables: + low: "{{ states('sensor.overnight_lowest_temperature') | int }}" + high: "{{ states('sensor.todays_high_temp') | int }}" + - if: - condition: state entity_id: input_boolean.master_bedroom_climate_override state: 'off' @@ -514,7 +530,6 @@ script: entity_id: input_datetime.master_bedroom_cooling data: time: > - {% set low = states('sensor.overnight_lowest_temperature') | int %} {% if is_state('input_boolean.hot_day','on') and is_state('binary_sensor.kallen_school_tomorrow','on') %} 20:00 {% elif is_state('input_boolean.hot_day','on') and is_state('binary_sensor.kallen_school_tomorrow','off') %} @@ -530,8 +545,6 @@ script: entity_id: input_datetime.master_bedroom_fan data: time: > - {% set low = states('sensor.overnight_lowest_temperature') | int %} - {% set high = states('sensor.todays_high_temp') | int %} {% if low > 60 or is_state('input_boolean.hot_day','on') %} 22:30 {% elif low <= 60 and low >= 50 %} @@ -575,6 +588,8 @@ script: emma_bedroom_scheduling_morning: alias: 'Emma Bedroom Scheduling Morning' sequence: + - variables: + master_bedroom_wakeup: "{{ states('input_datetime.master_bedroom_wakeup') }}" - if: - condition: state entity_id: input_boolean.emma_wakeup_override @@ -583,7 +598,7 @@ script: - service: input_datetime.set_datetime entity_id: input_datetime.emma_wakeup data: - time: "{{ states('input_datetime.master_bedroom_wakeup') }}" + time: "{{ master_bedroom_wakeup }}" - service: input_boolean.turn_on target: entity_id: input_boolean.emma_scheduling_morning_ran @@ -591,6 +606,10 @@ script: emma_bedroom_scheduling_evening: alias: 'Emma Bedroom Scheduling Evening' sequence: + - variables: + high: "{{ states('sensor.todays_high_temp') | int }}" + low: "{{ states('sensor.overnight_lowest_temperature') | int }}" + aircon: "{{ states('input_number.emma_aircon_threshold') | int }}" - if: - condition: state entity_id: input_boolean.emma_bedtime_override @@ -600,8 +619,6 @@ script: entity_id: input_datetime.emma_bedtime data: time: > - {% set high = states('sensor.todays_high_temp') | int %} - {% set low = states('sensor.overnight_lowest_temperature') | int %} {% if is_state('input_boolean.hot_day','on') %} 22:00 {% elif high >= 80 or low >= 60 %} @@ -620,7 +637,7 @@ script: data: option: > {% if is_state('input_boolean.emma_bedroom_aircon_installed','on') %} - {% if (states('sensor.overnight_lowest_temperature') | int) >= (states('input_number.emma_aircon_threshold') | int) %} + {% if low >= aircon %} AC {% else %} White Noise @@ -635,25 +652,31 @@ script: security_scheduling: alias: Security Scheduling sequence: + - variables: + ct: "{{ ((now().hour * 60 + now().minute) * 60 ) }}" + kallen_school_start: "{{ state_attr('input_datetime.kallen_school_day_start','timestamp') }}" + # kallen_school_end: + # tina_work_start: + # tina_work_end: + #! IN PROGRESS - service: input_datetime.set_datetime entity_id: input_datetime.morning_alarm_disarm data: time: > - {% set ct = ((now().hour * 60 + now().minute) * 60 ) %} {% if is_state('input_boolean.work_today','on') and is_state('input_boolean.kallen_school_today','on') %} - {% if ct > state_attr('input_datetime.kallen_school_day_start','timestamp') %} + {% if ct > kallen_school_start %} {{ (state_attr('input_datetime.tina_workday_start','timestamp') - 1800) | timestamp_custom('%H:%M', false) }} - {% elif ct > state_attr('input_datetime.tina_workday_start','timestamp') and ct < state_attr('input_datetime.kallen_school_day_start','timestamp') %} - {{ (state_attr('input_datetime.kallen_school_day_start','timestamp') - 2700) | timestamp_custom('%H:%M', false) }} - {% elif state_attr('input_datetime.tina_workday_start','timestamp') < (state_attr('input_datetime.kallen_school_day_start','timestamp')) %} + {% elif ct > state_attr('input_datetime.tina_workday_start','timestamp') and ct < kallen_school_start %} + {{ kallen_school_start - 2700 | timestamp_custom('%H:%M', false) }} + {% elif state_attr('input_datetime.tina_workday_start','timestamp') < kallen_school_start %} {{ (state_attr('input_datetime.tina_workday_start','timestamp') - 1800) | timestamp_custom('%H:%M', false) }} {% else %} - {{ (state_attr('input_datetime.kallen_school_day_start','timestamp') - 2700) | timestamp_custom('%H:%M', false) }} + {{ kallen_school_start - 2700 | timestamp_custom('%H:%M', false) }} {% endif %} {% elif is_state('input_boolean.work_today','on') and is_state('input_boolean.kallen_school_today','off') %} {{ (state_attr('input_datetime.tina_workday_start', 'timestamp') - 1800) | timestamp_custom('%H:%M', false) }} {% elif is_state('input_boolean.kallen_school_today','on') %} - {{ (state_attr('input_datetime.kallen_school_day_start','timestamp') - 2700) | timestamp_custom('%H:%M', false) }} + {{ kallen_school_start - 2700 | timestamp_custom('%H:%M', false) }} {% else %} 11:00 {% endif %} @@ -661,21 +684,20 @@ script: entity_id: input_datetime.morning_alarm_rearm data: time: > - {% set ct = ((now().hour * 60 + now().minute) * 60 ) %} {% if is_state('input_boolean.work_today','on') and is_state('input_boolean.kallen_school_today','off') %} {{ (state_attr('input_datetime.tina_workday_start','timestamp') + 900) | timestamp_custom('%H:%M', false) }} {% elif is_state('input_boolean.kallen_school_today','on') and is_state('input_boolean.work_today','off') %} - {{ (state_attr('input_datetime.kallen_school_day_start','timestamp') + 3600) | timestamp_custom('%H:%M', false) }} + {{ kallen_school_start + 3600 | timestamp_custom('%H:%M', false) }} {% elif is_state('input_boolean.work_today','on') and is_state('input_boolean.kallen_school_today','on') %} - {% if ct > state_attr('input_datetime.kallen_school_day_start','timestamp') %} + {% if ct > kallen_school_start %} {{ (state_attr('input_datetime.tina_workday_start','timestamp') + 900) | timestamp_custom('%H:%M', false) }} - {% elif ct > state_attr('input_datetime.tina_workday_start','timestamp') and ct < state_attr('input_datetime.kallen_school_day_start','timestamp') %} - {{ (state_attr('input_datetime.kallen_school_day_start','timestamp') - 2700) | timestamp_custom('%H:%M', false) }} - {% elif state_attr('input_datetime.tina_workday_start','timestamp') < state_attr('input_datetime.kallen_school_day_start','timestamp') %} - {{ (state_attr('input_datetime.kallen_school_day_start','timestamp') + 1800) | timestamp_custom('%H:%M', false) }} + {% elif ct > state_attr('input_datetime.tina_workday_start','timestamp') and ct < kallen_school_start %} + {{ kallen_school_start - 2700 | timestamp_custom('%H:%M', false) }} + {% elif state_attr('input_datetime.tina_workday_start','timestamp') < kallen_school_start %} + {{ kallen_school_start + 1800 | timestamp_custom('%H:%M', false) }} {% else %} - {% if (state_attr('input_datetime.tina_workday_start','timestamp') - state_attr('input_datetime.kallen_school_day_start','timestamp')) > 5400 %} - {{ (state_attr('input_datetime.kallen_school_day_start','timestamp') + 1800) | timestamp_custom('%H:%M', false) }} + {% if (state_attr('input_datetime.tina_workday_start','timestamp') - kallen_school_start) > 5400 %} + {{ kallen_school_start + 1800 | timestamp_custom('%H:%M', false) }} {% else %} {{ (state_attr('input_datetime.tina_workday_start','timestamp') + 900) | timestamp_custom('%H:%M', false) }} {% endif %}