{
  "id": "bedroom_night_warmth_reset",
  "alias": "Bedroom night warmth reset",
  "description": "Night ceiling + room-too-warm step-down for the Upstairs Nest (feeds the bedroom). 20:00-09:00: Upstairs target above 68F (any source) -> 68F (68 since 2026-11-24; 65 until 2026-11-22, 64 for 11/22-11/23; owner 11/23: let manual go to 65 - automations never set above 64 anyway: pulse cap 64, restores go to the saved base). 20:00-09:00, only while the boost flag is on (owner 11/23: a hand-set temperature stays put; was any source 11/21-11/23): the bedroom above 64F for 10 min (re-checked every 15 min) -> Upstairs -1F, never raising it; floor = pre-boost setpoint (input_number.bedroom_pre_boost_setpoint) while input_boolean.bedroom_heat_boost is on, else 63F. Clears the boost flag at the pre-boost setpoint. 09:00 with boost on -> back down to the pre-boost setpoint (never raises). Managed from the infra repo, 2026-11-20 (2026-11-21: pre-boost restore, 64F threshold, applies to manual setpoints too). 2026-11-24: night ceiling raised 65 -> 68F as a hard safety max for any source (owner: nights drop to 15-25F outside and the house must be allowed to go higher); rescue burns also cap at 68. 2026-11-24 (ratchet): room > 64F with no burn running and input_number.bedroom_ratchet_steps > 0 -> Upstairs -0.5F and one step off (only ever undoes what the burns added); 09:00 -> undo whatever is left, counter to 0.",
  "triggers": [
    {
      "trigger": "numeric_state",
      "entity_id": "sensor.bedroom_temperature",
      "above": 64,
      "for": {
        "minutes": 10
      },
      "id": "warm"
    },
    {
      "trigger": "time_pattern",
      "minutes": "/15",
      "id": "warm"
    },
    {
      "trigger": "time",
      "at": "09:00:00",
      "id": "morning"
    },
    {
      "trigger": "state",
      "entity_id": "climate.upstairs_nest",
      "attribute": "temperature",
      "id": "ceiling"
    },
    {
      "trigger": "time",
      "at": "20:00:00",
      "id": "ceiling"
    }
  ],
  "conditions": [],
  "actions": [
    {
      "variables": {
        "up": "climate.upstairs_nest",
        "base": "{{ states('input_number.bedroom_pre_boost_setpoint') | float(64) }}"
      }
    },
    {
      "choose": [
        {
          "conditions": [
            {
              "condition": "time",
              "after": "20:00:00",
              "before": "09:00:00"
            },
            {
              "condition": "template",
              "value_template": "{{ state_attr(up, 'temperature') | float(0) > 68 }}"
            }
          ],
          "sequence": [
            {
              "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": 68
              }
            },
            {
              "action": "logbook.log",
              "data": {
                "name": "Bedroom night warmth",
                "message": "Upstairs above the 68F night safety max, set back to 68F",
                "entity_id": "climate.upstairs_nest"
              }
            }
          ]
        },
        {
          "conditions": [
            {
              "condition": "trigger",
              "id": "morning"
            },
            {
              "condition": "template",
              "value_template": "{{ (states('input_number.bedroom_ratchet_steps') | int(0)) > 0 }}"
            }
          ],
          "sequence": [
            {
              "variables": {
                "cur": "{{ state_attr(up,'temperature') | float(0) }}",
                "target": "{% set from = (states('input_number.bedroom_pre_boost_setpoint') | float(63)) if is_state('input_boolean.bedroom_heat_boost','on') else (state_attr(up,'temperature') | float(0)) %}{{ [ from - (states('input_number.bedroom_ratchet_steps') | int(0)) * 0.5, state_attr(up,'temperature') | float(0) ] | min }}",
                "undo": "{{ (states('input_number.bedroom_ratchet_steps') | int(0)) * 0.5 }}"
              }
            },
            {
              "if": [
                {
                  "condition": "template",
                  "value_template": "{{ cur > 0 and target | float < cur }}"
                }
              ],
              "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": "{{ target }}"
                  }
                }
              ]
            },
            {
              "action": "input_number.set_value",
              "target": {
                "entity_id": "input_number.bedroom_pre_boost_setpoint"
              },
              "data": {
                "value": "{{ target }}"
              }
            },
            {
              "action": "input_number.set_value",
              "target": {
                "entity_id": "input_number.bedroom_ratchet_steps"
              },
              "data": {
                "value": 0
              }
            },
            {
              "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": "09:00: undid tonight's +{{ undo }}F for the bedroom, Upstairs back to {{ target }}F"
              }
            }
          ]
        },
        {
          "conditions": [
            {
              "condition": "trigger",
              "id": "morning"
            },
            {
              "condition": "state",
              "entity_id": "input_boolean.bedroom_heat_boost",
              "state": "on"
            }
          ],
          "sequence": [
            {
              "if": [
                {
                  "condition": "template",
                  "value_template": "{{ state_attr(up, 'temperature') | float(0) > base | float }}"
                }
              ],
              "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": "{{ base }}"
                  }
                }
              ]
            },
            {
              "action": "input_boolean.turn_off",
              "target": {
                "entity_id": "input_boolean.bedroom_heat_boost"
              }
            }
          ]
        },
        {
          "conditions": [
            {
              "condition": "trigger",
              "id": "warm"
            },
            {
              "condition": "time",
              "after": "20:00:00",
              "before": "09:00:00"
            },
            {
              "condition": "numeric_state",
              "entity_id": "sensor.bedroom_temperature",
              "above": 64
            },
            {
              "condition": "template",
              "value_template": "{{ not is_state('automation.bedroom_night_warmth', 'on') or state_attr('automation.bedroom_night_warmth', 'current') | int(0) == 0 }}"
            },
            {
              "condition": "state",
              "entity_id": "input_boolean.bedroom_heat_boost",
              "state": "on"
            }
          ],
          "sequence": [
            {
              "variables": {
                "boost": "{{ is_state('input_boolean.bedroom_heat_boost','on') }}",
                "floor": "{{ (base | float) if is_state('input_boolean.bedroom_heat_boost','on') else 63 }}",
                "cur": "{{ state_attr(up, 'temperature') | float(0) }}",
                "new_target": "{% set cur = state_attr(up, 'temperature') | float(0) %}{% set fl = (base | float) if is_state('input_boolean.bedroom_heat_boost','on') else 63 %}{{ [ [cur - 1, fl] | max, cur ] | min }}"
              }
            },
            {
              "condition": "template",
              "value_template": "{{ cur > 0 and new_target | float < cur }}"
            },
            {
              "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": "{{ new_target }}"
              }
            },
            {
              "if": [
                {
                  "condition": "template",
                  "value_template": "{{ boost and new_target | float <= base | float }}"
                }
              ],
              "then": [
                {
                  "action": "input_boolean.turn_off",
                  "target": {
                    "entity_id": "input_boolean.bedroom_heat_boost"
                  }
                }
              ]
            },
            {
              "action": "logbook.log",
              "data": {
                "name": "Bedroom night warmth",
                "message": "the bedroom {{ states('sensor.bedroom_temperature') }}F > 64F: Upstairs {{ cur }}F -> {{ new_target }}F ({{ 'boost' if boost else 'manual/other' }} setpoint)",
                "entity_id": "climate.upstairs_nest"
              }
            }
          ]
        },
        {
          "conditions": [
            {
              "condition": "trigger",
              "id": "warm"
            },
            {
              "condition": "time",
              "after": "20:00:00",
              "before": "09:00:00"
            },
            {
              "condition": "numeric_state",
              "entity_id": "sensor.bedroom_temperature",
              "above": 64
            },
            {
              "condition": "state",
              "entity_id": "input_boolean.bedroom_heat_boost",
              "state": "off"
            },
            {
              "condition": "template",
              "value_template": "{{ not is_state('automation.bedroom_night_warmth', 'on') or state_attr('automation.bedroom_night_warmth', 'current') | int(0) == 0 }}"
            },
            {
              "condition": "template",
              "value_template": "{{ (states('input_number.bedroom_ratchet_steps') | int(0)) > 0 }}"
            }
          ],
          "sequence": [
            {
              "variables": {
                "target": "{{ (state_attr(up,'temperature') | float(0)) - 0.5 }}"
              }
            },
            {
              "condition": "template",
              "value_template": "{{ target | float > 50 }}"
            },
            {
              "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": "{{ target }}"
              }
            },
            {
              "action": "input_number.set_value",
              "target": {
                "entity_id": "input_number.bedroom_pre_boost_setpoint"
              },
              "data": {
                "value": "{{ target }}"
              }
            },
            {
              "action": "input_number.set_value",
              "target": {
                "entity_id": "input_number.bedroom_ratchet_steps"
              },
              "data": {
                "value": "{{ [ (states('input_number.bedroom_ratchet_steps') | int(0)) - 1, 0 ] | max }}"
              }
            },
            {
              "action": "logbook.log",
              "data": {
                "name": "Bedroom night warmth",
                "entity_id": "climate.upstairs_nest",
                "message": "the bedroom {{ states('sensor.bedroom_temperature') }}F > 64F: Upstairs stepped down 0.5 to {{ target }}F ({{ (states('input_number.bedroom_ratchet_steps') | int(0)) * 0.5 }}F of tonight's raise left)"
              }
            }
          ]
        }
      ]
    }
  ],
  "mode": "queued",
  "max": 3
}
