ESPHome Time-based cover at boot

Hi all,
i’m using time-based cover esphome integration on a wemos d1 mini. It is working but i noticed that at wemos reboot cover position always starts at 50%, so the sync with real position is lost. I’m tryng to use globals to store cover position but i can’t get it working. It always start at 50% at reboot. Where i’m wrong?
Below my related code…
I’m sorry but i’m new to esphome

esphome:
  name: tapparelle_zona_giorno
  platform: ESP8266
  board: d1_mini
  on_boot:
    priority: -10
    # ...
    then:
      - lambda: |-
          id(my_cover_1).publish_state(my_cover_1_global);
          id(my_cover_2).publish_state(my_cover_2_global);
globals:
  - id: my_cover_1_global
    type: float
    restore_value: yes
#    initial_value: '0'
  - id: my_cover_2_global
    type: float
    restore_value: yes
#    initial_value: '0'
cover:
- platform: time_based
  name: "Tapparella 1"
  id: my_cover_1
  open_action:
    - switch.turn_on: on_cover_1
    - globals.set:
          id: my_cover_1_global
          value: !lambda |-
            return id(my_cover_1).position;
  open_duration: 24s
  close_action:
    - switch.turn_on: on_cover_1
    - switch.turn_on: dir_cover_1
    - globals.set:
          id: my_cover_1_global
          value: !lambda |-
            return id(my_cover_1).position;
  close_duration: 22s
  stop_action:
    - switch.turn_off: on_cover_1
    - switch.turn_off: dir_cover_1
    - globals.set:
          id: my_cover_1_global
          value: !lambda |-
            return id(my_cover_1).position;
    
- platform: time_based
  name: "Tapparella 2"
  id: my_cover_2
  open_action:
    - switch.turn_on: on_cover_2
    - globals.set:
          id: my_cover_2_global
          value: !lambda |-
            return id(my_cover_2).position;
  open_duration: 24s
  close_action:
    - switch.turn_on: on_cover_2
    - switch.turn_on: dir_cover_2
    - globals.set:
          id: my_cover_2_global
          value: !lambda |-
            return id(my_cover_2).position;
  close_duration: 22s
  stop_action:
    - switch.turn_off: on_cover_2
    - switch.turn_off: dir_cover_2
    - globals.set:
          id: my_cover_2_global
          value: !lambda |-
            return id(my_cover_2).position;

I think you need to enable the restore capability
ESP8266 Options:

  • esp8266_restore_from_flash ( Optional , boolean): Whether to save & restore data from flash on ESP8266s. Defaults to no . See esp8266_restore_from_flash for more info

Also probably a good idea to have the initial_value set as well for backup in case restore does not work

1 Like

The restore from flash should work. You may want to consider the esp32, which supposedly has more robust flash writes, but based on your code this won’t be writing too often.

Also in this code, will the state be set by the current state of the cover not the final desired state? Have you checked the logs while it is running to make sure you are setting your global variables the way you want?

Thank you!!! It works!
I think i will modify the way position is saved to limit writes… I think to save position only when cover state become IDLE instead to save at each movement . How can i add this mode? Do i need a script to do this?
Edit: i think i can do this by only leaving globals.set on cover stop_action. What do you think?

FINAL EDIT: enabling esp8266_restore_from_flash all the automations that i added to manage globals (and even globals too) are unnecessary: cover position is natively saved through reboots.