Totally Lost, trying to put physical button to control servo

HI All

I’m looking for some help. I have a window blind that I’ve automated using a servo and automation within Home assistant but I’ll looking to upgrade to include a physical button and my mind is drawing a blank.

basically I have a rocker switch, if I press “up” I’d like the blind to open to a set location, if I long press “up” I’d like it to open until I release the button.
If I press “down” I’d like it to close the blind fully and if I long press “down” I’d like it to close until I release the button.

I can’t work out how to have the button control the servo. .

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

servo:
  - id: stairs_tilt
    output: 'pwm_output'

output:
  - platform: esp8266_pwm #this is for the servo
    id: 'pwm_output'
    pin: GPIO2 #D4
    frequency: 50 Hz
  - platform: gpio
    id: 'hb_open'
    pin: D2
  - platform: gpio
    id: 'hb_close'
    pin: D3

switch:
  - platform: output
    name: "HBOpen"
    output: 'hb_open'
    id: HBOpen
  - platform: output
    name: "HBClose"
    output: 'hb_close'
    id: HBClose 


this is what I have so far but I’d drawing a total blank for working out the rest.

any help is greatly appreciated.

thank you

Robin

I’m no expert, but have a look at the binary sensor docs in ESPHome.

on_press and on_release should be able to handle the set location, while on_click , on_multi_click or on_double_click could be used for fully open or fully closed.

I would seriously aboid using a rocker switch for this use. You could do it, i just dont think its a very user-friendly method for other people. I guess it depends on whether or not its a "“locking” switch or a “momentary” switch. If you push it one direction, does it lock in that position? If it does, i highly recommend you dont utry using that as a “push and hold”. You need a momentary switch for this, 100%

After that its like the other person said, binary_sensor.

This isn’t exactly what you need but, this should give you a good idea with yours.

I use a servo for moving a camera Left/Right.

number:
  - platform: template
    name: "Barn Servo"
    id: barn_servo_number
    min_value: -100
    initial_value: 0
    max_value: 100
    step: 5
    optimistic: true
    #internal: true
    set_action:
      then:
        - servo.write:
            id: barn_cam_servo
            level: !lambda 'return x / 100.0;'    

button:
  - platform: template
    name: Servo Left
    id: camera_left
    on_press:
      - number.operation:
         id: barn_servo_number
         operation: Decrement         
         cycle: false
      - servo.detach: barn_cam_servo


  - platform: template
    name: Servo Right
    id: camera_right
    on_press:
      - number.operation:
         id: barn_servo_number
         operation: Increment
         cycle: false
      - servo.detach: barn_cam_servo   

I used buttons which you could use as well for a HA front-end control Up/Down. Just a thought…

For binary sensor you’d just use the button automation the same way. You’d also have to use automatons like this. This way your not locked into a min/max time limit. Push and hold as much or little as you want.

 on_press:
   while:
     condition:
       - binary_sensor.is_on:
     then:
        - number.operation:
           id: barn_servo_number
           operation: Increment
           cycle: false

Thank you Falling Away and Shadow Fist. Sorry for the Delay in replying I’ve been away from home due to work so projects like this can take me months.

I’m still struggling though… perhaps I’m not understanding what you have kindly shared or perhaps I am not explaining what I am looking for properly. I’m going to assume it’s me…

What I am looking to achieve is to have a physical, momentary rocker switch attached to the GPIO pins of a D1 Mini, to control a servo, Idealy, I’m looking to limit the opening to -12 (total movement range is -100 to 100) but close to the max 100.

I’ve tried and looked into the example you have shared but I see no way to link that to a GPIO lin location for the trigger, I already have buttons in HA that I can use to control the servo from there and via automation but I’m trying to upgrade it to include a physical button (i.e. make it wife friendly).

thank you for your help and time.

Robin

Then have a look at Gpio binary sensor:

Hi Karosm, Thank you for your reply. I’ve been having a look at that and though it had got it but still nothing is working.

my complete Yaml is

esphome:
  name: stair-blind
  friendly_name: Stair Blind
  platform: esp8266
  board: d1_mini


# Enable logging
logger:

# Enable Home Assistant API
api:
  services: 
    - service: control_servo
      variables:
        level: float
      then:
        - servo.write:
            id: stairs_tilt
            level: !lambda 'return level /100.0;'

ota:
  platform: esphome
  password: "************"

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


captive_portal:

servo:
  - id: stairs_tilt
    output: 'pwm_output'


output:
  - platform: esp8266_pwm #this is for the servo
    id: 'pwm_output'
    pin: D4
    frequency: 50 Hz
  - platform: gpio
    id: 'hb_open'
    pin: D2
  - platform: gpio
    id: 'hb_close'
    pin: D3

switch:
  - platform: output
    name: "HBOpen"
    output: 'hb_open'
    id: HBOpen
  - platform: output
    name: "HBClose"
    output: 'hb_close'
    id: HBClose   

binary_sensor:
  - platform: gpio
    id: hb_bopen
    name: "Hall Blind Button Open"
    pin: 
      number: D5
    on_press:
      then:
        - switch.turn_off: HBClose
        - switch.turn_on: HBOpen
    on_release:
      then:
        - switch.turn_off: HBClose
        - switch.turn_off: HBOpen
    on_click:
      then:
        - switch.turn_off: HBClose
        - switch.turn_on: HBOpen
  - platform: gpio
    id: hb_bclose
    name: "Hall Blind Button Close"
    pin: 
      number: D6
    on_press:
      then:
        - switch.turn_off: HBOpen
        - switch.turn_on: HBClose
    on_release:
      then:
        - switch.turn_off: HBOpen
        - switch.turn_off: HBClose
    on_click:
      then:
        - switch.turn_off: HBOpen
        - switch.turn_on: HBClose     
        


I have the switch wired to Ground, D5 and D6 on the D1 mini.

D3 and D4 are strapping pins and they have pullups. So use D1 or D7 for servo.
What you have on D2 and D3?

This is my D1mini with all connected wires, for some reason i dont have anything connected to D2 or D3 and now im not sure why.

I followed a tutorial on youtube to get the servo (attached to a blind) to work. Everything works through HA and my automations work, i just cant get a physical button added for some reason.

I’m trying to find the tutorial I followed but it was a while ago…

Not important.
So what you really have connected? Only servo on D4?

Servo on D4, momentary rocker switch on D5, D6 & Ground

One leg of the switch should go to GND.
And I would also move servo to another pin. D1/2/5/6/7

Sorry the Central pin of the Roccker is on Ground.

I’ll move the servo to pin D1 just now. (Servo is now on Pin D1)

thank you for your help

Ok, let me understand switch you use is rocker switch with 3 positions, but not momentary? But you use it like it was momentary?

no the rocker switch is momentary. if I press “up” and let go it will automatically rest at “neutral”, same goes for if I press “down”

Ok. It’s still not clear to me what the servo should do when you push your momentary switch. Move to defined position, or move until you release the button?

If possible, what im looking to do is.

Short press up, the blind will open to a fixed position (say -12).
Long press up, the blind will keep opening untill either it hits -100 or i release the switch, whichever happens first.

Short press down, the blind will close all the way.
Long press down, the blind will close all the way or untill i release the switch, whichever happens first.

If that is not possible, then im looking to at a minimum achieve the short press functions if possible.

Ok, makes sense with regular gear motor. But servo is fast as it’s nature. What servo you have?

I bought this Servo
https://www.aliexpress.com/item/1005002990973782.html?spm=a2g0o.order_list.order_list_main.66.79e8180267K2nu its model MG996 180

It’s beefy servo, how is your power supply for it?

Try this code, at the moment there is no “manual adjustment” just open and close.
But we can easily add it in multiple ways.
I have no idea what your home assistant service code does.

esphome:
  name: stair-blind
  friendly_name: Stair Blind
  platform: esp8266
  board: d1_mini


# Enable logging
logger:

# Enable Home Assistant API
api:
  services: 
    - service: control_servo
      variables:
        level: float
      then:
        - servo.write:
            id: stairs_tilt
            level: !lambda 'return level /100.0;'

ota:
  platform: esphome
  password: "************"

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


captive_portal:

servo:
  - id: stairs_tilt
    output: 'pwm_output'
    transition_length: 2s #to limit servo speed


output:
  - platform: esp8266_pwm #this is for the servo
    id: 'pwm_output'
    pin: D1
    frequency: 50 Hz
 # - platform: gpio
 #   id: 'hb_open'
 #   pin: D2
 # - platform: gpio
 #   id: 'hb_close'
 #   pin: D3



binary_sensor:
  - platform: gpio
    pin:
      number: D5
      mode:
        input: true
        pullup: true
    id: hb_bopen
    name: "Hall Blind Button Open"
    on_press:
      then:
      - servo.write:
      id: stairs_tilt
      level: -12.0%

  - platform: gpio
    pin:
      number: D6
      mode:
        input: true
        pullup: true
    id: hb_bclose
    name: "Hall Blind Button Close"
    on_press:
      then:
      - servo.write:
      id: stairs_tilt
      level: 100.0%
1 Like

Thank you that works!! one small bug with it (but I can easily live with it) when it’s open, if I press “open” again, it closes and on occasion when I press “open” after about 30 seconds or so it closes. Like I said though I can easily live with this my guess is that I need to check the wiring.

if I remember correctly the service code is to allow home assistant to see it and let me create automation.

but thank you! it works !!