Tare button for hx711

Hello, I have an hx711 sensor running in ESPHome, and it works quite well so far. but I definitely need a tare function with a button. I just can not manage it…

I need a button like the one in the picture, it should set the value below to zero.
Screenshot 2024-05-05 155742

This is the code:

esphome:
  name: waage-1-hx711
  friendly_name: Waage 1 hx711

esp8266:
  board: esp01_1m

# Enable logging
logger:

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

ota:
  password: "XXXXXXX"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Waage-1-Hx711 Fallback Hotspot"
    password: "XXXXXXXX"

# Example configuration entry
sensor:
  - platform: wifi_signal
    name: "Pot Waage WiFi Signal"
    update_interval: 60s

  - platform: hx711
    name: "HX711 Value"
    dout_pin: 12
    clk_pin: 13
    gain: 128
    update_interval: 2s
    unit_of_measurement: kg
    accuracy_decimals: 1


    filters:
      - calibrate_linear:
          - 144614 -> 0
          - 1004000 -> 1

Try this:

sensor:
  - platform: hx711
    id: weigth 
    name: "HX711 Value"
    dout_pin: 12
    clk_pin: 13
    gain: 128
    update_interval: 2s
    unit_of_measurement: kg
    accuracy_decimals: 1
    filters:
      - calibrate_linear:
          - 144614 -> 0
          - 1004000 -> 1
      - lambda: |-
          id(weigth_no_tare).publish_state(x);
          return (x - id(weigth_tare));

  - platform: template
    id: weigth_no_tare
    internal: True

globals:
  - id: weigth_tare
    type: float
    restore_value: False
    initial_value: '0.0'

button:
  - platform: template
    id: weigth_tare_set
    name: 'Tare'
    on_press:
      - lambda: id(weigth_tare) = id(weigth_no_tare).state;
2 Likes

It works just perfect! That’s exactly what I couldn’t do! Thank you very much! <3