Increase or decrease servo position with a button on the dashboard

Hi,

I’m quite new to home assistant so bare with me if i get some terminology incorrect.

i have a servo in esphome which i am using to turn the thermostat for my home heating to adjust the temp during the day (a temporary step before i get smart heating next year).

I have this set up with automations to move the thermostat to set values through out the day to set the house temp automatically which works well.

I would like to be able to have a button on my Overview Dashboard for ‘increase temp’ and ‘decrease temp’. however i am unsure of how to get the current servo position value to increase it by a given amount.

the esphome servo yaml is as follows

substitutions:
  name: esphome-web-fad234
  friendly_name: Thermostat_control

esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  min_version: 2024.6.0
  name_add_mac_suffix: false
  project:
    name: esphome.web
    version: dev

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:

# Allow Over-The-Air updates
ota:
- platform: esphome

# Allow provisioning Wi-Fi via serial
improv_serial:

wifi:
  ssid: private
  password: private
  # Set up a wifi access point
  ap: {}

# In combination with the `ap` this allows the user
# to provision wifi credentials to the device via WiFi AP.
captive_portal:

dashboard_import:
  package_import_url: github://esphome/example-configs/esphome-web/esp32.yaml@main
  import_full_config: true

# Sets up Bluetooth LE (Only on ESP32) to allow the user
# to provision wifi credentials to the device.
esp32_improv:
  authorizer: none

# To have a "next url" for improv serial
web_server:
  port: 80

number:
  - platform: template
    name: Servo_Control
    min_value: -100
    initial_value: 0
    max_value: 100
    step: 1
    optimistic: true
    set_action:
      then:
        - servo.write:
            id: Hallway_servo
            level: !lambda 'return x / 100.0;'

# Example configuration entry
servo:
  - id: Hallway_servo
    output: pwm_output

# Example output platform
# On ESP32, use ledc output
output:
  - platform: ledc
    id: pwm_output
    pin: GPIO22
    frequency: 50 Hz

sensor:
  - platform: dht
    pin: GPIO23
    temperature:
      name: "Hallway Temperature"
    humidity:
      name: "Hallway Humidity"
    update_interval: 10s

i have attempted to write a script which i have connected to a button on my dash which has the following

alias: Increase Thermostat Control
sequence:
  - data:
      entity_id: input_number.Servo_Control
      value: "{{ [states('input_number.Servo_Control') | int + 5, 100] | min }}"
    action: input_number.set_value
  - target:
      entity_id: number.Servo_Control
    data:
      value: "{{ states('input_number.Servo_Control') | int }}"
    action: number.set_value
mode: single

unfortunatly i get the following error when i click the button

Failed to perform the action script/increase_thermostat_control. Error rendering data template:
ValueError: Template error: int got invalid input 'unknown' when rendering template '{{ 
states('input_number.Servo_Control') | int + 5, 100] | min }}' but no default was specified

I’m guessing i am not providing the script with the current value for the servo, but i am unsure how do to that.

If anyone can help point out where i have gone wrong that would be much appreciated.

Your number component should give a slider that you can adjust.
Why you need button?

Hi @Karosm Thanks for your response.

Because the slider has 100 steps above and below the start point 0, there is quite a lot of small steps and its difficult to use a touch screen to set it specifically with the slider.

I would like to have the buttons so that i just ‘nudge’ the current set temp by 5 for each button press.

good point.
If you don’t need fine adjustment,

number:
  - platform: template
    name: Servo_Control
    id: servonumber
    min_value: -100
    initial_value: 0
    max_value: 100
    step: 5
    optimistic: true
    set_action:
      then:
        - servo.write:
            id: Hallway_servo
            level: !lambda 'return x / 100.0;'

button:
  - platform: template
    name: Button UP
    on_press:
      then:
        - number.increment:
            id: servonumber
            cycle: false
 - platform: template
    name: Button DOWN
    on_press:
      then:
        - number.decrement:
            id: servonumber
            cycle: false

@Karosm Thanks, this is an interesting work around, but I’m really after a button to push.

I should have probably mentioned this earlier so forgive me. At some point i will be linking this button a a physical button which is located on the thermostat so people can adjust the temp without needing to log into home assistant.

But for now before i get the physical part done i would like to have it possible to increase or decrease with a button in the dashboard UI as a test.

I don’t follow you now…, there are two buttons on the code…
When you need physical ones, you replace them with GPIO binary sensor.

For some reason, when i try to enter this into my code after the number section, it flags as having invalide ssyntax error

INFO ESPHome 2024.11.3
INFO Reading configuration /config/esphome/esphome-web-fad234.yaml...
ERROR Error while reading config: Invalid YAML syntax:

while parsing a block mapping
  in "/config/esphome/esphome-web-fad234.yaml", line 1, column 1
expected <block end>, but found '<block sequence start>'
  in "/config/esphome/esphome-web-fad234.yaml", line 78, column 2

but if i remove that section its fine.

I was actually able to add a button in another way though.

I was able to create the automation as follows

alias: Increase Thermostat Control
sequence:
  - data:
      entity_id: number.esphome_web_fad234_servo_control
      value: >-
        {% set current_value = states('number.esphome_web_fad234_servo_control')
        | float %} {{ [current_value + 5, 100] | min }}
    action: number.set_value
mode: single

apparently this was the figure i should have been using: number.esphome_web_fad234_servo_control. I was able to find this in Developer Tools > States and it had the correct value for the servo. So this will be handy in future when im trying to find the current state value for my stuff.

I then have a helper button called ‘Temp Increase Helper’. Which I then link to a button i created on my Temp & Humidity Dashboard. When I click that button, it runs the automation script and increases the servo position by 5.
I have also done a decrease one as well.

Thanks for your help with this.

Sounds error on your indentation

Ah interesting, yes could be.
Thanks for the help.