Stand Alone Climate Control

Tags: #<Tag:0x00007fc40a7e1910> #<Tag:0x00007fc40a7e1780> #<Tag:0x00007fc40a7e15c8> #<Tag:0x00007fc40a7e1460>

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

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

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:

1 Like