- platform: template name: "Boost Duration (min)" id: boost_duration min_value: 5 max_value: 120 step: 1 initial_value: 30 optimistic: true
Example: Dynamic Setpoints from Home Assistant
You can allow Home Assistant to set the Eco, Confort, and Boost temperatures by exposing them as number entities in ESPHome. Home Assistant can then adjust these values, and ESPHome will use them for the setpoints.
number:
- platform: template
name: "Hors gel Temperature"
id: hors_gel_temp
min_value: 5
max_value: 10
step: 0.5
initial_value: 7
optimistic: true
- platform: template
name: "Eco Temperature"
id: eco_temp
min_value: 10
max_value: 25
step: 0.5
initial_value: 18
optimistic: true
- platform: template
name: "Confort Temperature"
id: confort_temp
min_value: 15
max_value: 25
step: 0.5
initial_value: 21
optimistic: true
- platform: template
name: "Boost Temperature"
id: boost_temp
min_value: 18
max_value: 28
step: 0.5
initial_value: 23
optimistic: true
interval:
- interval: 1s
then:
- lambda: |-
static unsigned long boost_start = 0;
static bool boost_active = false;
if (id(thermostat_mode).state == "Boost") {
if (!boost_active) {
boost_start = millis();
boost_active = true;
}
id(heater_thermostat).set_target_temperature(id(boost_temp).state);
id(heater_thermostat).turn_on();
unsigned long boost_duration_ms = (unsigned long)(id(boost_duration).state * 60 * 1000);
if (millis() - boost_start > boost_duration_ms) {
// End boost, switch to previous mode or Eco by default
id(thermostat_mode).publish_state("Eco");
boost_active = false;
}
} else {
boost_active = false;
if (id(thermostat_mode).state == "Hors gel") {
id(heater_thermostat).set_target_temperature(id(hors_gel_temp).state);
id(heater_thermostat).turn_on();
} else if (id(thermostat_mode).state == "Eco") {
id(heater_thermostat).set_target_temperature(id(eco_temp).state);
id(heater_thermostat).turn_on();
} else if (id(thermostat_mode).state == "Confort") {
id(heater_thermostat).set_target_temperature(id(confort_temp).state);
id(heater_thermostat).turn_on();
} else {
id(heater_thermostat).turn_off();
}
}
How it works:
- Home Assistant can change the Eco, Confort, and Boost temperatures from the UI or automations.
- ESPHome uses the current value of each number entity as the setpoint for the selected mode.
Full Example: ESPHome YAML for Sonoff R2 Thermostat with Modes
Below is a complete ESPHome YAML configuration for a Sonoff R2 acting as a thermostat, using a Xiaomi temperature sensor via MQTT, and supporting remote mode selection (Off, Eco, Confort, Boost) from Home Assistant. All logic runs on the Sonoff R2.
esphome:
name: sonoff_thermostat
platform: ESP8266
board: esp01_1m
wifi:
ssid: "YOUR_WIFI_SSID"
password: "YOUR_WIFI_PASSWORD"
logger:
api:
ota:
mqtt:
broker: 192.168.1.100
username: your_mqtt_user
password: your_mqtt_password
sensor:
- platform: mqtt_subscribe
name: "Room Temperature"
id: room_temp
topic: "zigbee2mqtt/your_sensor"
unit_of_measurement: "°C"
value_template: "{{ value_json.temperature }}"
switch:
- platform: gpio
pin: 12
id: relay
name: "Heater Relay"
select:
- platform: template
name: "Thermostat Mode"
id: thermostat_mode
options:
- "Off"
- "Hors gel"
- "Eco"
- "Confort"
- "Boost"
initial_option: "Eco"
optimistic: true
climate:
- platform: thermostat
name: "Heater Thermostat"
id: heater_thermostat
sensor: room_temp
min_temperature: 5
max_temperature: 30
heat_action:
- switch.turn_on: relay
idle_action:
- switch.turn_off: relay
interval:
- interval: 1s
then:
- lambda: |-
// Setpoint logic based on mode
if (id(thermostat_mode).state == "Eco") {
id(heater_thermostat).set_target_temperature(18);
id(heater_thermostat).turn_on();
} else if (id(thermostat_mode).state == "Confort") {
id(heater_thermostat).set_target_temperature(21);
id(heater_thermostat).turn_on();
} else if (id(thermostat_mode).state == "Boost") {
id(heater_thermostat).set_target_temperature(23);
id(heater_thermostat).turn_on();
// Optionally, add a timer for boost mode
} else {
id(heater_thermostat).turn_off();
}
# Optionally, add a timer for Boost mode (example: 30 minutes)
# See ESPHome docs for advanced timer logic if needed
How it works:
- The Sonoff R2 subscribes to the Xiaomi sensor temperature via MQTT.
- The relay controls the heater.
- The
selectexposes the mode (Off/Eco/Confort/Boost) to Home Assistant. - The
intervalautomation updates the thermostat setpoint and state based on the selected mode. - All logic is local to the Sonoff R2; Home Assistant only changes the mode.
ESP-Home Sonoff R2 Thermostat with MQTT Temperature Sensor
Advanced: Control Modes (Off / Eco / Confort / Boost) from Home Assistant
If you want the thermostat logic (when to turn the heater ON/OFF) to run inside the Sonoff R2 (ESPHome), but still be able to set the mode (Off, Eco, Confort, Boost) remotely from Home Assistant, follow these guidelines:
1. Add a Select or Input Mode in ESPHome
- Use the
selectortemplatecomponent in ESPHome to define the current mode (Off, Eco, Confort, Boost). - Expose this as a selectable entity to Home Assistant.
2. Use Automations in ESPHome
- In your ESPHome YAML, use automations to set the target temperature or behavior based on the selected mode.
- Example: Eco = 18°C, Confort = 21°C, Boost = 23°C for 30 minutes, Off = always off.
3. Home Assistant Integration
- In Home Assistant, you will see the mode selector as an entity. You can change the mode from the UI or automations.
- No need for Versatile Thermostat in Home Assistant; all logic is in ESPHome.
4. Example ESPHome YAML Snippet
select:
- platform: template
name: "Thermostat Mode"
id: thermostat_mode
options:
- "Off"
- "Eco"
- "Confort"
- "Boost"
initial_option: "Eco"
optimistic: true
climate:
- platform: thermostat
name: "Heater Thermostat"
sensor: room_temp
min_temperature: 5
max_temperature: 30
heat_action:
- switch.turn_on: relay
idle_action:
- switch.turn_off: relay
on_boot:
- lambda: |-
if (id(thermostat_mode).state == "Eco") {
id(heater_thermostat).set_target_temperature(18);
} else if (id(thermostat_mode).state == "Confort") {
id(heater_thermostat).set_target_temperature(21);
} else if (id(thermostat_mode).state == "Boost") {
id(heater_thermostat).set_target_temperature(23);
// Add timer logic for boost if needed
} else {
id(heater_thermostat).turn_off();
}
automation:
- alias: Set Thermostat Mode
trigger:
- platform: state
entity_id: select.thermostat_mode
action:
- lambda: |-
if (id(thermostat_mode).state == "Eco") {
id(heater_thermostat).set_target_temperature(18);
id(heater_thermostat).turn_on();
} else if (id(thermostat_mode).state == "Confort") {
id(heater_thermostat).set_target_temperature(21);
id(heater_thermostat).turn_on();
} else if (id(thermostat_mode).state == "Boost") {
id(heater_thermostat).set_target_temperature(23);
id(heater_thermostat).turn_on();
// Add timer logic for boost if needed
} else {
id(heater_thermostat).turn_off();
}
5. Remarks
- All logic (when to turn ON/OFF, setpoints, boost timer) is handled by ESPHome on the Sonoff R2.
- Home Assistant only sends the desired mode (Off/Eco/Confort/Boost) to ESPHome.
- This approach is robust: the heater will continue to operate as expected even if Home Assistant is offline.
This guide explains how to use a Sonoff R2 switch with ESPHome to control a heater as a thermostat, using a Xiaomi temperature sensor available via an MQTT broker.
Overview
- Device: Sonoff R2 (relay switch)
- Heater: Controlled via Sonoff relay (ON/OFF)
- Temperature Sensor: Xiaomi (publishing to MQTT broker)
- Goal: Use ESPHome to turn the heater ON/OFF based on the temperature received from MQTT.
Steps
1. Flash Sonoff R2 with ESPHome
- Install ESPHome on your computer.
- Create a new ESPHome configuration for the Sonoff R2.
- Flash the Sonoff R2 with the ESPHome firmware.
2. Configure MQTT in ESPHome
- Enable the MQTT component in your ESPHome YAML config.
- Set the MQTT broker address, username, and password.
3. Subscribe to Xiaomi Sensor via MQTT
- Identify the MQTT topic where the Xiaomi sensor publishes temperature (e.g.,
zigbee2mqtt/your_sensoror similar). - Use the
mqtt_subscribeormqttsensor in ESPHome to read the temperature value.
4. Create a Thermostat Logic in ESPHome
- Use the
climatecomponent with agenericplatform, or use a template switch with automation. - Set the relay (switch) as the output to control the heater.
- Use the MQTT temperature sensor as the input for the thermostat.
5. Example ESPHome YAML Snippet
esphome:
name: sonoff_thermostat
mqtt:
broker: 192.168.1.100
username: your_mqtt_user
password: your_mqtt_password
sensor:
- platform: mqtt_subscribe
name: "Room Temperature"
id: room_temp
topic: "zigbee2mqtt/your_sensor"
unit_of_measurement: "°C"
value_template: "{{ value_json.temperature }}"
switch:
- platform: gpio
pin: 12
id: relay
name: "Heater Relay"
climate:
- platform: thermostat
name: "Heater Thermostat"
sensor: room_temp
default_target_temperature_low: 19
default_target_temperature_high: 21
heat_action:
- switch.turn_on: relay
idle_action:
- switch.turn_off: relay
6. Upload and Test
- Upload the configuration to the Sonoff R2.
- Monitor the logs to ensure the temperature is received and the relay switches as expected.
7. Integrate with Home Assistant (Optional)
- Add the ESPHome device to Home Assistant for remote control and monitoring.
Troubleshooting
- Ensure the MQTT broker is accessible from the Sonoff R2.
- Verify the correct MQTT topic and payload format for the Xiaomi sensor.
- Check ESPHome logs for errors.
References
You now have a Sonoff R2 acting as a thermostat, using a Xiaomi temperature sensor via MQTT!
To control modes from Home Assistant, expose a select entity in ESPHome and use automations in ESPHome to change setpoints or behavior based on the selected mode.