Is it possible to control a Stepper through a box input number?

Got my ESPHome Stepper working, but since I want to use it for fertigation (and not for blinds as apparently most of us do), I need to extend my control beyond the default slider. It would be great if I could manage:

  • a box where I can input the precise number of steps I need the pump to make (instead of the slider);
  • a button that can reset the position of the motor, to zero, for example.

Any thoughts? Thank you in advance for giving an hand to someone without programming skills.

My ESPHome device config is like so:

esphome:
  name: stepper1

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  services:
    - service: control_stepper
      variables:
        target: int
      then:
        - logger.log: "Speed updated"
        - stepper.set_target:
            id: stepper1
            target: !lambda 'return target;'

stepper:
  - platform: uln2003
    id: stepper1
    sleep_when_done: true
    pin_a: D1
    pin_b: D2
    pin_c: D3
    pin_d: D4
    max_speed: 250 steps/s 
    
ota:
  password: "xxxxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Stepper1 Fallback Hotspot"
    password: "xxxxxxxx"

captive_portal:
    

My HA config file earned this bunch:

# STEPPERS
input_number: 
  stepper_control:
    name: Stepper Control
    initial: 0
    min: -1000
    max: 1000
    step: 1
    mode: slider

And my automations.yaml file got this:

- id: '1661897634736'
  alias: Write Stepper Value to ESP
  trigger:
  - platform: state
    entity_id: input_number.stepper_control
  action:
  - service: esphome.stepper1_control_stepper
    data_template:
      target: '{{ trigger.to_state.state | int }}'

All I can get is this in my dashboard:
Untitled

I can help with the first.

change “mode: slider” to “mode: box”

1 Like

I tried that before, but it didn’t worked. At least this plain change does not work.
EDIT: yes this works! Little issue in config code. Now it is okay.
Thank you very much for giving me an hand :slight_smile:
Now I just need a reset button…

For the second, part you could just make an automation that resets the steps back to 0 and then use a the tap method in a card to reset it:

tap_action:
      action: call-service
      service: automation.trigger
      service_data:
        entity_id: automation.step_reset

I think that would work for you.

Many thanks for the suggestion. I will try this now and will come back with an update.

So I created this button in my dashboard, as you suggested, and I’m currently playing around the automations and can’t figure how to create one that resets the value of my stepper. Probably very simple but…

Can you please tell me a bit more how to do this?
Once again thank you very much.

I think something like this would work, I create a trigger that would never happen, cause I’m not sure if you can create an automation without a trigger:

id: Stepper Reset to Zero
alias: Stepper Reset to Zero
trigger:
  platform: numeric_state
  entity_id: sensor.uptime_sensor_stepper1
  below: -120
action:
  - service: esphome.stepper1_position
    data:
      position: '0'

Tried this, but when I try to execute the automation, it does not work. My stepper value stills the same, does not resets.

Try this:

id: Stepper Reset to Zero
alias: Stepper Reset to Zero
trigger:
  platform: numeric_state
  entity_id: sensor.uptime_sensor_stepper1
  below: -120
action:
  - service: esphome.stepper1_position
    data:
      position: '0'
  - service: esphome.stepper1_control_stepper
    data:
      target: '0'

Still does not resets. I’m getting this error " Unable to find service esphome.stepper1_position"

You need to add this to your esphome yaml and upload it:

api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  services:
    - service: control_stepper
      variables:
        target: int
      then:
        - logger.log: "Speed updated"
        - stepper.set_target:
            id: stepper1
            target: !lambda 'return target;'
    - service: stepper_position
      variables:
        position: int
      then:
       - stepper.report_position:
            id: stepper1
            position: !lambda 'return position;'
            
sensor:
  - platform: template
    name: "Stepper Position"
    unit_of_measurement: steps
    lambda: return id(stepper1).current_position;
    update_interval: 1s

Updated and uploaded the ESP device. Still does not resets. Still the same error “Unable to find service esphome.stepper1_position”.

After that you may need to restart Homeassistant, to pickup the new entities.

yep, I did restarted before trying again. Still no reset.

I think I see the issue, we need to change:

    - service: stepper_position
      variables:
        position: int

to

    - service: stepper1_position
      variables:
        position: int

Now, neither the stepper nor the reset automation work.

Make sure you valid the esphome config, to check for errors.

Ok, I will check this tomorrow and will let you know. Thank you again for so much help! :slight_smile:

Hi again, sorry for only being able to check this atm. I double checked the ESPHome config, it validates ok. The stepper motor is working again (it was a misplaced cable).
But Im still unable to reset the position.

For convenience, here is the code I currently have:
ESPHome device:

esphome:
  name: stepper1

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxx"
  services:
    - service: control_stepper
      variables:
        target: int
      then:
        - logger.log: "Speed updated"
        - stepper.set_target:
            id: stepper1
            target: !lambda 'return target;'
    - service: stepper1_position
      variables:
        position: int
      then:
       - stepper.report_position:
            id: stepper1
            position: !lambda 'return position;'

stepper:
  - platform: uln2003
    id: stepper1
    sleep_when_done: true
    pin_a: D1
    pin_b: D2
    pin_c: D3
    pin_d: D4
    max_speed: 250 steps/s 

sensor:
  - platform: template
    name: "Stepper Position"
    unit_of_measurement: steps
    lambda: return id(stepper1).current_position;
    update_interval: 1s
    
ota:
  password: "xxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Stepper1 Fallback Hotspot"
    password: "xxxxxx"

captive_portal:
    

My automation for reset:

alias: Stepper Reset to Zero
trigger:
  - platform: numeric_state
    entity_id: sensor.uptime_sensor_stepper1
    below: -120
action:
  - service: esphome.stepper1_position
    data:
      position: "0"
  - service: esphome.stepper1_control_stepper
    data:
      target: "0"

My automation for writing value:

- id: '1661897634736'
  alias: Write Stepper Value to ESP
  trigger:
  - platform: state
    entity_id: input_number.stepper_control
  action:
  - service: esphome.stepper1_control_stepper
    data_template:
      target: '{{ trigger.to_state.state | int }}'

My HA config:

  stepper_control:
    name: Stepper Control
    initial: 0
    min: -50000
    max: 50000
    step: 100
    mode: box

Once again, tank you for all the help!

Ok first thing I found, that I missed we need to change to this:

alias: Stepper Reset to Zero
trigger:
  - platform: numeric_state
    entity_id: sensor.uptime_sensor_stepper1
    below: -120
action:
  - service: esphome.stepper1_stepper1_position
    data:
      position: "0"
  - service: esphome.stepper1_control_stepper
    data:
      target: "0"
1 Like