{
  "id": "bedroom_night_warmth",
  "alias": "Bedroom night warmth",
  "description": "Rescue heating for the bedroom (floor 62F), 20:00-09:00. Room < 62F for 2 min (re-checked every 15 min) -> remember the Upstairs setpoint (input_number.bedroom_pre_boost_setpoint), set Upstairs to its OWN current reading + 2F (rounded up to 0.5, at least base+0.5, cap 68 = night safety max) so the furnace really starts, burn for a calculated time, put the setpoint back, settle 20 min. Burn minutes = 5 + (63 - room)/0.065, clamped 10-60 (40 until 2026-11-24 later; raised for 15-25F nights) (fit 2026-11-24 from 20 Upstairs cycles: ~0.065F room rise per furnace minute after ~5 min warm-up, peak 10-15 min after the burner stops). Target room 63F. Repeats while room < 62.5F, max 2 per run. Runs even after a hand-set (the hand-set value is what gets restored); a hand change DURING a burn ends it without restoring over it. Gives up a burn if the furnace has not started within 5 min. Restores are confirmed (retry once; else boost flag stays on for 'Bedroom warmth watchdog'). No burns while the room sensor is stale (45 min). The night ceiling in '_reset' stands aside while input_boolean.bedroom_heat_boost is on. History: 2026-11-22 fixed 8-min pulses to a fixed 64F cap; 2026-11-24 rewritten because the Upstairs Nest (master bedroom) read >= 64 on cold nights so no pulse ever started the furnace. 2026-11-24 (ratchet): each completed burn also raises the normal night setting (the saved base the burn restores to) by 0.5F, max 68, counted in input_number.bedroom_ratchet_steps; '_reset' steps it back down 0.5F per 15 min while the room is > 64F, and undoes what is left at 09:00. A hand change zeroes the counter (the hand value becomes the base).",
  "triggers": [
    {
      "trigger": "numeric_state",
      "entity_id": "sensor.bedroom_temperature",
      "below": 62,
      "for": {
        "minutes": 2
      }
    },
    {
      "trigger": "time",
      "at": "20:00:00"
    },
    {
      "trigger": "time_pattern",
      "minutes": "/15"
    },
    {
      "trigger": "homeassistant",
      "event": "start"
    }
  ],
  "conditions": [
    {
      "condition": "time",
      "after": "20:00:00",
      "before": "09:00:00"
    },
    {
      "condition": "numeric_state",
      "entity_id": "sensor.bedroom_temperature",
      "below": 62
    },
    {
      "condition": "state",
      "entity_id": "climate.upstairs_nest",
      "state": "heat"
    },
    {
      "condition": "template",
      "value_template": "{% set s = states.sensor.bedroom_temperature %}{{ s is not none and s.state not in ['unknown','unavailable'] and (now() - s.last_reported).total_seconds() < 2700 }}"
    }
  ],
  "actions": [
    {
      "variables": {
        "settle_min": 20,
        "max_pulses": 2
      }
    },
    {
      "action": "input_number.set_value",
      "target": {
        "entity_id": "input_number.bedroom_pre_boost_setpoint"
      },
      "data": {
        "value": "{{ state_attr('climate.upstairs_nest','temperature') | float(63) }}"
      }
    },
    {
      "repeat": {
        "while": [
          {
            "condition": "template",
            "value_template": "{{ repeat.index <= max_pulses and states('sensor.bedroom_temperature') | float(99) < 62.5 }}"
          },
          {
            "condition": "time",
            "after": "20:00:00",
            "before": "09:00:00"
          },
          {
            "condition": "template",
            "value_template": "{% set s = states.sensor.bedroom_temperature %}{{ s is not none and s.state not in ['unknown','unavailable'] and (now() - s.last_reported).total_seconds() < 2700 }}"
          }
        ],
        "sequence": [
          {
            "variables": {
              "start": "{{ states('sensor.bedroom_temperature') | float }}",
              "burn_min": "{% set r = states('sensor.bedroom_temperature') | float(62) %}{{ [[ (5 + (63 - r) / 0.065) | round(0), 10 ] | max, 60 ] | min | int }}",
              "pulse_sp": "{% set c = state_attr('climate.upstairs_nest','current_temperature') | float(64) %}{% set b = states('input_number.bedroom_pre_boost_setpoint') | float(63) %}{{ [[ ((c + 2) * 2) | round(0, 'ceil') / 2, b + 0.5 ] | max, 68 ] | min }}"
            }
          },
          {
            "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": "{{ pulse_sp }}"
            }
          },
          {
            "action": "input_boolean.turn_on",
            "target": {
              "entity_id": "input_boolean.bedroom_heat_boost"
            }
          },
          {
            "wait_template": "{{ state_attr('climate.upstairs_nest','hvac_action') == 'heating' }}",
            "timeout": "00:05:00",
            "continue_on_timeout": true
          },
          {
            "if": [
              {
                "condition": "template",
                "value_template": "{{ not wait.completed }}"
              }
            ],
            "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": "{{ not wait.completed }}"
                  }
                ],
                "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))) | abs < 0.25 }}"
                  }
                ],
                "then": [
                  {
                    "action": "input_boolean.turn_off",
                    "target": {
                      "entity_id": "input_boolean.bedroom_heat_boost"
                    }
                  }
                ],
                "else": [
                  {
                    "action": "logbook.log",
                    "data": {
                      "name": "Bedroom night warmth",
                      "entity_id": "climate.upstairs_nest",
                      "message": "Pulse {{ repeat.index }} no-start: restore to {{ states('input_number.bedroom_pre_boost_setpoint') | float(63) }}F NOT confirmed (Upstairs at {{ state_attr('climate.upstairs_nest','temperature') }}F); boost flag left on for the watchdog"
                    }
                  }
                ]
              },
              {
                "action": "logbook.log",
                "data": {
                  "name": "Bedroom night warmth",
                  "entity_id": "climate.upstairs_nest",
                  "message": "Pulse {{ repeat.index }}: furnace did not start in 5 min (Nest reads {{ state_attr('climate.upstairs_nest','current_temperature') }}F, asked {{ pulse_sp }}F); setpoint restored, giving up this run"
                }
              },
              {
                "stop": "furnace did not start"
              }
            ]
          },
          {
            "action": "logbook.log",
            "data": {
              "name": "Bedroom night warmth",
              "entity_id": "climate.upstairs_nest",
              "message": "Pulse {{ repeat.index }}: the bedroom {{ start }}F < 62F, burning {{ burn_min }} min to reach ~63F (Upstairs {{ states('input_number.bedroom_pre_boost_setpoint') | float(63) }} -> {{ pulse_sp }}F)"
            }
          },
          {
            "wait_template": "{{ ((state_attr('climate.upstairs_nest','temperature') | float(0)) - (pulse_sp | float)) | abs > 0.25 }}",
            "timeout": {
              "minutes": "{{ burn_min }}"
            },
            "continue_on_timeout": true
          },
          {
            "if": [
              {
                "condition": "template",
                "value_template": "{{ wait.completed }}"
              }
            ],
            "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": "Pulse {{ repeat.index }}: Upstairs changed to {{ state_attr('climate.upstairs_nest','temperature') }}F during the burn, burn ended, setpoint left as set"
                }
              },
              {
                "stop": "manual change"
              }
            ]
          },
          {
            "if": [
              {
                "condition": "template",
                "value_template": "{{ (states('input_number.bedroom_pre_boost_setpoint') | float(63)) < 68 }}"
              }
            ],
            "then": [
              {
                "action": "input_number.set_value",
                "target": {
                  "entity_id": "input_number.bedroom_pre_boost_setpoint"
                },
                "data": {
                  "value": "{{ [ (states('input_number.bedroom_pre_boost_setpoint') | float(63)) + 0.5, 68 ] | min }}"
                }
              },
              {
                "action": "input_number.set_value",
                "target": {
                  "entity_id": "input_number.bedroom_ratchet_steps"
                },
                "data": {
                  "value": "{{ (states('input_number.bedroom_ratchet_steps') | int(0)) + 1 }}"
                }
              },
              {
                "action": "logbook.log",
                "data": {
                  "name": "Bedroom night warmth",
                  "entity_id": "climate.upstairs_nest",
                  "message": "Pulse {{ repeat.index }}: the bedroom needed heat, normal night setting raised to {{ states('input_number.bedroom_pre_boost_setpoint') | float(63) }}F (+{{ (states('input_number.bedroom_ratchet_steps') | int(0)) * 0.5 }}F tonight)"
                }
              }
            ]
          },
          {
            "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": "{{ not wait.completed }}"
              }
            ],
            "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))) | abs < 0.25 }}"
              }
            ],
            "then": [
              {
                "action": "input_boolean.turn_off",
                "target": {
                  "entity_id": "input_boolean.bedroom_heat_boost"
                }
              }
            ],
            "else": [
              {
                "action": "logbook.log",
                "data": {
                  "name": "Bedroom night warmth",
                  "entity_id": "climate.upstairs_nest",
                  "message": "Pulse {{ repeat.index }}: restore to {{ states('input_number.bedroom_pre_boost_setpoint') | float(63) }}F NOT confirmed (Upstairs at {{ state_attr('climate.upstairs_nest','temperature') }}F); boost flag left on for the watchdog"
                }
              }
            ]
          },
          {
            "action": "logbook.log",
            "data": {
              "name": "Bedroom night warmth",
              "entity_id": "climate.upstairs_nest",
              "message": "Pulse {{ repeat.index }} done ({{ burn_min }} min): Upstairs back to {{ states('input_number.bedroom_pre_boost_setpoint') | float(63) }}F, waiting {{ settle_min }} min (room {{ states('sensor.bedroom_temperature') }}F)"
            }
          },
          {
            "delay": {
              "minutes": "{{ settle_min }}"
            }
          }
        ]
      }
    },
    {
      "action": "logbook.log",
      "data": {
        "name": "Bedroom night warmth",
        "entity_id": "climate.upstairs_nest",
        "message": "Run ended: the bedroom {{ states('sensor.bedroom_temperature') }}F"
      }
    }
  ],
  "mode": "single",
  "max_exceeded": "silent"
}
