Created
January 30, 2021 19:52
-
-
Save boelle/b08c2c78948f243f9a44a551cb0f4775 to your computer and use it in GitHub Desktop.
Esphome code for roller blinds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) Press button for > 1 second to enter setup mode | |
# 2) Press button again to start the blind closing | |
# 3) Press button again when closed and blind starts to open (actually resets the stepper position to 0) | |
# 4) Press button again when blind is fully open | |
# 5) Job Done | |
# Button is also used to open/close the blind (must be fully open/closed first) | |
esphome: | |
name: gardin_kontor | |
platform: ESP8266 | |
board: nodemcuv2 | |
esp8266_restore_from_flash: True | |
on_boot: | |
- priority: -200.0 | |
then: | |
- stepper.report_position: # Set stepper to global variable | |
id: my_stepper | |
position: !lambda return id(my_stepper_global); | |
- stepper.set_target: # Set stepper to global variable | |
id: my_stepper | |
target: !lambda return id(my_stepper_global); | |
- if: # If blind is Closed | |
condition: | |
- lambda: 'return id(my_stepper_global) == 0;' | |
then: # Publish state etc. | |
- cover.template.publish: | |
id: blinded | |
state: CLOSED | |
current_operation: IDLE | |
- if: # If blind is Open | |
condition: | |
- lambda: 'return id(my_stepper_global) == id(endstop);' | |
then: # Publish state etc. | |
- cover.template.publish: | |
id: blinded | |
state: OPEN | |
current_operation: IDLE | |
- if: # If blind is Neither | |
condition: | |
- lambda: 'return (id(my_stepper_global) != 0) && (id(my_stepper_global) != id(endstop));' | |
then: # # Publish state etc. | |
- cover.template.publish: | |
id: blinded | |
position: !lambda 'return (float(float(id(my_stepper).current_position) / float(id(endstop))));' | |
current_operation: IDLE | |
wifi: | |
ssid: !secret wifi_ssid | |
password: !secret wifi_password | |
ap: | |
ssid: Roller Blind | |
password: !secret ap_password | |
web_server: | |
port: 80 | |
logger: | |
api: | |
ota: | |
captive_portal: | |
stepper: | |
- platform: a4988 | |
id: my_stepper | |
dir_pin: | |
number: D3 | |
inverted: True | |
step_pin: D2 | |
sleep_pin: D1 | |
max_speed: 500 steps/s # Set the speed of the motor | |
acceleration: 300 | |
deceleration: 300 | |
globals: | |
- id: my_stepper_global # Integer for storing the stepper position in case of reboot | |
type: int | |
restore_value: True | |
initial_value: '0' | |
- id: openclosed # Boolean to store OPEN/CLOSED state | |
type: bool | |
restore_value: True | |
initial_value: '0' | |
- id: endstop # Variable for storing ENDSTOP (how far to move stepper) | |
type: int | |
restore_value: True | |
initial_value: '1000' | |
- id: settingmode # Integer for Setup Mode | |
type: int | |
restore_value: no | |
initial_value: '0' | |
binary_sensor: | |
- platform: gpio | |
pin: | |
number: D6 # Connect Button to D6 and GND | |
mode: INPUT_PULLUP | |
inverted: True | |
name: Button | |
internal: True | |
on_click: | |
- min_length: 50ms | |
max_length: 500ms | |
then: # Short press to OPEN/CLOSE blinds and also for setting up | |
- if: # If settings variable is on | |
condition: | |
- lambda: 'return id(settingmode) != 0;' | |
then: # Enter Setting Mode | |
- script.execute: setupbutton | |
else: | |
- if: # If blind is closed | |
condition: | |
- lambda: 'return id(openclosed) == 0;' | |
then: # Open blind | |
- cover.open: blinded | |
else: # Close blind | |
- cover.close: blinded | |
- min_length: 1000ms | |
max_length: 3000ms | |
then: # Long press to Enter Setting Mode | |
- logger.log: "Entered Settings Mode" | |
- globals.set: | |
id: settingmode | |
value: '1' | |
switch: | |
- platform: template | |
name: Roller Blind Setup Switch # Switch to enter Setup Mode | |
id: setupswitch | |
lambda: |- | |
if (id(settingmode) != 0) { | |
return true; | |
} else { | |
return false; | |
} | |
turn_on_action: | |
then: | |
- logger.log: "Entered Settings Mode" | |
- globals.set: | |
id: settingmode | |
value: '1' | |
turn_off_action: | |
then: | |
- logger.log: "Exiting Settings Mode" | |
- globals.set: | |
id: settingmode | |
value: '0' | |
- platform: template | |
name: Roller Blind Setup Button # Switch to replicate the Physical Button | |
id: hasetup | |
turn_on_action: | |
- if: # If settings variable is on | |
condition: | |
- lambda: 'return id(settingmode) != 0;' | |
then: # Enter Setting Mode | |
- script.execute: setupbutton | |
- switch.turn_off: hasetup | |
cover: | |
- platform: template | |
name: "Gardin Kontor" | |
id: blinded | |
open_action: | |
then: | |
- logger.log: "Opening" | |
- stepper.set_target: # Send stepper to endstop | |
id: my_stepper | |
target: !lambda return id(endstop); | |
- while: | |
condition: | |
lambda: 'return id(my_stepper).current_position != id(endstop);' | |
then: | |
- cover.template.publish: | |
id: blinded | |
position: !lambda 'return (float(float(id(my_stepper).current_position) / float(id(endstop))));' | |
current_operation: OPENING | |
- delay: 1000 ms | |
- globals.set: # Set global to current position | |
id: my_stepper_global | |
value: !lambda return id(my_stepper).current_position; | |
- globals.set: # Set toggle to OPEN (No need for 'optimistic mode') | |
id: openclosed | |
value: '1' | |
- cover.template.publish: | |
id: blinded | |
state: OPEN | |
current_operation: IDLE | |
close_action: | |
then: | |
- logger.log: "Closing" | |
- stepper.set_target: # Send stepper to 0 | |
id: my_stepper | |
target: '0' | |
- while: | |
condition: | |
lambda: 'return id(my_stepper).current_position != 0;' | |
then: | |
- cover.template.publish: | |
id: blinded | |
position: !lambda 'return (float(float(id(my_stepper).current_position) / float(id(endstop))));' | |
current_operation: CLOSING | |
- delay: 1000 ms | |
- globals.set: # Set global to current position | |
id: my_stepper_global | |
value: !lambda return id(my_stepper).current_position; | |
- globals.set: # Set toggle to CLOSED (No need for 'optimistic mode') | |
id: openclosed | |
value: '0' | |
- cover.template.publish: | |
id: blinded | |
state: CLOSED | |
current_operation: IDLE | |
position_action: | |
then: | |
- stepper.set_target: | |
id: my_stepper | |
target: !lambda return int(id(endstop) * pos); | |
- while: | |
condition: | |
lambda: 'return id(my_stepper).current_position != int(id(endstop) * pos);' | |
then: | |
- cover.template.publish: | |
id: blinded | |
position: !lambda 'return (float(float(id(my_stepper).current_position) / float(id(endstop))));' | |
- delay: 1000 ms | |
- globals.set: # Set global to current position | |
id: my_stepper_global | |
value: !lambda return id(my_stepper).current_position; | |
- cover.template.publish: | |
id: blinded | |
position: !lambda 'return (float(float(id(my_stepper).current_position) / float(id(endstop))));' | |
current_operation: IDLE | |
stop_action: | |
then: | |
- stepper.set_target: | |
id: my_stepper | |
target: !lambda return id(my_stepper).current_position; | |
- globals.set: # Set global to current position | |
id: my_stepper_global | |
value: !lambda return id(my_stepper).current_position; | |
- cover.template.publish: | |
id: blinded | |
position: !lambda 'return (float(float(id(my_stepper).current_position) / float(id(endstop))));' | |
current_operation: IDLE | |
has_position: true | |
device_class: blind | |
script: | |
- id: setupbutton | |
then: | |
- if: | |
condition: | |
- lambda: 'return (id(settingmode) == 3);' | |
then: | |
- logger.log: "Pressed Setup Button: Mode 3" | |
- stepper.set_target: # Set Stepper position | |
id: my_stepper | |
target: !lambda return id(my_stepper).current_position; | |
- globals.set: # Set Endstop Variable | |
id: endstop | |
value: !lambda return id(my_stepper).current_position; | |
- globals.set: # Set Global stepper position | |
id: my_stepper_global | |
value: !lambda return id(my_stepper).current_position; | |
- globals.set: # Reset Setting Mode | |
id: settingmode | |
value: '0' | |
- globals.set: # Set toggle to Open | |
id: openclosed | |
value: '1' | |
- cover.template.publish: | |
id: blinded | |
state: OPEN | |
current_operation: IDLE | |
- logger.log: "Exiting Setting Mode" | |
- if: | |
condition: | |
- lambda: 'return (id(settingmode) == 2);' | |
then: | |
- logger.log: "Pressed Setup Button: Mode 2" | |
- stepper.report_position: # Reset Stepper position to 0 | |
id: my_stepper | |
position: '0' | |
- stepper.set_target: # Reset Stepper position to 0 | |
id: my_stepper | |
target: '0' | |
- globals.set: # Move stepper to 0 (doesn't move it's already there!) | |
id: my_stepper_global | |
value: '0' | |
- stepper.set_target: # Reset Stepper position to 72000 | |
id: my_stepper | |
target: '72000' | |
- globals.set: # Advance setup to next mode | |
id: settingmode | |
value: '3' | |
- if: | |
condition: | |
- lambda: 'return (id(settingmode) == 1);' | |
then: | |
- logger.log: "Pressed Setup Button: Mode 1" | |
- stepper.report_position: # Set Stepper position to 72000, makes it move to 0 (Closed) | |
id: my_stepper | |
position: '72000' | |
- globals.set: # Advance setup to next mode | |
id: settingmode | |
value: '2' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment