Difficulties with global variables in ESPHome and how to read them in Home Assistant

Hello!

I’m having difficulties with global variables (I need to save the variable value over boots) and how to read them in Home Assistant.

Purpose in brief: I’m running a stepper motor with Home Assistant automation and I want to save the value of the stepper motor position (integer) in ESP32 so in case of lost connection etc. boot the value is saved in the ESP32. It’s very important that the stepper motor position doesn’t exceed certain values, let’s say the value should always be between 0 and 6000. Therefore, I need to keep track of the position in all cases. I have no problems turning the stepper motor so that part works.

For this I need to create a global variable in ESP32 which has the restore flag set to yes:

globals:
  - id: stepper_position_esp32
    type: int
    restore_value: yes
    initial_value: '0'

I assume the above code should be correct?

Then I set that value in the API call which is used to turn the stepper motor:

api:
  services:
    - service: stepper_control
      variables:
        target: int
      then:
        - stepper.set_target:
            id: nibe_stepper
            target: !lambda 'return target;'  
        - globals.set:
            id: stepper_position_esp32
            value: !lambda 'return target;'

I assume I might have some mistake here? I would also like to add here an extra verification that the stepper motor is turned only if the target is within the accepted range. What could the syntax for this? Is there some other steps I should take in order to prevent stepper motor stepping outside accepted range?

How could I then read the variable stepper_position_esp32 from Home Assistant? There are lot of discussion how to expose Home Assistant variables for ESP32 but I would like to do the opposite. I tried to adding a sensor and that works in a way that when Home Assistant changes that value, it changes in ESP32 also but that’s not what I’m looking for here. Home Assistant should not be able to change that value, just read that. Stepper motor turning is then done with the automation which calls the API to turn the stepper motor and writes the new value for stepper motor position (variable stepper_position_esp32).

I’m probably a little lost here but I have found many good tips on this forum so I hope little guidance could be given here as well. :blush:

Hi, still looking for a solution?

i do some stuff inside esp.yamls and do define globals inside to make calculations with lambda. Theese globals can NOT be seen directly from outside by HA.

The solution ist to create a TEMPLATE sensor / or binary_sensor or text sensor inside esp and load the global values by a lambda call like:

  - platform: template
    name: minutensignal_esp
    id: minutensignal_esp
    state_class: measurement
    update_interval: 500ms
    accuracy_decimals: 0
    lambda: |-
      return id((minutensignal)) ;

minutensignal is the global variable inside esp.
iside HA you can see the minutensignal_esp

hope this helps
BR