How to run stepper motor (replacing steps) with a value from a sensor?

Hello. I’m new to esphome and I’m trying to recreate a project I created in Arduino a couple of years ago.

What I’m trying to achieve is to run the stepper motor (replacing the steps) with the value of a sensor? I already got it working with the on_click. But I want it to constantly compare the current value with the previous value of the sensor, and run the stepper motor based from the values compared.
This is the on_click script: (but I want to change this to script.execute instead.)

on_click:
  then:
    - stepper.set_target:
        id: stepper_motor
        target: !lambda return id(ambient_sensor).state;

I’m trying to write a script to activate on button click, but I don’t know how to compare the value, save the new value to current value, and compare it to the new value, and to run the motor based from the result.
This is what I have so far: which obviously is not gonna compile.

globals:
  - id: lightmode
    type: int
    restore_value: True
    initial_value: '0'

 - id: autolight
    then:
      if:
        condition:
          - lambda: ((id(lightmode)) != (id(ambient_sensor).state));
        then:    
          - stepper.set_target:
              id: stepper_motor
              target: !lambda return id(ambient_sensor).state;
          - globals.set:
              id: lightmode
              value: return id(ambient_sensor).state;
        else:
          - stepper.set_target:
              id: stepper_motor
              target: '0'

Thanks.

What is the error on the compile? You ran it as is? global.set need to add !lambda to the value and lowercase true. If you want it to constantly update place it under the ambient sensor using on_value.

Sorry for the late response.

Anyway… I tried the on_value under the ambient sensor and it works as it should be. But adding on_value under sensor interferes with another operation.

I have 2 buttons.

  • The first one manually controls the motor by turning it clockwise or counter clockwise.
  • The second button, I would like to run a script that will enable the motor to turn depending on the value of the sensor constantly. Is it possible?

Adding the on_value in a script on works whenever I press the button.

  - id: autolight
    then:
      on_value: 
        then:
          - stepper.set_target:
              id: stepper_motor
              target: !lambda return id(ambient_sensor).state;

Create a template switch.

switch:
  - platform: template 
    name: enable script
    id: enable_script
    optimistic: true

Then use that as a conditional, put this before an automation. You can use else to add another action or omit for it to do nothing.

on_value
  if:
    condition:
      switch.is_on: enable_script
    then:
      - script.execute: my_script
    else: ##If this is omitted it will behave like an on/off switch for script.
      - stepper.set_target:
          id: stepper_motor
          target: !lambda return id(ambient_sensor).state;
1 Like

Thanks! This did the trick!

1 Like