Running a Servo on ESPhome

I am trying to run a servo with a D1 mini using ESPhome. I followed the example here but it isn’t working. It compiles and uploads, but no entities appear in Home Assistant. I also have a DHT sensor connected and that works fine, but no joy on the servo. Any ideas? My ESPhome yaml is here:

esphome:
  name: crawl_space
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "redacted"
  password: "redacted"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "redacted"
    password: "redacted"

captive_portal:

# Enable logging
logger:

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

output:
  - platform: esp8266_pwm
    id: pwm_output
    pin: D3
    frequency: 50 Hz
    
servo:
  - id: my_servo
    output: pwm_output
    
sensor:
  - platform: dht
    pin: D2
    temperature:
      filters:
      - lambda: return x * (9.0/5.0) + 32.0;
      unit_of_measurement: "°F"
      name: "Crawl Space Temperature"
    humidity:
      name: "Crawl Space Humidity"
    update_interval: 60s
    model: DHT22

and I have this in my config.yaml

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

and this in my automations.yaml

- id: servo
  alias: Write Servo Value to ESP
  trigger:
    platform: state
    entity_id: input_number.servo_control
  action:
    - service: esphome.crawl_space_servo_control
      data_template:
        level: "{{ trigger.to_state.state | int }}"

Thanks in advance.

I didn’t get it to work either, any solutions yet?

Youve set your API service as control_servo and in the automation you use esphome.crawl_space_servo_control, shouldn’t that be esphome.crawl_space_control_servo ?

Also, no entities should show up in HA, see the Note on this page:

And remember to power the servo from the power supply directly, there is not enough current in the VCC pin if you use USB.

I forgot that I had posted this. I did get it to work, and it’s been working for months now.

There are actually 2 servos and a sensor. Here is what I ended up with:

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

  
ota:

output:
  - platform: esp8266_pwm
    id: pwm_output
    pin: D3
    frequency: 50 Hz
  - platform: esp8266_pwm
    id: pwm_output_2
    pin: D4
    frequency: 50 Hz
    
servo:
  - id: my_servo
    output: pwm_output
  - id: my_servo2
    output: pwm_output_2 
    
sensor:
  - platform: dht
    pin: D2
    temperature:
      filters:
      - lambda: return x * (9.0/5.0) + 32.0;
      unit_of_measurement: "°F"
      name: "Crawl Space Temperature"
    humidity:
      name: "Crawl Space Humidity"
    update_interval: 60s
    model: DHT22

1 Like

This is how I was able to use it.
api:
services:
- service: control_servo
variables:
level: float
then:
- servo.write:
id: my_spray_servo
level: !lambda ‘return level / 100.0;’

output:

  • platform: esp8266_pwm
    id: pwm_output
    pin: GPIO14
    frequency: 50 Hz

servo:

  • id: my_spray_servo
    output: pwm_output

auto_detach_time: 1s

Configuration.yaml

input_number:
servo_control:
initial: 0
min: -100
max: 100
step: 1
mode: slider

Automation

alias: Spray Servo
description: Spray Servo
trigger:

  • platform: state
    entity_id:
    • input_number.servo_control
      condition:
      action:
  • service: esphome.esphome_web_33b765_control_servo
    data:
    level: “{{ trigger.to_state.state | int }}”
    mode: single

Finally add card on Dashboard

type: vertical-stack
cards:

  • type: tile
    entity: automation.spray_servo
  • type: custom:mushroom-number-card
    entity: input_number.servo_control
    icon_color: teal
    fill_container: false

Hello. Maybe try to use frendly name (that does not contain spaces). For example in your ESP YAML file:
" esphome:
name: “ESP1”
friendly_name: ESP1 " ← here
This is a name that Home Assistant use and so the name that should be in automations.yaml:
" action:

  • service: esphome.ESP1_control_servo " ← change the ESP1 if you have set diffrent friendly name earlier.