{
  "id": "bedroom_warmth_watchdog",
  "alias": "Bedroom warmth watchdog",
  "description": "Safety net for 'Bedroom night warmth': if input_boolean.bedroom_heat_boost is on but no warmth run is in progress (HA restart, automation reload, lost NLE/MQTT command), put the Upstairs Nest back to the pre-boost setpoint (never raises) and clear the flag. Checks every 5 min, at HA start and after automation reloads; failed restores retry next check and raise a persistent notification. Managed from the infra repo, 2026-11-22.",
  "triggers": [
    {
      "trigger": "time_pattern",
      "minutes": "/5"
    },
    {
      "trigger": "homeassistant",
      "event": "start"
    },
    {
      "trigger": "event",
      "event_type": "automation_reloaded"
    }
  ],
  "conditions": [
    {
      "condition": "state",
      "entity_id": "input_boolean.bedroom_heat_boost",
      "state": "on"
    },
    {
      "condition": "template",
      "value_template": "{{ not is_state('automation.bedroom_night_warmth','on') or state_attr('automation.bedroom_night_warmth','current') | int(0) == 0 }}"
    }
  ],
  "actions": [
    {
      "delay": "00:00:20"
    },
    {
      "condition": "template",
      "value_template": "{{ is_state('input_boolean.bedroom_heat_boost','on') and (state_attr('automation.bedroom_night_warmth','current') | int(0) == 0) }}"
    },
    {
      "if": [
        {
          "condition": "template",
          "value_template": "{{ (state_attr('climate.upstairs_nest','temperature') | float(0)) > (states('input_number.bedroom_pre_boost_setpoint') | float(63)) + 0.25 }}"
        }
      ],
      "then": [
        {
          "action": "input_text.set_value",
          "target": {
            "entity_id": "input_text.bedroom_auto_write"
          },
          "data": {
            "value": "{{ now().timestamp() }}"
          }
        },
        {
          "action": "climate.set_temperature",
          "target": {
            "entity_id": "climate.upstairs_nest"
          },
          "data": {
            "temperature": "{{ states('input_number.bedroom_pre_boost_setpoint') | float(63) }}"
          }
        },
        {
          "wait_template": "{{ ((state_attr('climate.upstairs_nest','temperature') | float(99)) - (states('input_number.bedroom_pre_boost_setpoint') | float(63))) | abs < 0.25 }}",
          "timeout": "00:01:00",
          "continue_on_timeout": true
        }
      ]
    },
    {
      "if": [
        {
          "condition": "template",
          "value_template": "{{ (state_attr('climate.upstairs_nest','temperature') | float(99)) <= (states('input_number.bedroom_pre_boost_setpoint') | float(63)) + 0.25 }}"
        }
      ],
      "then": [
        {
          "action": "input_boolean.turn_off",
          "target": {
            "entity_id": "input_boolean.bedroom_heat_boost"
          }
        },
        {
          "action": "logbook.log",
          "data": {
            "name": "Bedroom night warmth",
            "entity_id": "climate.upstairs_nest",
            "message": "Watchdog: orphaned boost cleared, Upstairs at {{ state_attr('climate.upstairs_nest','temperature') }}F"
          }
        }
      ],
      "else": [
        {
          "action": "logbook.log",
          "data": {
            "name": "Bedroom night warmth",
            "entity_id": "climate.upstairs_nest",
            "message": "Watchdog: Upstairs still {{ state_attr('climate.upstairs_nest','temperature') }}F, restore to {{ states('input_number.bedroom_pre_boost_setpoint') | float(63) }}F failed, retrying in 5 min"
          }
        },
        {
          "action": "persistent_notification.create",
          "data": {
            "notification_id": "bedroom_warmth_watchdog",
            "title": "Bedroom heat stuck",
            "message": "Upstairs Nest is at {{ state_attr('climate.upstairs_nest','temperature') }}F after a warmth pulse and won't go back to {{ states('input_number.bedroom_pre_boost_setpoint') | float(63) }}F. Check NLE/MQTT."
          }
        }
      ]
    }
  ],
  "mode": "single",
  "max_exceeded": "silent"
}
