Versatile Thermostat (VTherm)

repository·main·Indexed 22 days ago

https://github.com/jmcollin78/versatile_thermostat

A Home Assistant custom component that provides an intelligent, adaptive virtual thermostat layer over heating and cooling devices such as switches, climate entities, or valves. It features advanced control algorithms like TPI and auto-TPI for energy optimization, native event management for presence and window detection, and a plugin mechanism (v10.0+). It includes specialized tools for TRV recalibration, boiler power activation thresholds, and a log collector for troubleshooting.

Tokens
83K
Snippets
107
Records
346
Agent score
77%

What's inside Versatile Thermostat

  1. Overview of Versatile Thermostat (VTherm)

    main

    Versatile Thermostat (VTherm) is a highly configurable virtual thermostat for Home Assistant. It transforms various heating equipment—such as radiators, air conditioners, heat pumps, and switches—into intelligent, adaptive systems.

    Key capabilities include:

    • Centralized Control: Consolidate and manage multiple different heating systems from a single interface.
    • Energy Optimization: Uses advanced algorithms like TPI (Time Proportional Integral) and auto-TPI to adapt to your home's thermal characteristics and your habits.
    • Native Event Management: Automatically handles typical heating events like presence detection (nobody home?), room activity, window opening, and power load shedding, reducing the need for complex manual automations.
    • Plugin Mechanism: (Available in v10.0+) Allows the use of external integrations as plugins.
  2. Overview of VTherm (Versatile Thermostat)

    main

    VTherm is a highly configurable virtual thermostat for Home Assistant that transforms any heating device (radiators, air conditioners, heat pumps, etc.) into an intelligent, adaptive system. It centralizes control of multiple heating systems and optimizes energy consumption using advanced algorithms like TPI and auto-TPI.

    Key benefits include:

    • Simplified Automation: Native handling of heating-related events like presence detection, room activity, open windows, and power loss, reducing the need for complex custom scripts.
    • Adaptive Learning: The thermostat learns your home's thermal characteristics and your habits to provide optimal comfort and reduce heating bills.
    • Device Consolidation: Allows managing diverse heating hardware through a unified interface.
  3. Overview of Versatile Thermostat

    main

    Versatile Thermostat (VTherm) is a highly configurable virtual thermostat for Home Assistant. It transforms any heating or cooling device (radiators, air conditioning, heat pumps, etc.) into an intelligent, adaptive system.

    Key capabilities include:

    • Centralized Control: Consolidate and manage multiple different heating systems centrally.
    • Energy Optimization: Uses advanced algorithms like TPI (Time Proportional Integral) and auto-TPI to optimize energy consumption.
    • Adaptive Learning: The thermostat learns your home's thermal characteristics and your habits.
    • Native Event Management: Automatically handles common heating events such as presence detection (is anyone home?), activity detection, open windows, and energy consumption limits, reducing the need for complex manual automations.
  4. Key features of Versatile Thermostat

    main

    Versatile Thermostat provides advanced management capabilities for Home Assistant heating and cooling systems, including:

    • Preset Management: Full use of preset modes, with the ability to disable presets when temperature is set manually.
    • Automation Triggers: Automatically turn thermostats off/on or change presets based on window/door status or occupancy/activity detection.
    • TPI Algorithm: Uses a Time Proportional Interval (TPI) algorithm for precise temperature regulation.
    • Load Shedding: Manages total power consumption by setting a hidden "power" preset when a defined maximum power is exceeded.
    • Presence Management: Dynamically modifies preset temperatures based on presence sensors.
    • Centralized Control: Ability to control all Versatile Thermostats simultaneously (e.g., stop all, set all to frost protection, or force heating/cooling modes).
    • Integration Actions: Allows external integrations to force presence/non-presence via services or dynamically change preset temperatures and security settings.
  5. What is blocked and allowed when the thermostat is locked

    main

    When a thermostat is locked, the system distinguishes between external requests (which are blocked) and internal logic (which remains active).

    Blocked (External calls)

    Depending on your configuration, the following are prevented:

    • HVAC mode changes (including on/off)
    • Target temperature changes
    • Preset changes and VTherm preset configuration services
    • HA action service calls

    Allowed (Internal VTherm logic)

    The following operations are always active and bypass the lock to ensure safety and core functionality:

    • Window detection and actions: (e.g., turning off or switching to eco/frost when a window opens, and restoring behavior when closed).
    • Safety protections: Overheat/frost safety presets and handling.
    • Power and overpowering management: Including PRESET_POWER behavior.
    • Automatic regulation algorithms: TPI / PI / PROP and the control loop.
    • Coordination: Central/parent/child coordination and other internal VTherm automations.
  6. What is self-regulation and when to use it

    main

    Self-regulation is a feature available for VTherms of type over_climate. It allows the thermostat to automatically adjust its control strategy to ensure the room temperature reaches the desired setpoint.

    There are two primary modes of operation depending on your hardware:

    1. Direct Valve Control: Used if your underlying device is a TRV (Thermostatic Radiator Valve) and the valve is directly controllable in Home Assistant (e.g., Sonoff TRVZB). In this mode, the VTherm directly manipulates the valve opening using a TPI (Time Proportional Integral) algorithm.
    2. Setpoint Adjustment: Used for other devices. Instead of controlling a valve, Versatile Thermostat adjusts the temperature setpoint sent to the underlying climate device to compensate for temperature deviations.
  7. What is Auto TPI and when to use it

    main

    Auto TPI (Automatic Learning) is a feature that allows the thermostat to automatically learn the thermal characteristics of a room by adjusting two key coefficients:

    • Kint (Internal Inertia): Controls heating responsiveness. It is adjusted during temperature rise when the deviation from the setpoint is significant (> 0.05°C) and heating is not at 100% power.
    • Kext (External Insulation): Compensates for heat loss. It is adjusted during stabilization around the setpoint when the deviation is small (< 1°C).

    Target Hardware: This feature is primarily designed for Switch (On/Off) heating systems (e.g., electric radiators, boilers, underfloor heating, or pellet stoves). It is not recommended for thermostatic radiator valves (TRV) due to their non-linearity.

  8. Synchronize thermostat and underlying states with check_initial_state()

    main

    The check_initial_state() method is called after all underlying entities are initialized. Its purpose is to ensure the thermostat and its underlying entities are in a synchronized state to prevent energy waste or failed heating/cooling.

    Implementation Details

    UnderlyingClimate

    Prevents the climate device from being ON while the thermostat is OFF (or vice versa):

    • If thermostat is VThermHvacMode_OFF but climate is active $\rightarrow$ turn climate OFF.
    • If thermostat is ON but climate is OFF $\rightarrow$ turn climate ON to match thermostat mode.

    UnderlyingValveRegulation

    Synchronizes valve opening with the thermostat's heating/cooling requirements:

    • If the thermostat should be ON (not sleeping and mode $\neq$ OFF) but the valve is closed $\rightarrow$ set valve to valve_open_percent.
    • If the thermostat should be OFF but the valve is open $\rightarrow$ set valve to _opening_threshold and set climate mode to VThermHvacMode_OFF.
  9. Decision logic for selecting a VTherm type

    main

    When deciding which VTherm type to implement, follow this priority order based on your equipment:

    1. Priority 1 (Best Regulation): If you have a controllable TRV exposed via a number entity (e.g., Shelly TRV), use over_valve.
    2. Priority 2: If you have an electric radiator controlled via a switch entity, use over_switch. Regulation is managed by VTherm based on your local thermometer.
    3. Priority 3 (Fallback): For all other cases (like HVAC systems), use over_climate. You retain your original climate entity, and VTherm manages its target temperature and on/off state.

    Regulation Strategy Tip:

    • If the equipment has reliable built-in regulation (e.g., standard HVAC), use over_climate.
    • For TRVs with a controllable valve in Home Assistant, use over_climate with Direct Valve Control self-regulation for the best results.
  10. Detect thermostat-originated changes

    main

    To prevent feedback loops when the thermostat updates an entity, compare the new state received from Home Assistant against the last value sent by the thermostat. If the values match, the change was likely triggered by the thermostat itself and should be ignored. If they differ, the change originated from an external source (e.g., a user or an automation) and should be processed.

    # In UnderlyingClimate._underlying_changed()
    new_target_temp = new_state.attributes.get("temperature")
    
    # Compare with last sent temperature to detect if change came from thermostat
    if new_target_temp == self._last_sent_temperature:
        # Change was from thermostat, ignore
        return
    
    # Otherwise it's from external source (user interaction, automation, etc.)
  11. How Regime Change Detection works in Auto TPI

    main

    The Auto TPI algorithm includes a mechanism to detect systemic thermal regime changes (e.g., seasonal changes, insulation modifications, or adding a radiator). This allows the model to accelerate adaptation when a systematic bias is detected in learning errors.

    Mechanism Details

    1. Error Tracking: During successful indoor learning cycles, the error (adjusted_theoretical - real_rise) is stored in a sliding window of the last 20 errors (state.recent_errors).
    2. Statistical Detection: The system applies a Student's t-test on the last 10 errors. If the t-statistic is > 2.0 (95% confidence), a regime change is detected, indicating a significant systematic bias.
    3. Adaptive Response: When detected, the state.regime_change_detected flag is activated. The method _get_adaptive_alpha uses this flag to temporarily boost the alpha coefficient (up to 3x the base value, capped at 0.15). This allows the EMA coefficient to adapt more rapidly to new conditions.
    4. Reset: The regime_change_detected flag is reset after the first learning cycle that utilizes the boosted alpha, ensuring the boost is temporary.

    Benefits

    • Rapid Adaptation: Quickly adjusts to sudden changes in thermal conditions.
    • Safety: The boost is limited (max 15%) and temporary.
    • Robustness: The statistical test prevents false detections caused by noise or isolated anomalies.
  12. Use Continuous Kext Learning

    main

    Continuous Kext Learning allows the thermostat to adapt to long-term climate changes and insulation variations in the background without manual sessions.

    • Mechanism: It monitors $K_{ext}$ precision and applies micro-corrections using an Exponential Moving Average (EWMA) when systematic deviations are detected.
    • Safety: Only active when power is not saturated and the system is stable.
    • Configuration: You can adjust the Alpha factor (default 0.04). Higher values increase adaptation speed but increase sensitivity to noise.
    • Requirement: Requires at least one successful outdoor learning cycle from a standard session to have been performed first.