diff --git a/ha-smart-thermostat-control.yaml b/ha-smart-thermostat-control.yaml index c8e944f..e048295 100644 --- a/ha-smart-thermostat-control.yaml +++ b/ha-smart-thermostat-control.yaml @@ -120,6 +120,17 @@ blueprint: max: 0.0 unit_of_measurement: minutes step: 30.0 + sync_delay: + name: Manual Sync Delay + description: 'Delay before syncing manual temperature changes to prevent cascade loops. (Default = 3s)' + default: 3 + selector: + number: + mode: box + min: 1.0 + max: 10.0 + unit_of_measurement: seconds + step: 1.0 trigger: - platform: state @@ -146,6 +157,8 @@ trigger: entity_id: !input thermostat attribute: temperature id: manual_adjustment + for: + seconds: !input sync_delay - platform: calendar event: end entity_id: !input away_calendar @@ -164,6 +177,9 @@ trigger: variables: thermostat_entities: !input thermostat + triggered_thermostat: "{{ trigger.entity_id }}" + new_temperature: "{{ trigger.to_state.attributes.temperature | float(0) }}" + old_temperature: "{{ trigger.from_state.attributes.temperature | float(0) }}" condition: [] @@ -173,20 +189,39 @@ action: - conditions: - condition: trigger id: manual_adjustment - # Prevent triggering on automation's own changes + # Only sync if the temperature actually changed + - condition: template + value_template: "{{ (new_temperature - old_temperature) | abs > 0.1 }}" + # Check that other thermostats need syncing (have different temps) - condition: template value_template: > - {{ (now() - trigger.from_state.last_changed).total_seconds() > 10 }} + {% set other_thermostats = thermostat_entities | reject('eq', triggered_thermostat) | list %} + {% if other_thermostats | length > 0 %} + {% set needs_sync = namespace(value=false) %} + {% for thermo in other_thermostats %} + {% set thermo_temp = state_attr(thermo, 'temperature') | float(0) %} + {% if (thermo_temp - new_temperature) | abs > 0.1 %} + {% set needs_sync.value = true %} + {% endif %} + {% endfor %} + {{ needs_sync.value }} + {% else %} + false + {% endif %} sequence: - - service: climate.set_temperature - target: - entity_id: !input thermostat - data: - temperature: "{{ trigger.to_state.attributes.temperature }}" + # Only sync to OTHER thermostats (not the one that triggered) + - repeat: + for_each: "{{ thermostat_entities | reject('eq', triggered_thermostat) | list }}" + sequence: + - service: climate.set_temperature + target: + entity_id: "{{ repeat.item }}" + data: + temperature: "{{ new_temperature }}" - service: logbook.log data: name: Smart Thermostat - message: 'Manual Adjustment: All thermostats synced to {{ trigger.to_state.attributes.temperature }}°' + message: 'Manual Adjustment: {{ triggered_thermostat }} changed to {{ new_temperature }}°, syncing to other thermostats' # Priority 1: Windows/Doors Open - Turn OFF heating - conditions: