Hello everyone, I’m having an issue using a Home Assistant automation with my ESPHome.
My idea is to capture the time when the sun reaches the “civil twilight” solar position and use that time to update the “time.esp_chicken_coop_doors_time_close” entity so that the closing time is always adjusted throughout the year.
My esphome code:
substitutions:
name: esp-chicken-coop-doors
friendly_name: ESP Chicken Coop Doors
comment: Controls coop doors
door_cycle_time: 120s
packages:
base: !include common/base.yaml
wifi: !include common/wifi.yaml
esp32: !include common/devices/esp32_az-delivery-devkit-v4.yaml
external_components:
- source:
type: git
url: https://github.com/trombik/esphome-component-ds1302
components:
- ds1302
ota:
- platform: esphome
password: "..."
api:
encryption:
key: "..."
esphome:
on_boot:
- priority: 600
then:
- delay: 10s
- if:
condition:
lambda: |-
ESP_LOGI("main", "BOOT DONE!");
auto now = id(ds1302_time).now();
auto timeopen = id(time_open).state_as_esptime();
auto timeclose = id(time_close).state_as_esptime();
return (now.hour > timeopen.hour || (now.hour == timeopen.hour && now.minute > timeopen.minute)) && (now.hour < timeclose.hour || (now.hour == timeclose.hour && now.minute < timeclose.minute));
then:
- cover.open: door_1
- cover.open: door_2
- cover.open: door_3
else:
- cover.close: door_1
- cover.close: door_2
- cover.close: door_3
datetime:
- platform: template
id: time_open
type: time
name: "Time Open"
icon: mdi:sort-clock-descending
optimistic: yes
initial_value: "08:30:00"
restore_value: true
on_time:
then:
- cover.open: door_1
- cover.open: door_2
- cover.open: door_3
- platform: template
id: time_close
type: time
name: "Time Close"
icon: mdi:sort-clock-ascending
optimistic: yes
initial_value: "21:30:00"
restore_value: true
on_time:
then:
- cover.close: door_1
- cover.close: door_2
- cover.close: door_3
time:
- platform: ds1302
id: ds1302_time
cs_pin: GPIO25
dio_pin: GPIO26
clk_pin: GPIO27
update_interval: never
- platform: sntp
id: sntp_time
on_time_sync:
then:
ds1302.write_time:
switch:
- platform: gpio
id: door_1_open
pin: GPIO12
inverted: false
interlock: [door_1_close]
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
- delay: ${door_cycle_time}
- switch.turn_off: door_1_open
- platform: gpio
id: door_1_close
pin: GPIO13
inverted: false
interlock: [door_1_open]
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
- delay: ${door_cycle_time}
- switch.turn_off: door_1_close
- platform: gpio
id: door_2_open
pin: GPIO23
inverted: false
interlock: [door_2_close]
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
- delay: ${door_cycle_time}
- switch.turn_off: door_2_open
- platform: gpio
id: door_2_close
pin: GPIO22
inverted: false
interlock: [door_2_open]
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
- delay: ${door_cycle_time}
- switch.turn_off: door_2_close
- platform: gpio
id: door_3_open
pin: GPIO19
inverted: false
interlock: [door_3_close]
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
- delay: ${door_cycle_time}
- switch.turn_off: door_3_open
- platform: gpio
id: door_3_close
pin: GPIO18
inverted: false
interlock: [door_3_open]
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
- delay: ${door_cycle_time}
- switch.turn_off: door_3_close
cover:
- platform: template
name: "Door 1"
id: door_1
device_class: garage
optimistic: true
open_action:
- switch.turn_on: door_1_open
close_action:
- switch.turn_on: door_1_close
stop_action:
- switch.turn_off: door_1_open
- switch.turn_off: door_1_close
- platform: template
name: "Door 2"
id: door_2
device_class: garage
optimistic: true
open_action:
- switch.turn_on: door_2_open
close_action:
- switch.turn_on: door_2_close
stop_action:
- switch.turn_off: door_2_open
- switch.turn_off: door_2_close
- platform: template
name: "Door 3"
id: door_3
device_class: garage
optimistic: true
open_action:
- switch.turn_on: door_3_open
close_action:
- switch.turn_on: door_3_close
stop_action:
- switch.turn_off: door_3_open
- switch.turn_off: door_3_close
I’m using the following automation:
alias: Adjust chicken coop door closing time
description: Closing time when solar elevation reaches civil twilight. (0-6°)
trigger:
- platform: numeric_state
entity_id:
- sensor.sun_solar_elevation
below: -4
condition: []
action:
- delay:
hours: 0
minutes: 5
seconds: 0
milliseconds: 0
- target:
entity_id: time.esp_chicken_coop_doors_time_close
data:
time: "{{ (now() - timedelta(minutes=5)).strftime('%H:%M:%S') }}"
action: time.set_value
mode: single
The problem I’m facing is that sometimes the internal automation in ESPHome doesn’t work correctly.