diff --git a/blueprints/automation/wd-close-weather-notification.yaml b/blueprints/automation/wd-close-weather-notification.yaml index 6520980..c4a6029 100644 --- a/blueprints/automation/wd-close-weather-notification.yaml +++ b/blueprints/automation/wd-close-weather-notification.yaml @@ -1,105 +1,164 @@ blueprint: - name: Open Windows/Doors Notification - description: Notify when windows or doors are open based on weather state or temperature thresholds. + name: Open Windows/Doors Weather Notification + description: >- + Sends a notification if selected windows or doors are open when the weather changes + to a specific state (e.g., raining) or the temperature crosses a set threshold. domain: automation input: openings: name: Windows & Doors - description: Select windows and doors to monitor + description: Select all windows and doors you want to monitor. selector: entity: domain: binary_sensor + device_class: + - door + - window + - opening multiple: true + + notify_target: + name: Notification Target + description: Select the device, group, or service to send the notification to. + selector: + target: + + # --- Optional Weather Trigger --- weather_sensor: - name: Weather Sensor - description: Optional weather entity to trigger notification + name: (Optional) Weather Sensor + description: The weather entity to monitor (e.g., weather.home). selector: entity: domain: weather + default: + weather_trigger_states: name: Weather Trigger States - description: Select weather states that should trigger notification - default: - - storm - - rain + description: If a weather sensor is selected, notify when the weather changes to any of these states. selector: - text: - multiline: true - temperature_sensor: - name: Temperature Sensor - description: Optional temperature entity to trigger notification - selector: - entity: - domain: - - sensor - - weather - device_class: temperature - temp_above: - name: Notify if Temperature Above - description: Optional threshold to trigger notification - default: 22 - selector: - number: - min: -50 - max: 50 - unit_of_measurement: °C - temp_below: - name: Notify if Temperature Below - description: Optional threshold to trigger notification - default: 17 - selector: - number: - min: -50 - max: 50 - unit_of_measurement: °C - notify_target: - name: Notification Target - selector: - entity: - domain: notify - custom_message: - name: Custom Notification Message - default: "{{ entity_name }} is open! Trigger: {{ trigger_state }}" - selector: - text: {} + select: + multiple: true + options: + - "cloudy" + - "fog" + - "hail" + - "lightning" + - "lightning-rainy" + - "partlycloudy" + - "pouring" + - "rainy" + - "snowy" + - "snowy-rainy" + - "sunny" + - "windy" + - "windy-variant" + - "exceptional" + default: ["rainy", "pouring", "snowy"] + # --- Optional Temperature Trigger --- + temperature_sensor: + name: (Optional) Temperature Sensor + description: The temperature sensor to monitor (e.g., sensor.outside_temperature). + selector: + entity: + domain: sensor + device_class: temperature + default: + + temp_above: + name: Notify if Temperature is Above + description: Trigger a notification if the temperature rises above this value. + selector: + number: + min: -20 + max: 40 + unit_of_measurement: "°C" + default: 25 + + temp_below: + name: Notify if Temperature is Below + description: Trigger a notification if the temperature drops below this value. + selector: + number: + min: -20 + max: 40 + unit_of_measurement: "°C" + default: 10 + +# --- Triggers --- +# The automation will run when any of these conditions are met. trigger: + # 1. Trigger when the weather sensor's state changes. - platform: state entity_id: !input weather_sensor - to: "{{ !input weather_trigger_states.split(',') | map('trim') | list }}" + id: weather_change + + # 2. Trigger when temperature goes above the threshold. - platform: numeric_state entity_id: !input temperature_sensor above: !input temp_above + id: temp_hot + + # 3. Trigger when temperature goes below the threshold. - platform: numeric_state entity_id: !input temperature_sensor below: !input temp_below - + id: temp_cold + +# --- Conditions --- +# The action will only run if ALL of these conditions are true. condition: - - condition: state - entity_id: !input openings - state: 'on' + # 1. Check if an input was actually provided for the trigger that ran. + - condition: template + value_template: >- + {{ (trigger.id == 'weather_change' and iif(states(!input weather_sensor), true, false)) or + (trigger.id in ['temp_hot', 'temp_cold'] and iif(states(!input temperature_sensor), true, false)) }} + # 2. Check if at least one of the selected doors/windows is 'on' (open). + - condition: template + value_template: "{{ expand(!input openings) | selectattr('state', 'eq', 'on') | list | count > 0 }}" + +# --- Actions --- +# What the automation will do when triggered and conditions are met. action: - - service: "[[[ return !input.notify_target ]]]" - data: - message: > - {% set open_entities = states.binary_sensor | selectattr('entity_id', 'in', !input.openings) | selectattr('state', 'eq', 'on') | map(attribute='attributes.friendly_name') | list %} - {% if open_entities | length > 0 %} - {% set trigger_state = 'Unknown' %} - {% if trigger.platform == 'numeric_state' %} - {% if trigger.above is defined %} - {% set trigger_state = 'Temperature > ' + trigger.above | string %} - {% elif trigger.below is defined %} - {% set trigger_state = 'Temperature < ' + trigger.below | string %} - {% endif %} - {% elif trigger.platform == 'state' %} - {% set trigger_state = trigger.to_state.state %} - {% endif %} + # Define variables to use in the message. + - variables: + open_entities: "{{ expand(!input openings) | selectattr('state', 'eq', 'on') | map(attribute='name') | list }}" + open_count: "{{ open_entities | count }}" + # Use 'choose' to run different actions based on what triggered the automation. + - choose: + # Case 1: Weather changed + - conditions: + - condition: trigger + id: weather_change + # Also check if the new weather state is in our list of trigger states + - condition: template + value_template: "{{ trigger.to_state.state in !input weather_trigger_states }}" + sequence: + - service: notify.notify + data: + target: !input notify_target + message: "It's {{ trigger.to_state.state }} outside! {{ open_count }} opening(s) are open: {{ open_entities | join(', ') }}." + title: "Open Window/Door Alert" - {% set msg = !input custom_message | replace('{{ trigger_state }}', trigger_state) %} - {% set message_parts = [] %} - {% for entity_name in open_entities %} - {% set message_parts = message_parts + [msg | replace('{{ entity_name }}', entity_name)] %} - {% endfor %} - {{ message_parts | join("\n") }} - {% endif %} \ No newline at end of file + # Case 2: Temperature is too high + - conditions: + - condition: trigger + id: temp_hot + sequence: + - service: notify.notify + data: + target: !input notify_target + message: "It's getting warm ({{ states(!input temperature_sensor) }}°C)! {{ open_count }} opening(s) are open: {{ open_entities | join(', ') }}." + title: "Open Window/Door Alert" + + # Case 3: Temperature is too low + - conditions: + - condition: trigger + id: temp_cold + sequence: + - service: notify.notify + data: + target: !input notify_target + message: "It's getting cold ({{ states(!input temperature_sensor) }}°C)! {{ open_count }} opening(s) are open: {{ open_entities | join(', ') }}." + title: "Open Window/Door Alert" \ No newline at end of file