Esphome stepper - save to flash with global variables

Hello. I need some help. I am trying to control a thermostat knob with esphome stepper motor. Everything works fine but I can’t save the steps value on flash memory of esp32 (in order to survive power loss). I have read posts that mention how to save to flash with global variables but whatever I tried didn’t work. If anyone has any suggestions it would really help. This is my esp code:

esphome:
  name: thermostat
  platform: ESP32
  board: nodemcu-32s
  on_boot:
    - priority: -200
      then:
      - stepper.report_position: # Set stepper to global variable
          id: thermostat
          position: !lambda return id(thermostat_global);
      - stepper.set_target: # Set stepper to global variable
          id: thermostat
          target: !lambda return id(thermostat_global);
globals:
  - id: thermostat_global # Integer for storing the stepper position in case of reboot
    type: int
    restore_value: True  
#Enable logging
logger:
  level: debug
stepper:
  - platform: uln2003
    id: thermostat
    pin_a: GPIO19
    pin_b: GPIO18
    pin_c: GPIO5
    pin_d: GPIO17
    max_speed: 250 steps/s
    step_mode: FULL_STEP
    sleep_when_done: YES
    acceleration: inf
    deceleration: inf
api:
  services:
    - service: control_stepper
      variables:
       target: int
      then:
        - stepper.set_target:
           id: thermostat
           target: !lambda 'return target;'
        - globals.set: # Set global to current position
            id: thermostat_global
            value: !lambda return id(thermostat).current_position; 
number:
  - platform: template
    name: "Thermostat stepper"
    optimistic: true
    min_value: 0
    max_value: 3000
    step: 1
    on_value:
      then:
        - stepper.set_target:
            id: thermostat
            target: !lambda "return x;"

i’m interested too

An example that works here:

# default HA integration, OTA updater and backup http web portal

ota:
   - platform: esphome
#    safe_mode: true
#    password: !secret ota_password
  
#########################
web_server:
  port: 80
  version: 3
  js_url: https://oi.esphome.io/v3/www.js
  
wifi:
  domain: .lan
 # use_address: oel-fuellstand-sensor
  networks:
    - ssid: !secret wifi_ssid1        
      password: !secret wifi_password1    
    - ssid: !secret wifi_ssid       
      password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${name}_Hotspot"
    password: ""

captive_portal:

# Enable logging
logger:
  level: debug

esphome:
  name: thermostat
  platform: ESP32
  board: esp32dev
  on_boot:
    priority: -200
    then:
      - logger.log:
          format: "Restoring thermostat_global: %f"
          args: [ 'id(thermostat_global)' ]
      - globals.set:
          id: thermostat_global
          value: !lambda return id(thermostat_global);
      - stepper.report_position:
          id: thermostat
          position: !lambda return id(thermostat_global);
      - stepper.set_target:
          id: thermostat
          target: !lambda return id(thermostat_global);
globals:
  - id: thermostat_global
    type: float
    restore_value: true

stepper:
  - platform: uln2003
    id: thermostat
    pin_a: GPIO19
    pin_b: GPIO18
    pin_c: GPIO5
    pin_d: GPIO17
    max_speed: 250 steps/s
    step_mode: FULL_STEP
    sleep_when_done: YES
    acceleration: inf
    deceleration: inf
api:
  encryption:
    key: DHrH6oy4444444Wzk0jnPsk9qLM6oOHzc1vdOaLcgsQo=
  services:
    - service: control_stepper
      variables:
       target: float
      then:
        - stepper.set_target:
           id: thermostat
           target: !lambda 'return target;'
        - globals.set: # Set global to current position
            id: thermostat_global
            value: !lambda return id(thermostat).current_position; 
number:
  - platform: template
    name: "Thermostat stepper"
    optimistic: true
    min_value: 0
    max_value: 3000
    step: 1
    restore_value: true
    on_value:
      then:
        - stepper.set_target:
            id: thermostat
            target: !lambda "return x;"
        - globals.set:
            id: thermostat_global
            value: !lambda "return x;"
        - logger.log:
            format: "Saved thermostat_global: %f"
            args: [ 'x' ]


Here is a log of how the app works:

Compressed 8192 bytes to 47...
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.1 seconds (effective 536.1 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
INFO Successfully uploaded program.
INFO Starting log output from COM3 with baud rate 115200
[08:52:29][I][logger:156]: Log initialized
[08:52:29][C][safe_mode:079]: There have been 0 suspected unsuccessful boot attempts
[08:52:29][D][esp32.preferences:114]: Saving 1 preferences to flash...
[08:52:29][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[08:52:29][I][app:029]: Running through setup()...
[08:52:29][D][number:012]: 'Thermostat stepper': Sending state 2750.000000
[08:52:29][D][main:103]: Saved thermostat_global: 2750.000000
[08:52:29][C][wifi:048]: Setting up WiFi...
[08:52:29][C][wifi:061]: Starting WiFi...
[08:52:29][C][wifi:062]:   Local MAC: 30:AE:A4:
[08:52:29][D][wifi:482]: Starting scan...
[08:52:29][W][component:157]: Component wifi set Warning flag: scanning for networks
[08:52:35][D][wifi:497]: Found networks:
[08:52:35][I][wifi:541]: - 'wifiK' (BC:EA:FA:) ▂▄▆█
[08:52:35][D][wifi:542]:     Channel: 6
[08:52:35][D][wifi:543]:     RSSI: -78 dB
[08:52:35][I][wifi:541]: - 'wifiK' (D0:D3:E0:) ▂▄▆█
[08:52:35][D][wifi:542]:     Channel: 1
[08:52:35][D][wifi:543]:     RSSI: -90 dB
[08:52:35][D][wifi:546]: - '' (D0:D3:) ▂▄▆█
[08:52:35][I][wifi:313]: WiFi Connecting to 'wifiK'...
[08:52:36][I][wifi:617]: WiFi Connected!
[08:52:36][C][wifi:428]:   Local MAC: 30:AE:A4:
[08:52:36][C][wifi:433]:   SSID: 'wifiK'
[08:52:36][C][wifi:436]:   IP Address: 10.1.1.65
[08:52:36][C][wifi:440]:   BSSID: BC:EA:FA:
[08:52:36][C][wifi:441]:   Hostname: 'thermostat'
[08:52:36][C][wifi:443]:   Signal strength: -78 dB ▂▄▆█
[08:52:36][C][wifi:447]:   Channel: 6
[08:52:36][C][wifi:448]:   Subnet: 255.255.255.0
[08:52:36][C][wifi:449]:   Gateway: 10.1.1.254
[08:52:36][C][wifi:450]:   DNS1: 1.1.1.1
[08:52:36][C][wifi:451]:   DNS2: 1.1.1.1
[08:52:36][D][wifi:626]: Disabling AP...
[08:52:36][C][web_server:100]: Setting up web server...
[08:52:36][C][api:026]: Setting up Home Assistant API server...
[08:52:36][D][main:110]: Restoring thermostat_global: 2750.000000
[08:52:36][I][app:062]: setup() finished successfully!
[08:52:36][W][component:170]: Component wifi cleared Warning flag
[08:52:36][W][component:157]: Component api set Warning flag: unspecified
[08:52:36][I][app:100]: ESPHome version 2024.10.3 compiled on Nov 21 2024, 15:28:27
[08:52:36][C][wifi:600]: WiFi:
[08:52:36][C][wifi:428]:   Local MAC: 30:AE:A4:
[08:52:36][C][wifi:433]:   SSID: 'wifiK'
[08:52:36][C][wifi:436]:   IP Address: 10.1.1.65
[08:52:36][C][wifi:440]:   BSSID: BC:EA:FA:
[08:52:36][C][wifi:441]:   Hostname: 'thermostat'
[08:52:36][C][wifi:443]:   Signal strength: -78 dB ▂▄▆█
[08:52:36][C][wifi:447]:   Channel: 6
[08:52:36][C][wifi:448]:   Subnet: 255.255.255.0
[08:52:36][C][wifi:449]:   Gateway: 10.1.1.254
[08:52:36][C][wifi:450]:   DNS1: 1.1.1.1
[08:52:36][C][wifi:451]:   DNS2: 1.1.1.1
[08:52:36][C][logger:185]: Logger:
[08:52:36][C][logger:186]:   Level: DEBUG
[08:52:36][C][logger:188]:   Log Baud Rate: 115200
[08:52:36][C][logger:189]:   Hardware UART: UART0
[08:52:36][C][uln2003.stepper:037]: ULN2003:
[08:52:36][C][uln2003.stepper:038]:   Pin A: GPIO19
[08:52:36][C][uln2003.stepper:039]:   Pin B: GPIO18
[08:52:36][C][uln2003.stepper:040]:   Pin C: GPIO5
[08:52:36][C][uln2003.stepper:041]:   Pin D: GPIO17
[08:52:36][C][uln2003.stepper:042]:   Sleep when done: YES
[08:52:36][C][uln2003.stepper:055]:   Step Mode: FULL STEP
[08:52:36][C][template.number:050]: Template Number 'Thermostat stepper'
[08:52:36][C][template.number:051]:   Optimistic: YES
[08:52:36][C][template.number:052]:   Update Interval: 60.0s
[08:52:36][C][captive_portal:089]: Captive Portal:
[08:52:36][C][web_server:153]: Web Server:
[08:52:36][C][web_server:154]:   Address: thermostat.lan:80
[08:52:36][C][mdns:116]: mDNS:
[08:52:36][C][mdns:117]:   Hostname: thermostat
[08:52:36][C][esphome.ota:073]: Over-The-Air updates:
[08:52:36][C][esphome.ota:074]:   Address: thermostat.lan:3232
[08:52:36][C][esphome.ota:075]:   Version: 2
[08:52:36][C][safe_mode:018]: Safe Mode:
[08:52:36][C][safe_mode:020]:   Boot considered successful after 60 seconds
[08:52:36][C][safe_mode:021]:   Invoke after 10 boot attempts
[08:52:36][C][safe_mode:023]:   Remain in safe mode for 300 seconds
[08:52:36][C][api:140]: API Server:
[08:52:36][C][api:141]:   Address: thermostat.lan:6053
[08:52:36][C][api:143]:   Using noise encryption: YES
[08:53:29][I][safe_mode:041]: Boot seems successful; resetting boot loop counter
[08:53:29][D][esp32.preferences:114]: Saving 1 preferences to flash...
[08:53:29][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[08:54:25][D][number:054]: 'Thermostat stepper' - Setting number value
[08:54:25][D][number:113]:   New number value: 1588.000000
[08:54:25][D][number:012]: 'Thermostat stepper': Sending state 1588.000000
[08:54:25][D][main:103]: Saved thermostat_global: 1588.000000
[08:55:10]

esp8266 must be enabled flash. esp32 may also be required.

esp8266:
board: esp01_1m
restore_from_flash: True