substitutions: device_name: "steppertest" friendly_name: "stepper" comment: "esp32-S2, motor" location: "zolder" api_password: !secret stepper_api ota_password: !secret ota_password wifi_ssid: !secret wifi_ssid wifi_password: !secret wifi_password gateway: !secret ip_gateway subnet: !secret ip_subnet ip: !secret stepper_ip update_interval: 30s pin_mot_rst: GPIO18 pin_mot_slp: GPIO21 pin_mot_dir: GPIO17 pin_mot_stp: GPIO16 blinds_name: speelkamer packages: board: !include boards/esp32_s2_arduino.yaml device_base: !include common/common.yaml connection: !include common/wifi.yaml logger: !include templates/logger.yaml api: services: - service: control_stepper variables: target: int then: - stepper.set_target: id: stepper_motor target: !lambda 'return target;' esphome: on_boot: - priority: -200.0 then: - stepper.report_position: # Set stepper to global variable id: stepper_motor position: !lambda return id(stepper_motor_global); - stepper.set_target: # Set stepper to global variable id: stepper_motor target: !lambda return id(stepper_motor_global); - if: # If blind is Closed condition: - lambda: 'return id(stepper_motor_global) == 0;' then: # Publish state etc. - cover.template.publish: id: ${blinds_name} state: CLOSED current_operation: IDLE - if: # If blind is Open condition: - lambda: 'return id(stepper_motor_global) == id(endstop).state;' then: # Publish state etc. - cover.template.publish: id: ${blinds_name} state: OPEN current_operation: IDLE - if: # If blind is Neither condition: - lambda: 'return (id(stepper_motor_global) != 0) && (id(stepper_motor_global) != id(endstop).state);' then: # # Publish state etc. - cover.template.publish: id: ${blinds_name} position: !lambda 'return (float(float(id(stepper_motor).current_position) / float(id(endstop).state)));' current_operation: IDLE globals: - id: stepper_motor_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: '750' # this is the max value # this is the max value sensor: - platform: template name: steppe_pos id: stepper_pos number: - platform: template name: endstop Control id: endstop min_value: 0 max_value: 2000 step: 1 mode: slider unit_of_measurement: steps restore_value: True optimistic: True # set_action: # then: # - globals.set: # id: endstop # value: !lambda 'return x;' stepper: - platform: a4988 id: stepper_motor step_pin: ${pin_mot_stp} dir_pin: ${pin_mot_dir} max_speed: 250 # Optional: sleep_pin: ${pin_mot_slp} acceleration: inf deceleration: inf cover: - platform: template name: Dining Room Blinds id: ${blinds_name} open_action: then: - logger.log: "Opening" - stepper.set_target: id: stepper_motor target: 750 - while: condition: lambda: 'return id(stepper_motor).current_position < id(endstop).state;' then: - cover.template.publish: id: ${blinds_name} position: !lambda 'return (float(float(id(stepper_motor).current_position) / float(id(endstop).state)));' current_operation: OPENING - delay: 1000 ms - globals.set: # Set global to current position id: stepper_motor_global value: !lambda return id(stepper_motor).current_position; - sensor.template.publish: id: stepper_pos state: !lambda return id(stepper_motor).current_position; - globals.set: # Set toggle to OPEN (No need for 'optimistic mode') id: openclosed value: '1' - cover.template.publish: id: ${blinds_name} state: OPEN current_operation: IDLE close_action: then: - logger.log: "Closing" - stepper.set_target: # Send stepper to 0 id: stepper_motor target: '0' - while: condition: lambda: 'return id(stepper_motor).current_position > 0;' then: - cover.template.publish: id: ${blinds_name} position: !lambda 'return (float(float(id(stepper_motor).current_position) / float(id(endstop).state)));' current_operation: CLOSING - delay: 1000 ms - globals.set: # Set global to current position id: stepper_motor_global value: !lambda return id(stepper_motor).current_position; - sensor.template.publish: id: stepper_pos state: !lambda return id(stepper_motor).current_position; - globals.set: # Set toggle to CLOSED (No need for 'optimistic mode') id: openclosed value: '0' - cover.template.publish: id: ${blinds_name} state: CLOSED current_operation: IDLE position_action: then: - stepper.set_target: id: stepper_motor target: !lambda return int(id(endstop).state * pos); - while: condition: lambda: 'return id(stepper_motor).current_position != int(id(endstop).state * pos);' then: - cover.template.publish: id: ${blinds_name} position: !lambda 'return (float(float(id(stepper_motor).current_position) / float(id(endstop).state)));' - delay: 1000 ms - globals.set: # Set global to current position id: stepper_motor_global value: !lambda return id(stepper_motor).current_position; - sensor.template.publish: id: stepper_pos state: !lambda return id(stepper_motor).current_position; - cover.template.publish: id: ${blinds_name} position: !lambda 'return (float(float(id(stepper_motor).current_position) / float(id(endstop).state)));' current_operation: IDLE stop_action: then: - stepper.set_target: id: stepper_motor target: !lambda return id(stepper_motor).current_position; - globals.set: # Set global to current position id: stepper_motor_global value: !lambda return id(stepper_motor).current_position; - sensor.template.publish: id: stepper_pos state: !lambda return id(stepper_motor).current_position; - cover.template.publish: id: ${blinds_name} position: !lambda 'return (float(float(id(stepper_motor).current_position) / float(id(endstop).state)));' current_operation: IDLE has_position: true device_class: blind switch: - platform: template name: Reset Dining Room Blinds id: reset turn_on_action: - stepper.set_target: id: stepper_motor target: !lambda 'return id(endstop).state * 2;' - wait_until: lambda: 'return id(stepper_motor).current_position == id(endstop).state*2;' - delay: 1s - stepper.set_target: id: stepper_motor target: '0' - globals.set: # Set global to current position id: stepper_motor_global value: !lambda return id(stepper_motor).current_position; - sensor.template.publish: id: stepper_pos state: !lambda return id(stepper_motor).current_position; - globals.set: # Set toggle to CLOSED (No need for 'optimistic mode') id: openclosed value: '0' - cover.template.publish: id: ${blinds_name} state: CLOSED current_operation: IDLE - switch.turn_off: reset # - platform: output # name: "A4988 reset" # output: 'A4988reset' # internal: false # id: swreset output: - platform: gpio id: 'A4988reset' pin: ${pin_mot_rst} inverted: True # cover: # - platform: template # name: "Template Cover" # lambda: |- # open_action: # - switch.turn_on: open_cover_switch # close_action: # - switch.turn_on: close_cover_switch # stop_action: # - switch.turn_on: stop_cover_switch # optimistic: true