How do I expose servo/number to automation?

I have a d1 mini and I’ve got a simple push button that I’m trying to use to operate a servo. There will only be two positions on the servo. I can get the button to show up in automation but not the servo. What am I doing wrong?

esphome:
  name: test-blast-gate
  friendly_name: Test Blast Gate

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: 

ota:
  - platform: esphome
    password:

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Test-Blast-Gate Fallback Hotspot"
    password: 

captive_portal:

binary_sensor:
  - platform: gpio
    pin:
      number: 2
      mode:
        input: true
        pullup: true
    name: "Simple Push Button"

output: 
  - platform: esp8266_pwm 
    id: pwm_output 
    pin: GPIO0
    frequency: 50 Hz

servo: 
  id: blast_gate_servo 
  output: pwm_output 

As far as I know that is not possible.
There is no servo domain in HA.
What you could do is create two buttons or scripts in ESP-Home that you can trigger from HA and have the buttons/scripts set the position of the servo.
Or you need to pass a value from HA to ESP-Home that is then sent to the servo as the position.

I think this could work…

number:
  - platform: template
    name: "Servo position"
    optimistic: true
    min_value: -100
    max_value: 100
    step: 1
    on_value:
      then:
        - servo.write:
          id: blast_gate_servo
          level: !lambda "return x/100;"
1 Like
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO2
      mode: INPUT_PULLUP
      inverted: true
    name: "Simple Push Button"
    filters:
      - delayed_on: 50ms
      
    on_press:
      then:
        - lambda: |-
            static bool state = false;
            state = !state;
            
            if (state) {
              id(blast_gate_servo).write(1.0);  // 180 degrees
            } else {
              id(blast_gate_servo).write(-1.0); // 0 degrees
            }
1 Like

I like this solution. I’ll try it out tonight. Thank you.

Not tested.
Map your servo angles though.
You’re welcome :wink:

This is what I did for continous servo’s. So as mentioned above, you’ll need to alter to fit your need i.e. the angles

time:
  - platform: homeassistant

#-------------------------------------------------------------------------------
# Timed Feeds
#-------------------------------------------------------------------------------

    on_time:
      - seconds: 0
        minutes: 00
        hours: 07
        then :
          - script.execute: trigger_servos

      - seconds: 0
        minutes: 30
        hours: 17
        then :
          - script.execute: trigger_servos

#-------------------------------------------------------------------------------
# Number
#-------------------------------------------------------------------------------

number:
  - platform: template
    name: Feed Amount
    id: servo_control
    min_value: 0
    initial_value: 0.5
    max_value: 2
    step: 0.25
    optimistic: true
    mode: slider
    icon: mdi:rotate-right

#-------------------------------------------------------------------------------
# Servos
#-------------------------------------------------------------------------------

servo:
  - id: servo1
    output: output1
  - id: servo2
    output: output2
  - id: servo3
    output: output3

output:
  - platform: ledc
    id: output1
    pin: 16
    frequency: 50 Hz

  - platform: ledc
    id: output2
    pin: 17
    frequency: 50 Hz

  - platform: ledc
    id: output3
    pin: 18
    frequency: 50 Hz

#-------------------------------------------------------------------------------
# Switch
#-------------------------------------------------------------------------------

switch:
  - platform: template
    name: Disable Feed
    id: disable_feed
    optimistic: True
    icon: mdi:sync-off
    restore_mode: ALWAYS_OFF

#-------------------------------------------------------------------------------
# Buttons
#------------------------------------------------------------------------------- 

binary_sensor:
  - platform: gpio
    id: momentary_button
    pin:
      number: 4
      mode:
        input: true
        pullup: true
    on_press:
      - script.execute: trigger_servos

button:
  - platform: template
    name: Feed Cats
    icon: mdi:cat
    on_press:
      - script.execute: trigger_servos

  - platform: restart
    name: "Reboot"

#-------------------------------------------------------------------------------
# Script
#-------------------------------------------------------------------------------

script:
  id: trigger_servos
  then:
    - if:
        condition:
          switch.is_off: disable_feed
        then:
          - servo.write:
              id: servo1
              level: 100%
          - delay: !lambda 'return id(servo_control).state * 1000;'
          - servo.detach: servo1

          - servo.write:
              id: servo2
              level: 100%
          - delay: !lambda 'return id(servo_control).state * 1000;'
          - servo.detach: servo2

          - servo.write:
              id: servo3
              level: 100%
          - delay: !lambda 'return id(servo_control).state * 1000;'
          - servo.detach: servo3

    - script.stop: trigger_servos

#-------------------------------------------------------------------------------
# Spoilt Rotten Cats Live Here
#-------------------------------------------------------------------------------