ESPHome Storing Stepper position on Flash

Hi, I have a UL2003 driver and a 28BYJ-48 stepper motor which I’m using to operate the tilt on my blinds with a 8266 D1 mini. I have created a custom 3D print and spindle for my unique blinds.

I can successfully automate the opening and closure of the blinds (tilt) with ESPHome. However, I’m struggling to store the motor position to flash, as the position resets to 0 when rebooted or flash a firmware update. I tried using node-red in HA, and can get the position and reset it with in ‘inject’ feature, but I want to do this on boot of the 8266 D1 Mini.

Any help or advice appreciated or any example code?

This is my ESPHome config:

#Setting up ESPHomme
esphome:
  name: blind3
  platform: ESP8266
  board: d1_mini
  esp8266_restore_from_flash: True
  on_boot:
    - priority: -200.0 #is this right priority?
      then:
      - stepper.report_position: #restore 
          id: $stepperid
          position: !lambda return id(stepper_position);
      - stepper.set_target:
          id: $stepperid
          target: !lambda return id(stepper_position);
  
#Define a global variable that can be restored from Flash
globals:
  - id: stepper_position
    type: int
    restore_value: true

#Defined values to make cloning of this easier
substitutions:    
  stepperid: stepper3
  blindname: "Blind 3"
  blindid: blind3

api:
  services:
    - service: control_blind #HA service to control blind
      variables:
        target: int
      then:
        - stepper.set_target:
            id: $stepperid
            target: !lambda 'return target;'
            
        - sensor.template.publish:
            id: position
            state: !lambda 'return target;'
    - service: reset_blind #HA service to reset the position
      variables:
        target: int
      then: 
        - stepper.report_position:
            id: $stepperid
            position: !lambda 'return target;'
        - sensor.template.publish:
            id: position
            state: !lambda 'return target;'
    

cover:
  - platform: template
    name: $blindname
    id: $blindid
    device_class: blind
    open_action: #Just an example to open the blind by increasing steps by 100
      - stepper.set_target:
          id: $stepperid
          target: !lambda return id($stepperid).current_position+100;
          
      - sensor.template.publish:
          id: position
          state: !lambda return id($stepperid).target_position;
      
    close_action: #Just an example to close the blind by decreasing steps by 100
      - stepper.set_target:
          id: $stepperid
          target: !lambda return id($stepperid).current_position-100;
          
      - sensor.template.publish:
            id: position
            state: !lambda return id($stepperid).target_position;
            
    stop_action: #Stop any movement
      - stepper.set_target:
          id: $stepperid
          target: !lambda return id($stepperid).current_position;
          
      - sensor.template.publish:
            id: position
            state: !lambda return id($stepperid).current_position;
            
    position_action:
     - globals.set: # Set global variable to current position
          id: stepper_position
          value: !lambda return id($stepperid).current_position; 
    
    optimistic: true
    assumed_state: true

sensor:
  - platform: template
    name: "$blindname Position"
    id: position

#  - platform: homeassistant #this is a HA input_number helper as experiemented using HA to store the stepper. Not used in this script
#    id: current_ha_position
#    name: "$blindname HA Position"
#    entity_id: input_number.blind3_position

stepper:
  - platform: uln2003
    id: $stepperid
    pin_a: D1
    pin_b: D2
    pin_c: D3
    pin_d: D4
    max_speed: 250 steps/s
    sleep_when_done: true #Puts the stepper to sleep to reduce power consumption and heat
    acceleration: inf
    deceleration: inf

binary_sensor:
  - platform: gpio
    pin:
      number: D6 #Physical push button to rotate the blind manually by 100 steps
      mode: INPUT_PULLUP
      inverted: True
    name: "CW"
    on_press:
      then:
        - stepper.set_target:
            id: $stepperid
            target: !lambda return id($stepperid).current_position +100;
        - logger.log: "CW Button Pressed"
            
    on_double_click: #Double click will move 1100 steps
      then:
        - stepper.set_target:
            id: $stepperid
            target: !lambda return 1100;
    on_release:
      then:
        - stepper.set_target:
            id: $stepperid
            target: !lambda return id($stepperid).current_position;

  - platform: gpio
    pin:
      number: D5 #Physical push button to rotate the blind manually by -100 steps
      mode: INPUT_PULLUP
      inverted: True
    name: "CCW"
    on_press:
      then:
        - stepper.set_target:
            id: $stepperid
            target: !lambda return id($stepperid).current_position -100;
        - logger.log: "CCW Button press"
    on_double_click: #Double click will move -1100 steps
      then:
        - stepper.set_target:
            id: $stepperid
            target: !lambda return -1100;
    on_release:
      then:
        - stepper.set_target:
            id: $stepperid
            target: !lambda return id($stepperid).current_position;
    
#Board configuration
wifi:
  ssid: !secret wifissid
  password: !secret wifipass

web_server:
  port: 80

logger:

ota:
  password: !secret blind3_otapass  
1 Like

hi pj

Have you found a solution?
I’m looking for the same .

Thanks in advance