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.