Skip to content

Instantly share code, notes, and snippets.

@Resinchem
Last active July 4, 2023 20:11
Show Gist options
  • Save Resinchem/831f6f07456a846e4e5e83ab5e147551 to your computer and use it in GitHub Desktop.
Save Resinchem/831f6f07456a846e4e5e83ab5e147551 to your computer and use it in GitHub Desktop.
ESPHome and Home Assistant Code for Midea Window AC local integration and automation
# *******************************************************************************
# This contains the code used in my Midea window air conditioner local
# integration and automation within Home Assistant. It is specific to
# my paricular AC unit and needs. It will likely need to be adapted and
# modified for your own needs and equiment.
#
# This file contains the ESPHome code for the IR transmitter and the FSR
# bed sensor, along with the Home Assistant Helpers and automations. It is
# not meant to be used as a single file, but broken out into ESPHome code and
# other Home Assistant code.
#
# See the following for details on build and use:
# YouTube Video: https://youtu.be/KM01H6OHl_k
# Written Blog: https://resinchemtech.blogspot.com/2023/05/automate-window-ac.html
# *******************************************************************************
# ===============================
# ESPHome Code
# ===============================
# ------------------------------
# Climate IR Remote
# ------------------------------
esphome:
name: bedroom2-ac-remote
friendly_name: bedroom2-ac-remote
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "kU/10zcjKF4wZPVSy2eA57/sN9EhqXxf052ljXAtphE=" #your auto-generated key will be different
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: "Bedroom2-Ac-Remote"
password: !secret ap_password
captive_portal:
remote_transmitter:
pin: GPIO22
carrier_duty_percent: 50%
# Uncomment this section if you add an IR Receiver
# remote_receiver:
# id: br2acrcvr
# pin:
# number: GPIO19
# inverted: true
# mode:
# input: true
# pullup: true
# # high 55% tolerance is recommended for some remote control units
# tolerance: 55%
climate:
- platform: midea_ir #change this platform to match your AC type
name: "Bedroom2 AC"
use_fahrenheit: true
sensor: br2_room_temp_c
supports_heat: false
visual:
min_temperature: 15.56
max_temperature: 23.89
temperature_step:
target_temperature: 1.0
current_temperature: 0.1
sensor:
- platform: homeassistant
id: br2_room_temp
entity_id: sensor.bedroom2_temperature
- platform: template
id: br2_room_temp_c
lambda: |-
if (id(br2_room_temp).state) {
return round((id(br2_room_temp).state - 32) * (0.5556));
} else {
return 0;
}
# Remote restart button
button:
- platform: restart
name: "Bedroom2 AC Remote Restart"
# -----------------------------------
# Bed Sensor
# -----------------------------------
esphome:
name: bedroom2-bed-sensor
friendly_name: bedroom2-bed-sensor
esp8266:
board: d1_mini
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "M097e186JTBXCVsv6SZyS2+FIJdHhp4/ngMTTszGHD8=" # your auto-generated key will be different
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: "Bedroom2-Bed-Sensor"
password: !secret ap_password
captive_portal:
binary_sensor:
- platform: template
name: "Bedroom2 Bed Occupied"
id: br2_o
lambda: |-
if (id(br2_bed_sensor).state < id(trigger_level).state) {
return true;
} else {
return false;
}
- platform: gpio
pin:
number: D1
mode:
input: true
pullup: true
inverted: true
name: Bedroom2 Bed Button A
filters:
- delayed_on: 20ms
- delayed_off: 20ms
- platform: gpio
pin:
number: D2
mode:
input: true
pullup: true
inverted: true
name: Bedroom2 Bed Button B
filters:
- delayed_on: 20ms
- delayed_off: 20ms
sensor:
- platform: adc
pin: A0
name: "Bedroom2 Bed Sensor"
id: "br2_bed_sensor"
icon: mdi:bed
update_interval: 0.5s
filters:
- multiply: 3.3
- sliding_window_moving_average:
window_size: 10
send_every: 1
- or:
- throttle: 180s
- delta: 0.02
- platform: homeassistant
name: "Trigger Level"
id: "trigger_level"
entity_id: input_number.bedroom2_bed_trigger_level
# Remote restart button
button:
- platform: restart
name: "Bedroom2 Bed Sensor Restart"
# =================================
# HOME ASSISTANT HELPER ENTITIES
# =================================
# These must be manually created in YAML or via the UI
# ---------------------
# INPUT BOOLEANS
# ---------------------
input_boolean:
# Auto-run AC
bedroom2_ac_auto_run:
name: Auto Run Bedroom2 AC
# ---------------------
# INPUT NUMBERS
# ---------------------
input_number:
# Bed Sensor Trigger Level
bedroom2_bed_trigger_level:
name: Bed2 Trigger Level
min: 0
max: 3.5
step: 0.05
unit_of_measurement: V
icon: mdi:arrow-collapse-down
# ==============================
# HOME ASSISTANT AUTOMATIONS
# ==============================
automation:
# ------------------------
# Auto Turn On Automation
# ------------------------
- alias: Bedroom2 AC Turn On
#Automatically turn on AC before bed
id: 8cf9c132-239b-4b0e-8ffc-bf47bcc35c28
trigger:
- platform: time
at: "22:00:00"
condition:
condition: and
conditions:
# Check that auto AC is enabled
- condition: state
entity_id: input_boolean.bedroom2_ac_auto_run
state: "on"
# Only turn on if room temp is above 69°F
- condition: numeric_state
entity_id: sensor.bedroom2_temperature
above: 69
action:
# - service: switch.turn_on
# target:
# entity_id: switch.bedroom2_ac
- service: climate.set_hvac_mode
target:
entity_id: climate.bedroom2_ac
data:
hvac_mode: cool
#---------------------------
# Auto Turn Off automations
# --------------------------
- alias: Bedroom2 AC Turn Off with Bed Sensor
# Turn off AC based on bed occupancy change if after 6 am
id: e0f18e39-3025-45fa-b30a-9a449530c81a
trigger:
- platform: state
entity_id: binary_sensor.bedroom2_bed_occupied
from: 'on'
to: 'off'
for: '00:05:00' #allow time to return to bed (e.g. bathroom)
condition:
condition: and
conditions:
# Check that auto AC is enabled
- condition: state
entity_id: input_boolean.bedroom2_ac_auto_run
state: "on"
# Only turn off if after 6 am
- condition: time
after: "06:00:00"
before: "12:00:00"
action:
- service: climate.turn_off
target:
entity_id: climate.bedroom2_ac
- alias: Bedroom2 AC Turn Off with Time
# Check each hour and turn AC off if bed unoccupied
id: f7888bd8-1ccc-445c-b3e2-1f292e64a8c7
trigger:
# Check once per hour from 7-10am
- platform: time
at:
- "07:00:00"
- "08:00:00"
- "09:00:00"
- "10:00:00"
condition:
condition: and
conditions:
# Check that auto AC is enabled
- condition: state
entity_id: input_boolean.bedroom2_ac_auto_run
state: "on"
# Check that AC is not currently off
- condition: not
conditions:
condition: state
entity_id: climate.bedroom2_ac
state: 'off'
# Check that bed is unoccupied
- condition: state
entity_id: binary_sensor.bedroom2_bed_occupied
state: "off"
action:
- service: climate.turn_off
target:
entity_id: climate.bedroom2_ac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment