Servo motor button/switch/activation

Hello, I was wondering if you could help me create a button or switch for my servo. I have been looking through diffrent forums and documentation and I can not figure it out. I would like to use my servo to push the button, so it needs to go so far as to press it and come back to default state. I will attach my code for servo, while I know that i would need to follow this:


Configuring HA would be so much easier if those guides had a possible use not just a snipet of code that could be placed anywhere between config files.
If anyone had maybe similar buttons/switches programed I would be greatful for sharing the code and placment of it.

tl dr:
button/switch/activator to make servo preset movment.

esphome:

api:
    services:
    - service: control_servo
      variables:
        level: float
      then:
        - servo.write:
            id: my_servo
            level: !lambda 'return level / 100.0;'

output:
  - platform: esp8266_pwm
    id: pwm_output
    pin: D4
    frequency: 50 Hz
 
servo:
  - id: my_servo
    output: pwm_output
   
configuration.yaml file:

input_number:
  servo_control:
    name: Servo Control
    initial: 0
    min: -10
    max: 100
    step: 1
    mode: box


automation:
  - alias: Write Servo Value to ESP
    trigger:
      platform: state
      entity_id: input_number.servo_control
    action:
      # Replace livingroom with the name you gave the ESP
      - service: esphome.temp_off_control_servo
        data_template:
          level: '{{ trigger.to_state.state | int }}'
    

Thank you in advance.
Jarek

There are complete examples in the cookbook section and the DIY examples section. https://esphome.io/#cookbook and https://esphome.io/guides/diy.html

I solved it with this code:

api:

  services:
    - service: control_servo
      variables:
        level: float
      then:
        - servo.write:
            id: open_close_button
            level: !lambda 'return level / 100.0;'


output:
  - platform: esp8266_pwm
    id: 'gate_t'
    pin: D7
    frequency: 50 Hz


binary_sensor:
  - platform: gpio
    pin: D3
    name: "Gate"
    id: top_end_stop
servo:
  - output: 'gate_t'
    id: open_close_button
 
cover:
  - platform: template
    name: "Open Close Gate"
    id: open_close_servo_control
    optimistic: true
    lambda: !lambda |-
      if (id(top_end_stop).state) {
          return COVER_CLOSED;
      } else {
          return COVER_OPEN;
        }

    open_action:
      - servo.write:
          id: open_close_button
          level: -30.0%
      - delay: 500ms
      - servo.write:
          id: open_close_button
          level: 100.0%

    close_action:
      - servo.write:
          id: open_close_button
          level: -50.0%
      - delay: 500ms
      - servo.write:
          id: open_close_button
          level: 100.0%

         

this code updates the status of microswitch that is open/closed pressed /unppressed by updating the little picture and make servo go and come back.

For anyone that might need it.

via Dr Zzs video on blinds and his config.

6 Likes