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

How do you create the service and the button on the dashboard that calls the service? Can you show the YAML code for that?

Figured it out, if anyone else is wondering, I did this:

Added this code in /homeassistant/scripts.yaml:

set_tare:
alias: “Set Tare”
sequence:
- service: esphome.vekt_hx711_tare_set_press
data: {}

And here is the yaml code for the Tare button:

type: button
tap_action:
action: call-service
service: esphome.vekt_hx711_set_tare
data: {}
show_name: true
show_icon: true
name: Set Tare
icon: mdi:scale

You dont even need to use any services, just an action that pushes the button. A template button is a “virtual button” just for HA so, it sort of is a service already.

Thanks for the clarification, I now see that the entry in /homeassistant/scripts.yaml is totally redundant!

Yes sir. Redundancy itself is definitely a good thing and i definitely recommend it. It has to make sense though, for example using mqtt as a secondary way to control devices incase your hardware running HA goes down. That is good and makes sense. In this case they basically are the same thing and dont offer much redundancy.

Ive been slowly making all my esphome/HA automations redundant so that I can which is primary and which is secondary. For example if HA is the primary for some automation. With simple conditions you can check to see if the node and HA are communicating and if not that is an indicator my HA automations will fail to launch and the esp node can take over because it has the same automations that HA had. If connection is reestablished, the esp node steps down and goes back into secondary.

Its not 100% but, if youve ever had a major HA crash then you know what a nightmare it is and although 80% isnt as good as 100%. Its monumentally better that 0%!

Thank you for the insight! I completely agree that redundancy is crucial, especially for maintaining functionality during potential HA crashes. Setting up ESPHome nodes to take over when HA is down is a smart approach. I’ll definitely consider implementing similar redundant automations to ensure greater reliability. Your strategy of having primary and secondary systems makes a lot of sense. Thanks again for sharing!