Stepper Motor new position calculate

This is my Automation

alias: Neue Automatisierung
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.test_living_room_window
    to: "on"
condition: []
action:
  - service: esphome.test_control_stepper
    data:
      target: 1000
mode: single

Can anyone say me how i can the Position of a Stepper new calculate if a Sensor triggered.

Or how i can add for example 1000 steps to the target.

Now is my problem with this Automation that it drive just of the Position 1000, but my thought is if the sensor is triggered drive 1000 steps again and again if the sensor is new triggered

Can you read data from the “esphome.test_control_stepper” as current step value? If so, you can add that state back like

action:
  - service: esphome.test_control_stepper
    data:
      target:  {{states('esphome.test_control_stepper') | int(0) +1000}}

Just to remind that if you’re using that binary_sensor as a trigger, it needs to fire “off->on” that automation triggers. Maybe use launch every minute and check in condition if binary_sensor is “on”?

1 Like

Hi,

thanks for the Example.

I tried the code, but unfortunately it didn’t work.
Is the syntax incorrect?

alias: Neue Automatisierung
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.test_living_room_window
    to: "on"
condition: []
action:
  - service: esphome.test_control_stepper
    data:
      target:  {{states('esphome.test_control_stepper') | int(0) +1000}}
mode: single

BTW

Thats the yaml example from my esphome. Maybe you can help me to realize my plan.
And there you can see what for sensors i have.

# Enable Home Assistant API
api:
  services:
    - service: control_stepper
      variables:
        target: int
      then:
        - stepper.set_target:
            id: my_stepper
            target: !lambda 'return target;'
        - sensor.template.publish:
            id: position
            state: !lambda 'return target;'

cover:
  - platform: template
    name: "Motor"
    id: my_blind
    device_class: blind
    open_action:
      - stepper.set_target:
          id: my_stepper
          target: 10000
      - sensor.template.publish:
          id: position
          state: !lambda return id(my_stepper).target_position;
    close_action:
      - stepper.set_target:
          id: my_stepper
          target: 0
      - sensor.template.publish:
            id: position
            state: !lambda return id(my_stepper).target_position;
    stop_action:
      - stepper.set_target:
          id: my_stepper
          target: !lambda return id(my_stepper).current_position;
      - sensor.template.publish:
            id: position
            state: !lambda return id(my_stepper).current_position;
    optimistic: True
    assumed_state: True

sensor:
  - platform: template
    name: "stepper Position"
    id: position
            
stepper:
  - platform: a4988
    id: my_stepper
    step_pin: GPIO18
    dir_pin: GPIO19
    max_speed: 500 steps/s

    # Optional:
    sleep_pin: GPIO2
    acceleration: inf
    deceleration: inf

The code was only an example to you if you know the real sensor you want to use in your automation.

Could you have a sensor in your system called something like “test_control_stepper”/“stepper Position”? You can find all you Home Assistant entities from the developer tools->states. Do you see it there?

1 Like

Yes i have it

But its not working

And the value +1000 is not blue like yours in your example i dont know if this is a syntax problem ?!

OK, so you have a “sensor.test_stepper_position” and I assume it shows current set position like “1000”. Now you add to your automation sensor value and add to it 1000. Everytime your binary_sensor goes something → “on”, it will keep adding 1000 value to the stepper motor position current position value. Just to give a hint, device need to change to the other value than “on” mean while if you need to continue to add 1000 to it, like on->off->on->off-on (3x1000 added to position).

action:
  - service: esphome.test_control_stepper
    data:
      target:  {{states('sensor.test_stepper_position') | int(0) +1000}}
1 Like

Perfekt thank you verry much it was a syntax mistakeđź«Ł just a small | was missing.

action:
  - service: esphome.test_control_stepper
    data:
      target: |
        {{states('sensor.test_stepper_position') | int(0) +1000}}
mode: single
1 Like