Issue Control Servo through dashboard

Following https://siytek.com/esphome-servo-example/#Configure-ESPHome
i’m having issues controlling a servo from an esp8266, D1 Mini. I have used a servo tester (from the same power source) to control the servo position and works, i think my issue is the esp device is not getting the input from the dashboard? i tried to add a log into the yaml to print the value but not sure if i did that correctly?

my aim is to have a few sensors for some garden automations, the other sensors are working fine and see them output on my dashboard. i can see the device in esphome is online and the servo moves on startup.

esphome:
  name: garden
  includes:
    - water_level_sensor.h

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxx"

  services:
    - service: control_servo
      variables:
        level: float
      then:
        - lambda: |-
            ESP_LOGD("custom", "Servo Level: %.2f", level);
        - servo.write:
            id: my_servo
            level: !lambda 'return level / 100.0;'
    - service: control_servo2
      variables:
        level: float
      then:
        - lambda: |-
            ESP_LOGD("custom", "Servo Level2: %.2f", level);
        - servo.write:
            id: my_servo2
            level: !lambda 'return level / 100.0;'

ota:
  password: "xxx"

wifi:
  ssid: "xxx"
  password: "xxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Garden Fallback Hotspot"
    password: "xxx"

captive_portal:

i2c:
  sda: GPIO4
  scl: GPIO5
  scan: true
  id: bus_a

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

  - platform: esp8266_pwm
    id: pwm_output2
    pin: GPIO14
    frequency: 50 Hz

servo:
  - id: my_servo
    output: pwm_output
    #auto_detach_time: 2s

  - id: my_servo2
    output: pwm_output2
    #auto_detach_time: 2s

binary_sensor:
  - platform: gpio
    pin: GPIO13
    name: "Soil Moisture 1"
    device_class: moisture
    filters:
      - delayed_on: 5s
      - delayed_off: 5s

  #- platform: gpio
  #  pin: GPIO15
  #  name: "Soil Moisture 2"
  #  device_class: moisture
  #  filters:
  #    - delayed_on: 5s
  #    - delayed_off: 5s

sensor:
  - platform: wifi_signal
    name: "WiFi Signal"
    update_interval: 60s
  - platform: uptime
    name: "Uptime"
    update_interval: 60s

  - platform: custom
    lambda: |-
     auto my_sensor = new WaterLevelSensor();
     App.register_component(my_sensor);
     return {my_sensor};

    sensors:
      name: "Water Level Sensor"
      unit_of_measurement: "%"
    #update_interval: 10s

  - platform: dht
    pin: GPIO12
    model: DHT11
    temperature:
      name: "Temperature"
      unit_of_measurement: "°C"
      accuracy_decimals: 1
    humidity:
      name: "Humidity"
      unit_of_measurement: "%"
      accuracy_decimals: 1
    update_interval: 10s

  - platform: pulse_counter
    pin: GPIO15
    name: "Water Flow"
    unit_of_measurement: "L/min"
    internal_filter: 10us
    update_interval: 10s
    # Convert pulses to flow rate in liters per minute
    filters:
      - lambda: |-
          return (x / 2.25) * 60.0;

configuration.yaml:

# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

http:
  use_x_forwarded_for: true
  trusted_proxies:
    - xxx

sensor:
  - platform: systemmonitor
    resources:
      - type: disk_use_percent
        arg: /config
      - type: memory_free

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

  servo_control2:
    name: Servo Control2
    initial: 0
    min: -100
    max: 100
    step: 1
    mode: slider

automations.yaml:

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

  - alias: Write Servo2 Value to ESP
    trigger:
      platform: state
      entity_id: input_number.servo_control2
    action:
      - service: esphome.garden_control_servo2
        data_template:
          level: "{{ trigger.to_state.state | int }}"

terminal output:

[20:20:23][C][ota:465]: There have been 0 suspected unsuccessful boot attempts.
[20:20:23][I][app:029]: Running through setup()...
[20:20:23][I][i2c.arduino:161]: Performing I2C bus recovery
[20:20:23][C][esp8266_pwm:017]: Setting up ESP8266 PWM Output...
[20:20:23][C][esp8266_pwm:017]: Setting up ESP8266 PWM Output...
[20:20:23][C][dht:011]: Setting up DHT...
[20:20:23][C][pulse_counter:147]: Setting up pulse counter 'Water Flow'...
[20:20:23][C][wifi:037]: Setting up WiFi...
[20:20:23][D][wifi:384]: Starting scan...
[20:20:23][D][sensor:126]: 'Uptime': Sending state 0.14800 s with 0 decimals of accuracy
[20:20:23][D][sensor:126]: 'Water Level Sensor': Sending state 5.00000 % with 0 decimals of accuracy
[20:20:23][D][dht:048]: Got Temperature=20.0°C Humidity=44.0%
[20:20:23][D][sensor:126]: 'Temperature': Sending state 20.00000 °C with 1 decimals of accuracy
[20:20:23][D][sensor:126]: 'Humidity': Sending state 44.00000 % with 1 decimals of accuracy
[20:20:23][D][servo:050]: Servo reached target
[20:20:23][D][servo:050]: Servo reached target
[20:20:28][D][binary_sensor:034]: 'Soil Moisture 1': Sending initial state OFF
[20:20:29][D][wifi:399]: Found networks:
[20:20:29][I][wifi:442]: - 'xxx' (xxx) ▂▄▆█
[20:20:29][D][wifi:444]:     Channel: 1
[20:20:29][D][wifi:445]:     RSSI: -74 dB
[20:20:29][D][wifi:447]: - 'xxx ▂▄▆█
[20:20:29][D][wifi:447]: - 'xxx ▂▄▆█
[20:20:29][D][wifi:447]: - 'xxx ▂▄▆█
[20:20:29][I][wifi:255]: WiFi Connecting to 'eir33427672'...
[20:20:30][D][pulse_counter:174]: 'Water Flow': Retrieved counter: 0.00 pulses/min
[20:20:30][D][sensor:126]: 'Water Flow': Sending state 0.00000 L/min with 2 decimals of accuracy
[20:20:31][D][dht:048]: Got Temperature=20.0°C Humidity=44.0%
[20:20:31][D][sensor:126]: 'Temperature': Sending state 20.00000 °C with 1 decimals of accuracy
[20:20:31][D][sensor:126]: 'Humidity': Sending state 44.00000 % with 1 decimals of accuracy
[20:20:31][I][wifi:516]: WiFi Connected!
[20:20:31][C][wifi:360]:   Local MAC: xxx
[20:20:31][C][wifi:361]:   SSID: 'xxx'
[20:20:31][C][wifi:362]:   IP Address: xxx
[20:20:31][C][wifi:363]:   BSSID: xxx
[20:20:31][C][wifi:365]:   Hostname: 'garden'
[20:20:31][C][wifi:367]:   Signal strength: -83 dB ▂▄▆█
[20:20:31][C][wifi:371]:   Channel: 1
[20:20:31][C][wifi:372]:   Subnet: 255.255.255.0
[20:20:31][C][wifi:373]:   Gateway: xxx
[20:20:31][C][wifi:374]:   DNS1: xxx
[20:20:31][C][wifi:375]:   DNS2: 0.0.0.0
[20:20:31][D][wifi:525]: Disabling AP...
[20:20:31][C][ota:089]: Over-The-Air Updates:
[20:20:31][C][ota:090]:   Address: garden.local:8266
[20:20:31][C][ota:093]:   Using Password.
[20:20:31][C][api:025]: Setting up Home Assistant API server...
[20:20:31][I][app:062]: setup() finished successfully!
[20:20:31][D][sensor:126]: 'WiFi Signal': Sending state -84.00000 dBm with 0 decimals of accuracy
[20:20:31][I][app:102]: ESPHome version 2022.9.2 compiled on May 28 2023, 20:04:37
[20:20:31][C][wifi:502]: WiFi:
[20:20:31][C][wifi:360]:   Local MAC: xxx
[20:20:31][C][wifi:361]:   SSID: 'xxx'
[20:20:31][C][wifi:362]:   IP Address: xxx
[20:20:31][C][wifi:363]:   BSSID: xxx
[20:20:31][C][wifi:365]:   Hostname: 'garden'
[20:20:31][C][wifi:367]:   Signal strength: -86 dB ▂▄▆█
[20:20:31][C][wifi:371]:   Channel: 1
[20:20:31][C][wifi:372]:   Subnet: 255.255.255.0
[20:20:31][C][wifi:373]:   Gateway: xxx
[20:20:31][C][wifi:374]:   DNS1: xxx
[20:20:31][C][wifi:375]:   DNS2: xxx
[20:20:31][C][logger:275]: Logger:
[20:20:31][C][logger:276]:   Level: DEBUG
[20:20:31][C][logger:277]:   Log Baud Rate: 115200
[20:20:31][C][logger:278]:   Hardware UART: UART0
[20:20:31][C][i2c.arduino:038]: I2C Bus:
[20:20:31][C][i2c.arduino:039]:   SDA Pin: GPIO4
[20:20:31][C][i2c.arduino:040]:   SCL Pin: GPIO5
[20:20:31][C][i2c.arduino:041]:   Frequency: 50000 Hz
[20:20:31][C][i2c.arduino:044]:   Recovery: bus successfully recovered
[20:20:31][I][i2c.arduino:054]: Results from i2c bus scan:
[20:20:31][I][i2c.arduino:060]: Found i2c device at address 0x77
[20:20:31][C][esp8266_pwm:022]: ESP8266 PWM:
[20:20:31][C][esp8266_pwm:023]:   Pin: GPIO2
[20:20:31][C][esp8266_pwm:024]:   Frequency: 50.0 Hz
[20:20:31][C][esp8266_pwm:022]: ESP8266 PWM:
[20:20:31][C][esp8266_pwm:023]:   Pin: GPIO14
[20:20:31][C][esp8266_pwm:024]:   Frequency: 50.0 Hz
[20:20:31][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Soil Moisture 1'
[20:20:31][C][gpio.binary_sensor:015]:   Device Class: 'moisture'
[20:20:31][C][gpio.binary_sensor:016]:   Pin: GPIO13
[20:20:31][C][uptime.sensor:031]: Uptime Sensor 'Uptime'
[20:20:31][C][uptime.sensor:031]:   State Class: 'total_increasing'
[20:20:31][C][uptime.sensor:031]:   Unit of Measurement: 's'
[20:20:31][C][uptime.sensor:031]:   Accuracy Decimals: 0
[20:20:31][C][uptime.sensor:031]:   Icon: 'mdi:timer-outline'
[20:20:31][C][servo:013]: Servo:
[20:20:31][C][servo:014]:   Idle Level: 7.5%
[20:20:31][C][servo:015]:   Min Level: 3.0%
[20:20:31][C][servo:016]:   Max Level: 12.0%
[20:20:31][C][servo:017]:   auto detach time: 0 ms
[20:20:31][C][servo:018]:   run duration: 0 ms
[20:20:31][C][servo:013]: Servo:
[20:20:31][C][servo:014]:   Idle Level: 7.5%
[20:20:31][C][servo:015]:   Min Level: 3.0%
[20:20:31][C][servo:016]:   Max Level: 12.0%
[20:20:31][C][servo:017]:   auto detach time: 0 ms
[20:20:31][C][servo:018]:   run duration: 0 ms
[20:20:31][C][dht:017]: DHT:
[20:20:31][C][dht:018]:   Pin: GPIO12
[20:20:31][C][dht:022]:   Model: DHT11
[20:20:31][C][dht:027]:   Update Interval: 10.0s
[20:20:31][C][dht:029]:   Temperature 'Temperature'
[20:20:31][C][dht:029]:     Device Class: 'temperature'
[20:20:31][C][dht:029]:     State Class: 'measurement'
[20:20:31][C][dht:029]:     Unit of Measurement: '°C'
[20:20:31][C][dht:029]:     Accuracy Decimals: 1
[20:20:31][C][dht:030]:   Humidity 'Humidity'
[20:20:31][C][dht:030]:     Device Class: 'humidity'
[20:20:31][C][dht:030]:     State Class: 'measurement'
[20:20:31][C][dht:030]:     Unit of Measurement: '%'
[20:20:31][C][dht:030]:     Accuracy Decimals: 1
[20:20:31][C][pulse_counter:160]: Pulse Counter 'Water Flow'
[20:20:31][C][pulse_counter:160]:   State Class: 'measurement'
[20:20:31][C][pulse_counter:160]:   Unit of Measurement: 'L/min'
[20:20:31][C][pulse_counter:160]:   Accuracy Decimals: 2
[20:20:31][C][pulse_counter:160]:   Icon: 'mdi:pulse'
[20:20:31][C][pulse_counter:161]:   Pin: GPIO15
[20:20:31][C][pulse_counter:162]:   Rising Edge: INCREMENT
[20:20:31][C][pulse_counter:163]:   Falling Edge: DISABLE
[20:20:31][C][pulse_counter:164]:   Filtering pulses shorter than 10 µs
[20:20:31][C][pulse_counter:165]:   Update Interval: 10.0s
[20:20:31][C][captive_portal:088]: Captive Portal:
[20:20:31][C][mdns:094]: mDNS:
[20:20:31][C][mdns:095]:   Hostname: garden
[20:20:31][C][ota:089]: Over-The-Air Updates:
[20:20:31][C][ota:090]:   Address: garden.local:8266
[20:20:31][C][ota:093]:   Using Password.
[20:20:31][C][api:138]: API Server:
[20:20:31][C][api:139]:   Address: garden.local:6053
[20:20:31][C][api:141]:   Using noise encryption: YES
[20:20:31][C][wifi_signal.sensor:009]: WiFi Signal 'WiFi Signal'
[20:20:31][C][wifi_signal.sensor:009]:   Device Class: 'signal_strength'
[20:20:31][C][wifi_signal.sensor:009]:   State Class: 'measurement'
[20:20:32][C][wifi_signal.sensor:009]:   Unit of Measurement: 'dBm'
[20:20:32][C][wifi_signal.sensor:009]:   Accuracy Decimals: 0
[20:20:34][D][sensor:126]: 'Water Level Sensor': Sending state 5.00000 % with 0 decimals of accuracy
[20:20:37][D][api:102]: Accepted xxx
[20:20:38][D][api.connection:861]: Home Assistant 2023.5.4 (192.168.1.10): Connected successfully
[20:20:40][D][pulse_counter:174]: 'Water Flow': Retrieved counter: 0.00 pulses/min
[20:20:40][D][sensor:126]: 'Water Flow': Sending state 0.00000 L/min with 2 decimals of accuracy
[20:20:41][D][dht:048]: Got Temperature=20.0°C Humidity=44.0%
[20:20:41][D][sensor:126]: 'Temperature': Sending state 20.00000 °C with 1 decimals of accuracy
[20:20:41][D][sensor:126]: 'Humidity': Sending state 44.00000 % with 1 decimals of accuracy
[20:20:49][D][sensor:126]: 'Water Level Sensor': Sending state 5.00000 % with 0 decimals of accuracy
[20:20:50][D][pulse_counter:174]: 'Water Flow': Retrieved counter: 0.00 pulses/min
[20:20:50][D][sensor:126]: 'Water Flow': Sending state 0.00000 L/min with 2 decimals of accuracy
[20:20:51][D][dht:048]: Got Temperature=20.0°C Humidity=44.0%
[20:20:51][D][sensor:126]: 'Temperature': Sending state 20.00000 °C with 1 decimals of accuracy
[20:20:51][D][sensor:126]: 'Humidity': Sending state 44.00000 % with 1 decimals of accuracy

if anyone has any suggestions on what could help me that would be great!

What do the automation traces show when you adjust the input numbers?

Are there any errors in Settings → System → Logs related to these automations?

the only error in logs is when i reboot the device:

Logger: aioesphomeapi.connection
Source: runner.py:179
First occurred: May 28, 2023 at 20:05:23 (7 occurrences)
Last logged: 21:15:59

garden @ xxx: Connection error occurred: [Errno 104] Connection reset by peer
garden @ xxx: Connection error occurred: Ping response not received after 90.0 seconds

i guess the issue could be that the automation isnt loaded if it isnt in this screen?

but i added it via the file editor?


and have rebooted since setting, and have no issues when in developer tools and check configurations

i tried creating it though the ui rather than pasting into the yaml but getting an error 500 with:

have to use the yaml editor otherwise the data is only allowing a float

if i change this (level) value then the servo moves! when the automation is run

but unsure what is wrong to link it to the slider on my homepage

using

i get ‘Error rendering data template: UndefinedError: ‘trigger’ is undefined’

super weird that it works now!
i edited the yaml back to how the format was origionally, did a soft reload, and now it magically works.

- id: '1685395498019'
  alias: New Automation
  description: ''
  trigger:
  - platform: state
    entity_id:
    - input_number.servo_control
  condition: []
  action:
  - service: esphome.garden_control_servo
    data_template:
      level: '{{ trigger.to_state.state | int }}'
  mode: single

is the code in the automations yaml file

The problem was the first line of your automations.yaml file. The include in your configuration.yaml file would resolve to:

automation:
  automation:
    - alias: Write Servo Value to ESP
      trigger:
        platform: etc...

When it should be:

automation:
  - alias: Write Servo Value to ESP
    trigger:
      platform: etc...
1 Like