I can’t code. I used ChatGPT for everything and tidied up all the errors I could.
For a chicken coop I need the ESPhome device (kc868-a8) handle the automations separately from HASS because I don’t want chickens do die because my Pi is not booting. I do want HASS to keep an eye on the ESPhome device.
For time is use DS3231 RTC (DS1307 in config) together with the SNTP platform.
GPT decided to use intervals and lambdas for control of the following:
- Gate needs to be open from sunrise till sunset if it is not too cold outside
- Lamp needs to be on from sunrise till 14 hours (user set) after sunrise.
- Heater needs to be on when it is cold.
but it gets really stuck on row 189 where you need to know:
- What time it is?
- When is sunrise and sunset?
- Are the automations at correct states for this time?
esphome:
name: kc868-a8
platform: ESP32
board: esp32dev
ethernet:
type: LAN8720
mdc_pin: GPIO23
mdio_pin: GPIO18
clk_mode: GPIO17_OUT
phy_addr: 0
i2c:
sda: 4
scl: 5
scan: true
pcf8574:
- id: 'pcf8574_hub_out_1' # for output channel 1-8
address: 0x24
- id: 'pcf8574_hub_in_1' # for input channel 1-8
address: 0x22
time:
- platform: sntp
id: sntp_time
servers:
- pool.ntp.org
on_time:
- seconds: 0
minutes: 0
hours: 2
days_of_week: MON-SUN
then:
- lambda: |-
ESPTime time = id(sntp_time).now();
id(my_rtc).write_time();
- platform: ds1307
id: my_rtc
update_interval: 60s
sensor:
- platform: dallas_temp
name: "Outside Temperature"
id: outside_temperature
accuracy_decimals: 1
one_wire_id: bus1
- platform: dallas_temp
name: "Indoor Temperature"
id: indoor_temperature
accuracy_decimals: 1
one_wire_id: bus2
- platform: homeassistant
id: lamp_duration_input
entity_id: input_number.lamp_duration
on_value:
then:
- lambda: |-
id(lamp_duration) = (int) id(lamp_duration_input).state;
binary_sensor:
- platform: gpio
pin:
pcf8574: pcf8574_hub_in_1
number: 0
mode: INPUT
inverted: true
id: gate_open
name: "Gate Open"
- platform: gpio
pin:
pcf8574: pcf8574_hub_in_1
number: 1
mode: INPUT
inverted: true
id: gate_closed
name: "Gate Closed"
- platform: gpio
pin:
pcf8574: pcf8574_hub_in_1
number: 2
mode: INPUT
inverted: true
id: door_open
name: "Door Open"
- platform: homeassistant
id: gate_automation
entity_id: input_boolean.gate_automation
on_state:
then:
- lambda: |-
id(gate_automation_enabled) = id(gate_automation).state;
- platform: homeassistant
id: lamp_automation
entity_id: input_boolean.lamp_automation
on_state:
then:
- lambda: |-
id(lamp_automation_enabled) = id(lamp_automation).state;
- platform: homeassistant
id: heater_automation
entity_id: input_boolean.heater_automation
on_state:
then:
- lambda: |-
id(heater_automation_enabled) = id(heater_automation).state;
switch:
- platform: gpio
pin:
pcf8574: pcf8574_hub_out_1
number: 0
mode: OUTPUT
inverted: true
id: gate_rise
name: "Gate Rise"
- platform: gpio
pin:
pcf8574: pcf8574_hub_out_1
number: 1
mode: OUTPUT
inverted: true
id: gate_close
name: "Gate Close"
- platform: gpio
pin:
pcf8574: pcf8574_hub_out_1
number: 2
mode: OUTPUT
inverted: true
id: lamp
name: "Lamp"
- platform: gpio
pin:
pcf8574: pcf8574_hub_out_1
number: 3
mode: OUTPUT
inverted: true
id: heater
name: "Heater"
globals:
- id: gate_automation_enabled
type: bool
restore_value: yes
initial_value: 'true'
- id: lamp_automation_enabled
type: bool
restore_value: yes
initial_value: 'true'
- id: heater_automation_enabled
type: bool
restore_value: yes
initial_value: 'true'
- id: lamp_duration
type: int
restore_value: yes
initial_value: '14'
sun:
latitude: 59
longitude: 24
id: sunc
interval:
- interval: 1min
then:
- lambda: |-
// Get current time from RTC
auto time = id(my_rtc).now();
// Calculate sunrise and sunset times
auto sun = id(sunc);
// auto sunrise = sun->sunrise().timestamp;
// auto sunset = sun->sunset().timestamp;
auto sunrise = sun->sunrise(time, 0.0).value();
auto sunset = sun->sunset(time, 0.0).value();
auto lamp_off_time = sunrise + id(lamp_duration) * 3600;
// Check if it's between sunrise and sunset
if (time.timestamp > sunrise && time.timestamp < sunset) {
// Gate automation check
if (id(gate_automation_enabled) && id(outside_temperature).state > 0) {
if (!id(gate_open).state) {
id(gate_rise).turn_on();
while (!id(gate_open).state) {
delay(100); // Wait until the gate is open
}
id(gate_rise).turn_off();
}
}
}
// Check if it's between sunrise and lamp_off_time
if (time.timestamp > sunrise && time.timestamp < lamp_off_time) {
// Lamp automation check
if (id(lamp_automation_enabled) && !id(lamp).state) {
id(lamp).turn_on();
}
} else {
// Turn off the lamp after lamp_off_time
if (id(lamp).state) {
id(lamp).turn_off();
}
}
- interval: 30s
then:
- if:
condition:
and:
- lambda: 'return id(heater_automation_enabled);'
- lambda: 'return id(indoor_temperature).state < 2;'
then:
- switch.turn_on: heater
- if:
condition:
lambda: 'return id(indoor_temperature).state >= 5;'
then:
- switch.turn_off: heater
one_wire:
- platform: gpio
pin: GPIO14
id: bus1
- platform: gpio
pin: GPIO13
id: bus2
api:
encryption:
key: "secret"
Thanks for your time!