I used a D1 Mini to automate an air freshener following these instructions. It worked well for a month or so but now will not stay connected to my network. A snippet of my ESPHome log:
I tried reflashing the D1 Mini and it would work for a few minutes but then the same thing would happen. I figured I had a faulty D1 so started from scratch with a new D1 Mini. It worked for about 10 minutes then the same thing happened. Reflashed it and it worked again for about 20 minutes before failing. Does anyone have an idea of what might be going on? My code:
substitutions:
bright_level: "50"
dark_level: "10"
dark_mode_sprays: "1" # Number of times to spray in Bathroom Mode
# Set the Interval Timings here (Minutes)
interval1: "5"
interval2: "15"
interval3: "30"
interval4: "60"
globals:
- id: sprays
type: float
restore_value: yes
esphome:
name: "air-freshener"
friendly_name: Air Freshener
esp8266:
board: d1_mini
restore_from_flash: true
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "hx3CGH3gUAIOyMitRSEQR1twCW7Mxj+0svpoSHyviAo="
ota:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esphome-Web-Ee2Ead"
password: "jIAnWwI3nU0z"
captive_portal:
binary_sensor:
- platform: gpio
id: reset
pin: D4
filters:
- invert:
on_click:
min_length: 500ms
max_length: 5000ms
then:
- light.turn_on:
id: led
effect: flashfast
- lambda: |-
id(sprays) = 0;
select:
- platform: template
id: set_interval
name: Interval
icon: mdi:timer-outline
optimistic: true
restore_value: true
options:
- 0 (Bathroom Mode)
- $interval1 Minutes
- $interval2 Minutes
- $interval3 Minutes
- $interval4 Minutes
on_value:
- if:
condition:
- switch.is_on: enable
- lambda: "return atoi(x.c_str()) >= 1;"
then:
- switch.turn_on: spray
sensor:
- platform: template
id: can_level
name: "Can Level"
device_class: pressure
unit_of_measurement: "Can"
update_interval: 2s
lambda: |-
return ( (2500-id(sprays) )/2500 ) * 100;
- platform: adc
id: brightness
name: Lux
device_class: illuminance
unit_of_measurement: lx
pin: A0
update_interval: 1s
filters:
- lambda: return 85-(x * 150);
- sliding_window_moving_average:
window_size: 3
send_every: 3
on_value:
- if:
condition:
for:
time: 20min # Waits for this time before resetting 'Dark Mode'. Default 20min
condition:
and:
- switch.is_on: enable
- lambda: "return atoi(id(set_interval).state.c_str()) == 0;"
- lambda: "return id(brightness).state < $dark_level;"
- switch.is_off: its_dark
then:
- switch.turn_on: its_dark
- if:
condition:
and:
- switch.is_on: enable
- lambda: "return atoi(id(set_interval).state.c_str()) == 0;"
- lambda: "return id(brightness).state > $bright_level;"
- switch.is_on: its_dark
then:
- switch.turn_on: spray
- delay: !lambda return (1500 * $dark_mode_sprays);
- switch.turn_off: its_dark
switch:
- platform: gpio
id: spray
name: Spray
icon: mdi:spray
pin: D2
on_turn_on:
- light.turn_on:
id: led
effect: pulser
- switch.turn_on: timer
- delay: 0.14s
- switch.turn_off: spray
- lambda: |-
id(sprays) += 1;
- platform: template
id: enable
name: Enable
icon: mdi:autorenew
optimistic: true
on_turn_on:
- switch.turn_on: timer
- platform: template
id: its_dark
name: Room is Dark
icon: mdi:brightness-4
optimistic: true
- platform: template
id: timer
optimistic: true
on_turn_on:
- delay: !lambda return atoi(id(set_interval).state.c_str()) * (60000); # Default 60000
- switch.turn_off: timer
- if:
condition:
- switch.is_on: enable
- lambda: "return atoi(id(set_interval).state.c_str()) >= 1;"
then:
- light.turn_on:
id: led
effect: flashfast
- delay: 1.0s
- light.turn_off: led
- switch.turn_on: spray
light:
- platform: monochromatic
id: led
name: LED
output: out_led
default_transition_length: 0s
effects:
- pulse:
name: pulser
transition_length: 0.0s
update_interval: 0.25s
- pulse:
name: flashfast
transition_length: 0.0s
update_interval: 0.025s
on_turn_on:
- delay: 3s
- light.turn_off: led
output:
platform: esp8266_pwm
pin:
number: D3
inverted: true
id: out_led
HA Errors:
- Can’t connect to ESPHome API for air-freshener @ 192.168.1.126: Handshake timed out (TimeoutAPIError)
- Can’t connect to ESPHome API for air-freshener @ 192.168.1.126: Error connecting to [AddrInfo(family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, proto=6, sockaddr=IPv4Sockaddr(address=‘192.168.1.126’, port=6053))]: [Errno 111] Connect call failed (‘192.168.1.126’, 6053) (SocketAPIError)