How do I remember the position of a blind through power cycles? I have tried various attempts using global variables but haven’t managed to get it working yet so any ideas based on the code below would be very welcome, please.
The blind is driven by a DC motor, controlled by an H-bridge and uses an optical endstop to detect fully open, with a rotary encoder to detect position and to provide a fully closed endstop function.
At the moment I have the blind do an initialise sequence to determine the fully open position when power is restored but I’d like it to remember the position and resume normal operation from there.
esphome:
name: blind_utility_room
platform: ESP8266
board: d1_mini
on_boot:
priority: -10
then:
# the next few lines force the blind to a known state by first closing the blind for 1s, then opening it until the optical endstop is reached
- switch.turn_off: blind_s1
- switch.turn_on: blind_s2
- delay: 1s
- switch.turn_off: blind_s2
- cover.open: utility_blind
globals:
- id: closed_steps # how many encoder steps when fully closed
type: int
restore_value: no
initial_value: '100'
- id: manual_action # used to allow the binary touch switch to raise or lower the blind
type: int
restore_value: no
initial_value: '0'
wifi:
ssid: !secret wifi_name
password: !secret wifi_password
domain: !secret wifi_domain
manual_ip:
static_ip: A.B.C.D
gateway: A.B.C.X
subnet: 255.255.255.0
# Enable logging
logger:
level: DEBUG
logs:
light: NONE
# Enable Home Assistant API
api:
ota:
sensor:
- platform: rotary_encoder
name: "Rotary Encoder"
pin_b: D1 #GPIO05
pin_a: D2 #GPIO04
id: encoder
on_value:
then:
- if:
condition: #used to stop the blind when it gets to fully closed position
lambda: return id(encoder).state >= id(closed_steps) ;
then:
cover.stop: utility_blind
- platform: wifi_signal
name: "WiFi - Utility Room Blind"
update_interval: 60s
binary_sensor:
- platform: gpio # for optical endstop at fully open position
pin:
number: D5 #GPIO14
mode: INPUT_PULLUP
inverted: FALSE
id: endstop
name: "Endstop"
internal: True
on_press: # used to light the LED when the endstop is reached (just visual, not used programatically)
then:
- light.turn_on: endstop_led
- cover.stop: utility_blind
- sensor.rotary_encoder.set_value:
id: encoder
value: 0
on_release:
then:
- light.turn_off: endstop_led
- platform: gpio
pin:
number: D0 #GPIO16
mode: INPUT_PULLUP
inverted: FALSE
id: touch_switch
name: "Touch_Switch"
internal: True
on_press: # used to allow manual movement of the blind (either Open or Close)
then:
- if:
condition:
lambda: return id(manual_action) == 0 ;
then:
- cover.close: utility_blind
- globals.set:
id: manual_action
value: '1'
else:
- cover.open: utility_blind
- globals.set:
id: manual_action
value: '0'
output:
# two outputs to H-bridge for direction and movement
- platform: gpio
id: 'blind1'
pin: D6 #GPIO12
- platform: gpio
id: 'blind2'
pin: D7 #GPIO#13
# for LED indicator
- platform: esp8266_pwm
pin: D3 #GPIO00
inverted: TRUE
frequency: 1000 Hz
id: led
light:
# LED indicator light
- platform: monochromatic
output: led
name: "Endstop_Reached"
id: endstop_led
internal: True
default_transition_length: 0s
switch:
# This is to restart the ESPHome device remotely
- platform: restart
name: "Restart ESPHome - Utility Room Blind"
# For the H-bridge outputs
- platform: output
name: "blind1"
output: 'blind1'
id: blind_s1
internal: true
- platform: output
name: "blind2"
output: 'blind2'
id: blind_s2
internal: true
cover:
- platform: template
name: "Utility Room Blind"
id: utility_blind
assumed_state: true
optimistic: true
device_class: shade
open_action:
- switch.turn_off: blind_s2
- switch.turn_on: blind_s1
- globals.set: # reverses the manual direction for next manual action
id: manual_action
value: '0'
- delay: 30s
- switch.turn_off: blind_s1
close_action:
- switch.turn_off: blind_s1
- switch.turn_on: blind_s2
- globals.set: # reverses the manual direction for next manual action
id: manual_action
value: '1'
- delay: 30s
- switch.turn_off: blind_s2
stop_action:
- switch.turn_off: blind_s1
- switch.turn_off: blind_s2