Stand Alone Climate Control

I am moving my HA environment from a centralised command and control on a raspberry pi to a distributed ESPHome setup (HA on a virtual machine, with control via ESPHome on ESP8266 and ESP32 units). Part of that journey means moving functionality from within HA automations and YAML files to ESPHome configurations. One such area is the climate control I currently use…

I found this code to setup a climate control on ESPHome…

climate:
  - platform: thermostat
    sensor: my_temperature_sensor
    default_target_temperature_high: 22 °C
    cool_action:
      - switch.turn_on: air_cond
    idle_action:
      - switch.turn_off: air_cond

Which is great, but I am stuck trying to integrate the functionality from this automation code below.

alias: Open Damper
description: Open Damper Based on Outside Temperature
trigger:
  - platform: numeric_state
    entity_id: sensor.esp_outside_temp
    below: '21'
  - platform: state
    entity_id: binary_sensor.ac_status
    from: 'on'
    to: 'off'
condition:
  - condition: state
    entity_id: binary_sensor.ac_status
    state: 'off'
action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.damperrelay
  - service: mqtt.publish
    data:
      topic: HomeAssistant/Studio/Damper/Status
      payload: '1'
mode: single
alias: Close Damper
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.esp_outside_temp
    above: '21'
  - platform: state
    entity_id: binary_sensor.ac_status
    from: 'off'
    to: 'on'
condition: []
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.hvac_damper
  - service: mqtt.publish
    data:
      topic: HomeAssistant/Studio/Damper/Status
      payload: '0'
mode: single
alias: Studio Fan Status
description: ''
trigger:
  - platform: state
    entity_id: switch.hvac_fan
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: switch.hvac_fan
            state: 'on'
        sequence:
          - service: mqtt.publish
            data:
              topic: HomeAssistant/Studio/Fan/Status
              payload: '1'
      - conditions:
          - condition: state
            entity_id: switch.hvac_fan
            state: 'off'
        sequence:
          - service: mqtt.publish
            data:
              topic: HomeAssistant/Studio/Fan/Status
              payload: '0'
    default: []
mode: single
alias: Start Fan
description: Start Fan Climate Control Based on Temperature and Damper Status
trigger:
  - platform: state
    entity_id: switch.hvac_damper
    from: 'off'
    to: 'on'
condition: []
action:
  - service: climate.turn_on
    target:
      entity_id: climate.studiofan
mode: single
alias: Stop Fan
description: Stop Fan Climate Control Based on Damper Status
trigger:
  - platform: state
    entity_id: switch.hvac_damper
    from: 'on'
    to: 'off'
condition: []
action:
  - service: climate.turn_off
    target:
      entity_id: climate.studiofan
mode: single

The automations above were originally running on my raspberry pi, which also had the GPIO ports connected to a relay board. But since I now use a VM to host HA and ESP8266 and ESP32 to do the relay board things dont work so well.

In essence I want the ESPHome ESP8266 to be stand alone in the event HA stops working. So I want the ESPHome connected to the relay board to open and close the air damper and start the fan based on various checks (see automation examples above).

I am sure the solution is simple, and it’s just a learning curve for me… so if there’s any examples some of you have seen that I could use to guide me that would be great. Much appreciated.

Thanks, is there an editor I could use that could help me code the YAML? Something that might help me find my coding mistakes prior to loading onto the ESP8266?

The editor built into esphome does that, or use vscode.

Tried to setup the climate control but seeing weird behavior and results in the logs… Here’s the ESPHome config.

esphome:
  name: hvac-test
  platform: ESP8266
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Hvac-Test Fallback Hotspot"
    password: "<PASSWORD>"

captive_portal:

status_led:
  pin: GPIO2

dallas:
  - pin: D1
    update_interval: 30s

sensor:
  - platform: dallas
    id: in_dallas_id
    address: 0x1101143025227D28
    name: in_temp
  - platform: dallas
    id: out_dallas_id
    address: 0xE602131B13F9AA28
    name: out_temp

switch:
  - platform: template
    name: "Template Switch"
    id: template_swi

climate:
  - platform: thermostat
    id: climate_id
    sensor: in_dallas_id
    default_target_temperature_high: 21 °C
    cool_action:
      - switch.turn_on: template_swi
    idle_action:
      - switch.turn_off: template_swi

Here’s the log file entry…

[21:08:15][D][dallas.sensor:153]: 'in_temp': Got Temperature=19.9°C
[21:08:15][D][sensor:117]: 'in_temp': Sending state 19.93750 °C with 1 decimals of accuracy
[21:08:15][D][climate:369]: 'climate_id' - Sending state:
[21:08:15][D][climate:372]:   Mode: OFF
[21:08:15][D][climate:374]:   Action: OFF
[21:08:15][D][climate:392]:   Current Temperature: 19.94°C
[21:08:15][D][climate:398]:   Target Temperature: 21.00°C
[21:08:15][D][dallas.sensor:153]: 'out_temp': Got Temperature=15.8°C
[21:08:15][D][sensor:117]: 'out_temp': Sending state 15.81250 °C with 1 decimals of accuracy

I dont understand why MODE is OFF and ACTION is OFF??

Also, another unusual situation… when I am not connected to the nodemcu the blue led flashes about once every second. When I click on LOGS in the ESPHome panel the led changes to solid?

Question… I want the thermostat to only turn on the switch if several conditions are met… so I assume the checks of those conditions are placed within the thermostat sections below…

cool_action:
      - switch.turn_on: template_swi
    idle_action:
      - switch.turn_off: template_swi

Thanks, Mark

Haven’t received any responses to above, so not hopeful of anything on this one.

Have spent loads of time reading and learning, built a test rig and have come up with the following code. It works, but I am sure there are much more elegant ways it could be written/done?

Not a fan of the bang bang controller and would prefer an actual thermostat style with hysteresis, but at least this is a start.

Anyway, if anyone reads this and can offer some tips or suggestions that would be very much appreciated. If not, sorry for wasting your time.

esphome:
  name: testbuild
  platform: ESP8266
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Testbuild Fallback Hotspot"
    password: "<redacted>"

captive_portal:

# MQTT Interface
mqtt:
  broker: !secret mqtt_broker
  port: 1883
  keepalive: 60s
  on_message:
    - topic: GregoInc1/AirCon/Status
      payload: "1"
      qos: 0
      then:
        - switch.turn_on: ac_status
    - topic: GregoInc1/AirCon/Status
      payload: "0"
      qos: 0
      then:
        - switch.turn_off: ac_status

status_led:
  pin: GPIO2

#Temperature Sensor Pin
dallas:
  - pin: D1
    update_interval: 30s

#Relay switches
switch:
  - platform: gpio
    pin: 
      number: D5
      inverted: True
    id: damper_sw
    name: "damper_sw"
    restore_mode: RESTORE_DEFAULT_ON
  - platform: gpio
    pin: 
      number: D7
      inverted: True
    id: fan_sw
    name: "fan_sw"
    restore_mode: RESTORE_DEFAULT_OFF
  - platform: template
    name: "AirCon Switch"
    id: ac_status

sensor:
  - platform: dallas
    id: in_dallas_id
    address: 0x1101143025227D28
    name: test_in_temp
  - platform: dallas
    id: out_dallas_id
    address: 0xE602131B13F9AA28
    name: test_out_temp

interval:
  - interval: 30sec
    then:
      - if:
          condition:
            and:
            # Is the outside temp below 20 and the AC off
              - lambda: 'return id(out_dallas_id).state < 20;'
              - switch.is_off: ac_status
          then:
              - switch.turn_on: damper_sw
      - if:
          condition:
            or:
            # Is the outside temp above 20
              - lambda: 'return id(out_dallas_id).state > 20;'
            # Check to see if the AC is running
              - switch.is_on: ac_status
          then:
              - switch.turn_off: damper_sw

climate:
  - platform: bang_bang
    name: "Bang Bang Climate Controller"
    sensor: in_dallas_id
    default_target_temperature_low: 20 °C
    default_target_temperature_high: 22 °C
    cool_action:
      script.execute: control_fan
    idle_action:
      script.execute: control_fan

script:
  - id: control_fan
    then:
    - if:
        condition:
          lambda: 'return id(in_dallas_id).state <= 20;'
        then:
          - logger.log: "The indoor sensor value is below 20!"
          - switch.turn_off: fan_sw
    - if:
        condition:
          and:
            - switch.is_off: ac_status
            - lambda: 'return id(in_dallas_id).state >= 22;'
        then:
          - logger.log: "The indoor sensor value is above 22!"
          - switch.turn_on: fan_sw

I will add a few more things and see how it goes on the actual esphome unit. Thanks, Mark

1 Like

Have you joined the esphome discord server, it maybe better place to handle specific esphome stuff

Hi @aidbish great to hear from you, and thank for the advice/suggestion.

Honestly I haven’t explored the discord as I’ve felt this forum is possibly not as friendly as other IOT forums I have joined. I get that asking random ‘dumb’ questions can be frustrating for those experienced forum guru members, but in my opinion building/sharing the knowledge/experience is what can make a forum strong and the product grow in popularity.

For me personally I have found forums such as ESPEasy and Domoticz are friendly, share experience openly, and the gurus are incredibly generous with their time and knowledge with those of us that are dummies and were just starting out. Over time knowledge grew and those newbies became the next level of supporting the forum and newcomers.

Anyway, I guess each forum environment is different, as are the members. I am not complaining, just sharing a few comparisons/observations. I think Home Assistant and ESPHome are awesome, with so many possibilities… so the journey will continue :slight_smile:

Take care, Mark

1 Like

Have made some fantastic progress thanks to the folks on the esphome discord. Thought I would share my code here on the chance it might help someone new to esphome like me.

esphome:
  name: esp1_studio
  platform: ESP8266
  board: nodemcuv2
  on_boot:
    - lambda: 'id(climate_id).mode = climate::CLIMATE_MODE_COOL;'

# Enable logging
logger:

# Enable Home Assistant API
api:
  reboot_timeout: 0s
  
ota:
  password: !secret ota_password

wifi:
#  use_address: testbuild.local
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "esp1_studio Fallback Hotspot"
    password: "<redacted>"

captive_portal:

# MQTT Interface
mqtt:
  broker: !secret mqtt_broker
  port: 1883
  keepalive: 60s
  discovery: false
  on_message:
    - topic: GregoInc1/AirCon/Status
#    - topic: HomeAssistant/AirCon/Studio/Status
      payload: "0"
      qos: 0
      then:
        - binary_sensor.template.publish:
            id: ac_status
            state: off
#    - topic: HomeAssistant/AirCon/Studio/Status
    - topic: GregoInc1/AirCon/Status
      payload: "1"
      qos: 0
      then:
        - binary_sensor.template.publish:
            id: ac_status
            state: on

status_led:
  pin: GPIO2

# Studio door reed switch sensor.
binary_sensor:
  - platform: gpio
    device_class: door
    pin:
      number: D2
      mode: INPUT_PULLUP
      inverted: False
    name: "Studio Door"
    id: studio_door
    on_press:
      - mqtt.publish:
          topic: HomeAssistant/Studio/Door/Status
          payload: "1"
      - logger.log: "Studio Door is Open"
    on_release:
      # Publish the door is closed
      - mqtt.publish:
         topic: HomeAssistant/Studio/Door/Status
         payload: "0"
      - logger.log: "Studio Door is Closed"

  - platform: homeassistant
    id: s10_phone
    entity_id: device_tracker.galaxy_s10
    attribute: home

# AC status
  - platform: template
    id: ac_status
    on_press:
      - binary_sensor.template.publish:
          id: climate_status
          state: off
    on_release:
      - binary_sensor.template.publish:
          id: climate_status
          state: on
  - platform: template
    id: climate_status
    on_press:
      - climate.control:
          id: climate_id
          mode: 'cool'
      - switch.turn_on: damper_sw
    on_release:
      - climate.control:
          id: climate_id
          mode: 'off'
      - switch.turn_off: damper_sw

#Temperature Sensor Pin
dallas:
  - pin: D1
    update_interval: 30s

#Relay switches
switch:
  - platform: gpio
    pin: 
      number: D6
      inverted: True
    id: damper_sw
    name: "damper_sw"
    restore_mode: RESTORE_DEFAULT_OFF
  - platform: gpio
    pin: 
      number: D5
      inverted: True
    id: fan_sw
    name: "fan_sw"
    restore_mode: RESTORE_DEFAULT_OFF

sensor:
  # Studio Inside temperature sensor
  - platform: dallas
    id: in_dallas_id
    address: 0x1101143025227D28
    name: studio_in_temp
  # Studio Outside temperature sensor
  - platform: dallas
    id: out_dallas_id
    address: 0xE602131B13F9AA28
    name: studio_out_temp
    on_value:
      then:
      - if:
          condition:
              # Is the outside temp below 20 and the AC off
              - lambda: 'return id(out_dallas_id).state < 20;'
          then:
              # Open the damper
              - binary_sensor.template.publish:
                  id: climate_status
                  state: on
              - mqtt.publish:
                  topic: HomeAssistant/Studio/Damper/Status_Damper
                  payload: "1"
              - logger.log: "The damper and climate is active"
      - if:
          condition:
              # Is the outside temp above 20
              - lambda: 'return id(out_dallas_id).state > 20;'
          then:
              - binary_sensor.template.publish:
                  id: climate_status
                  state: off
              - mqtt.publish:
                  topic: HomeAssistant/Studio/Damper/Status_Damper
                  payload: "0"
              - logger.log: "The damper and climate is off"

climate:
  - platform: thermostat
    name: "Thermostat Climate Controller"
    id: climate_id
    sensor: in_dallas_id
    default_target_temperature_high: 21 °C
    cool_action:
      - switch.turn_on: fan_sw
      - logger.log: "The fan is now on"
      - mqtt.publish:
          topic: HomeAssistant/Studio/Fan/Status_Fan
          payload: "1"
    idle_action:
      - switch.turn_off: fan_sw
      - logger.log: "The fan is now off"
      - mqtt.publish:
          topic: HomeAssistant/Studio/Fan/Status_Fan
          payload: "0"

Still some way to go, but for now it works.

Hope this helps someone :slight_smile:

2 Likes

The code above has been running for some time now and is successfully running the ‘free cooling’ of the room i.e. sucking in cool air when the days are not so hot.

I have also been working on a setup that will activate an air conditioner on the hot days, with information coming via various other threads.

I’ve tried to get some help on the esphome discord, but no luck so far… so I thought I might try my luck here. I am close, very close… just need a bit of advice to get things completed. Here’s my YAML code… I have identified where I believe the problem is located with

esphome:
  name: esp5-studio-ac
  platform: ESP32
  board: esp32doit-devkit-v1
 
# Enable logging
logger:
 
# Enable Home Assistant API
api:
 
ota:
  password: "************************"
 
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  #use_address: esp32-ir-test.local
 
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32-Ir-Test Fallback Hotspot"
    password: "**************"
 
captive_portal:
 
status_led:
  pin: GPIO2
 
#Temperature Sensor Pin
dallas:
  - pin: GPIO15
    update_interval: 30s
 
mqtt:
  broker: !secret mqtt_broker
  port: 1883
  reboot_timeout: 0s
  keepalive: 60s
  discovery: false
 
# IR TRansmitter entry
remote_transmitter:
  pin: GPIO4
  id: ir_transmit
  carrier_duty_percent: 50%
 
climate:
  - platform: daikin
    name: "Aircon Studio"
    id: ac_climate_id
    supports_heat: false
    transmitter_id: ir_transmit
 
binary_sensor:
  - platform: template
    id: ac_climate_mgmt
    on_press:
      - logger.log: "AC is ON"
      - climate.control:
          id: ac_climate_id
          mode: 'cool'
    on_release:
      - logger.log: "AC is OFF"
      - climate.control:
          id: ac_climate_id
          mode: 'off'
  - platform: template
    id: out_temp_status
 
sensor:
# Studio Inside temperature sensor
  - platform: dallas
    id: in_ac_dallas_id
    resolution: 12
    address: 0x2401204FD9422728
    name: "Studio AC Inside Temperature"
 
# Studio Temperature / Climate Control  
  - platform: homeassistant
    id: out_temp_studio
    name: "Studio Outside Temperature"
    entity_id: sensor.studio_out_temp
    on_value:
      then:
      - if:
          condition:
              # Test if studio temperature is over 24 degrees
              - lambda: 'return id(in_ac_dallas_id).state > 24;'
          then:   
              - binary_sensor.template.publish:
                  id: ac_climate_mgmt
                  state: on
              - mqtt.publish:
                  topic: HomeAssistant/Studio/AirCon/Status
                  payload: "1"
              - logger.log: "AC Climate Control is Active"
      - if:
          condition:
              and:
 -->          # Test if studio inside temperature is below 24 degrees
              - lambda: 'return id(in_ac_dallas_id).state < 24;
 -->          # Test if studio outside temperature is below 19 degrees'
              - lambda: 'return id(out_temp_studio).state < 19;'
          then:   
              - binary_sensor.template.publish:
                  id: ac_climate_mgmt
                  state: off
              - mqtt.publish:
                  topic: HomeAssistant/Studio/AirCon/Status
                  payload: "0"
              - logger.log: "AC Climate Control is Not Active"

And here’s the error I receive when trying to do an OTA…

Compiling /data/esp5-studio-ac/.pioenvs/esp5-studio-ac/src/main.cpp.o
/config/esphome/esp5-studio-ac.yaml:102:43: error: stray '#' in program
               - lambda: 'return id(in_ac_dallas_id).state < 24;
                                           ^
/config/esphome/esp5-studio-ac.yaml: In lambda function:
/config/esphome/esp5-studio-ac.yaml:102:45: error: 'Test' was not declared in this scope
               - lambda: 'return id(in_ac_dallas_id).state < 24;
                                             ^
*** [/data/esp5-studio-ac/.pioenvs/esp5-studio-ac/src/main.cpp.o] Error 1
========================== [FAILED] Took 4.45 seconds ==========================

Any advice or suggestions greatly appreciated :slight_smile:

So I managed to solve the problem, but it’s not the most elegant code I have seen. Just in case this might help someone here is the code…

esphome:
  name: esp5-studio-ac
  platform: ESP32
  board: esp32doit-devkit-v1

# Enable logging
logger:
#  level: VERBOSE

# Enable Home Assistant API
api:

ota:
  password: "*********************"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32-Ir-Test Fallback Hotspot"
    password: "******************"

captive_portal:

status_led:
  pin: GPIO2

#Temperature Sensor Pin
dallas:
  - pin: GPIO15
    update_interval: 30s

mqtt:
  broker: !secret mqtt_broker
  port: 1883
  reboot_timeout: 0s
  keepalive: 60s
  discovery: false

# IR TRansmitter entry
remote_transmitter:
  pin: GPIO4
  id: ir_transmit
  carrier_duty_percent: 50%

climate:
  - platform: daikin
    name: "Aircon Studio"
    id: ac_climate_id
    supports_heat: false
    transmitter_id: ir_transmit

binary_sensor:
  - platform: template
    id: ac_climate_mgmt
    on_press:
      then:
              - logger.log: "AC is ON"
              - climate.control:
                  id: ac_climate_id
                  mode: 'cool'
                  target_temperature: 22°C
              - mqtt.publish:
                  topic: HomeAssistant/Studio/AirCon/Status
                  payload: "1"
    on_release:
      then:
      - if:
          condition:
              - binary_sensor.is_off: out_ac_temp_status
          then:
              - logger.log: "AC is OFF"
              - climate.control:
                  id: ac_climate_id
                  mode: 'off'
              - mqtt.publish:
                  topic: HomeAssistant/Studio/AirCon/Status
                  payload: "0"
  
  - platform: template
    id: out_ac_temp_status

sensor:
# Studio Inside temperature sensor
  - platform: dallas
    id: in_ac_dallas
    resolution: 12
    address: 0x2401204FD9422728
    name: "Studio AC Inside Temperature"
    on_value:
      then:
      - if:
          condition:
              # Is the inside temp higher than 24
              - lambda: 'return id(in_ac_dallas).state > 22;'
          then:
              # Open the damper
              - binary_sensor.template.publish:
                  id: ac_climate_mgmt
                  state: on
              - logger.log: "The inside temp is above 22"
          else:
              - binary_sensor.template.publish:
                  id: ac_climate_mgmt
                  state: off
              - logger.log: "Telling Climate to stop"

# Studio Temperature from HA  
  - platform: homeassistant
    id: out_temp_studio
    name: "Studio Outside Temperature"
    entity_id: sensor.studio_out_temp
    on_value:
      then:
      - if:
          condition:
              # Is the Outside temp under 19
              - lambda: 'return id(out_temp_studio).state < 19;'
          then:   
              - binary_sensor.template.publish:
                  id: out_ac_temp_status
                  state: off
              - logger.log: "The outside temp is below 19"
      - if:
          condition:
              # Is the Outside temp over 19
              - lambda: 'return id(out_temp_studio).state > 19;'
          then:
              - binary_sensor.template.publish:
                  id: out_ac_temp_status
                  state: on
              - logger.log: "The outside temp is above 19"

Not seeing any feedback which is unfortunate.

1 Like

Latest version… have made a few tweaks, and this is working well. Hope this helps someone, I always found looking at other folks code helpful.

esp8266:
 board: nodemcuv2
 framework:
  version: recommended

esphome:
  name: esp1-studio
  on_boot:
    - lambda: 'id(climate_id).mode = climate::CLIMATE_MODE_COOL;'

# Enable logging
logger:

# Enable Home Assistant API
api:
  reboot_timeout: 0s
  
ota:
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "esp1_studio Fallback Hotspot"
    password: "redacted"

captive_portal:

# MQTT Interface
mqtt:
  broker: !secret mqtt_broker
  port: 1883
  reboot_timeout: 0s
  keepalive: 60s
  discovery: false
  on_message:
    - topic: HomeAssistant/Studio/Aircon/Status
      payload: "OFF"
      qos: 0
      then:
        - binary_sensor.template.publish:
            id: ac_status
            state: off
        - logger.log: "The AC is OFF"
    - topic: HomeAssistant/Studio/Aircon/Status
      payload: "ON"
      qos: 0
      then:
        - binary_sensor.template.publish:
            id: ac_status
            state: on
        - logger.log: "The AC is ON"

status_led:
  pin: GPIO2

binary_sensor:

# AC status
  - platform: template
    id: ac_status
    on_press:
      - binary_sensor.template.publish:
          id: climate_status
          state: off
    on_release:
      - binary_sensor.template.publish:
          id: climate_status
          state: on
  - platform: template
    id: climate_status
    on_press:
      - climate.control:
          id: climate_id
          mode: 'cool'
      - switch.turn_on: damper_sw
    on_release:
      - climate.control:
          id: climate_id
          mode: 'off'
      - switch.turn_off: damper_sw

# Studio Air Conditioner Status from HA  
  - platform: homeassistant
    id: studio_ac_climate
    name: "Studio AC Status from HA"
    entity_id: binary_sensor.studio_ac_climate

#Temperature Sensor Pin
dallas:
  - pin: D1
    update_interval: 30s

#Relay switches
switch:
  - platform: gpio
    pin: 
      number: D6
      inverted: True
    id: damper_sw
    name: "damper_sw"
    restore_mode: RESTORE_DEFAULT_OFF
  - platform: gpio
    pin: 
      number: D5
      inverted: True
    id: fan_sw
    name: "fan_sw"
    restore_mode: RESTORE_DEFAULT_OFF
  - platform: gpio
    pin: 
      number: D7
      inverted: True
    id: rackfan_sw
    name: "rackfan_sw"
    restore_mode: RESTORE_DEFAULT_OFF

# Restart
  - platform: restart
    name: "esp1 Studio Restart"

sensor:
# Studio Outside DHT11 Sensor
  - platform: dht
    model: DHT11
    pin: D2
    temperature:
      name: "Studio DHT Out Temp"
      id: studio_dht_out_temp
    humidity:
      name: "Studio DHT Humidity"
    update_interval: 30s
  - platform: dht
    model: DHT11
    pin: D3
    temperature:
      name: "Studio DHT In Temp"
      id: studio_dht_in_temp
    humidity:
      name: "Studio DHT In Humidity"
    update_interval: 30s
  # Studio Inside temperature sensor
  - platform: dallas
    id: in_dallas_id
    resolution: 12
    address: <redacted>
    name: studio_in_temp
  # Studio Outside temperature sensor
#  - platform: dallas
#    id: out_dallas_id
#    resolution: 12
#    address: <redacted>
#    name: studio_out_temp
    on_value:
      then:
      - if:
          condition:
            and:
              # Is the outside temp below 19 and the AC off
              - binary_sensor.is_off: studio_ac_climate
              - lambda: 'return id(studio_dht_out_temp).state < 19;'
          then:
              # Open the damper
              - binary_sensor.template.publish:
                  id: climate_status
                  state: on
              - mqtt.publish:
                  topic: HomeAssistant/Studio/Damper/Status
                  payload: "1"
              - logger.log: "The damper and climate is active"
      - if:
          condition:
            or:
              # Is the outside temp above 19 or the AC on
              - binary_sensor.is_on: ac_status
              - lambda: 'return id(studio_dht_out_temp).state > 19;'
          then:
              - binary_sensor.template.publish:
                  id: climate_status
                  state: off
              - mqtt.publish:
                  topic: HomeAssistant/Studio/Damper/Status
                  payload: "0"
              - logger.log: "The damper and climate is off"

climate:
  - platform: thermostat
    min_idle_time: 30s
    min_cooling_off_time: 120s
    min_cooling_run_time: 120s
    name: "Studio Climate Controller"
    id: climate_id
    sensor: in_dallas_id
    default_target_temperature_high: 21 °C
    cool_action:
      - switch.turn_on: fan_sw
      - logger.log: "The fan is now on"
      - mqtt.publish:
          topic: HomeAssistant/Studio/Fan/Status
          payload: "1"
    idle_action:
      - switch.turn_off: fan_sw
      - logger.log: "The fan is now off"
      - mqtt.publish:
          topic: HomeAssistant/Studio/Fan/Status
          payload: "0"

And here is the code I am using to run my air conditioner…

esphome:
  name: esp5-studio-ac
  platform: ESP32
  board: esp32doit-devkit-v1
  on_boot:
  - wait_until:
      condition: api.connected
  - delay: 2s
  - climate.control:
      id: ac_climate_id
      mode: 'off'
  - mqtt.publish:
      topic: HomeAssistant/Studio/Aircon/Status
      payload: "OFF"

# Enable logging
logger:
#  level: VERBOSE

# Enable Home Assistant API
api:
  reboot_timeout: 0s

ota:
  password: "redacted"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32-Ir-Test Fallback Hotspot"
    password: "redacted"

captive_portal:

status_led:
  pin: GPIO2

#Temperature Sensor Pin
dallas:
  - pin: GPIO15
    update_interval: 30s

mqtt:
  broker: !secret mqtt_broker
  port: 1883
  reboot_timeout: 0s
  keepalive: 60s
  discovery: false

# IR TRansmitter entry
remote_transmitter:
  pin: GPIO4
  id: ir_transmit
  carrier_duty_percent: 50%

climate:
  - platform: daikin
    name: "Aircon Studio"
    id: ac_climate_id
    supports_heat: false
    transmitter_id: ir_transmit

script:
  - id: ac_timer
    then:
      - binary_sensor.template.publish:
          id: timer_status
          state: ON
      - logger.log: "AC Timer is ON"
      - delay: 20min
      - binary_sensor.template.publish:
          id: timer_status
          state: OFF
      - logger.log: "AC Timer is OFF"

binary_sensor:
  - platform: template
    id: ac_climate_mgmt
    name: "Studio AC Climate"
    on_press:
      then:
          - climate.control:
              id: ac_climate_id
              mode: COOL
              fan_mode: AUTO
              target_temperature: 22°C
          - mqtt.publish:
              topic: HomeAssistant/Studio/Aircon/Status
              payload: "ON"
          - logger.log: "AC is ON"
    on_release:
      then:
          - climate.control:
              id: ac_climate_id
              mode: 'off'
          - mqtt.publish:
              topic: HomeAssistant/Studio/Aircon/Status
              payload: "OFF"
          - logger.log: "AC is OFF"
  - platform: template
    id: out_ac_temp_status
  
  - platform: template
    id: timer_status

sensor:
# Studio Inside temperature sensor
  - platform: dallas
    id: in_ac_dallas
    resolution: 12
    address: <redacted>
    name: "Studio AC Inside Temperature"
    on_value:
      then:
      - if:
          condition:
              # Is the inside temp higher than 24
              - lambda: 'return id(in_ac_dallas).state > 24;'
          then:
              # Open the damper
              - binary_sensor.template.publish:
                  id: ac_climate_mgmt
                  state: on
              - script.execute: ac_timer
              - logger.log: "Telling Climate to START"
          
      - if:
          condition:
            and:
              # Is the Outside temp under 19
              - lambda: 'return id(out_temp_studio).state < 18;'
              # Is the inside temp lower than 22
              - lambda: 'return id(in_ac_dallas).state < 22;'
              - binary_sensor.is_off: timer_status
          then:
              - binary_sensor.template.publish:
                  id: ac_climate_mgmt
                  state: off
              - logger.log: "Telling Climate to STOP"

# Studio Outside Temperature from HA  
  - platform: homeassistant
    id: out_temp_studio
    name: "Studio Outside Temperature"
    entity_id: sensor.studio_dht_out_temp

Enjoy!

3 Likes

I’m not able to offer any help but I just wanted to let you know that I appreciate you posting this stuff in here as I’m about to embark on a similar journey and hopefully your work will be of assistance to me.!

2 Likes

Hi. First of all, i appreciate all the efforts put in by you. I see you were the only one updating your code with improvements.

I had a question on whether this will work with cooling and heating? Basically an HVAC.

Right now i am building a solution that is based off home assistant. But i want the solution to be autonomous, same as what you were trying to do.

Thanks for the code