Forked from markusressel/esphome_hx711_smart_scale.yaml
Last active
December 13, 2024 16:32
-
-
Save Aasikki/82bf0238fecb7328b18e2727902d7dca to your computer and use it in GitHub Desktop.
ESPHome sample configuration for an HX711 powered Smart Scale including Auto-Tare functionality.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################ | |
# Secrets | |
################################################ | |
# Add these to your secrets.yaml (in the top right corner of esphome): | |
# wifi_ssid: "your_wifi_ssid" | |
# wifi_password: "your_wifi_password" | |
# api_key: "your_api_key" | |
# ota_password: "your_ota_password" | |
# You can get a randomly generated key for api_key and ota_password from here: | |
# https://esphome.io/components/api.html | |
# You can also enter them direcly in here, | |
# but setting them in secrets.yaml allows you to easily reuse them for other esphome devices | |
wifi: | |
ssid: !secret wifi_ssid | |
password: !secret wifi_password | |
api: | |
encryption: | |
key: !secret api_key | |
ota: | |
password: !secret ota_password | |
################################################ | |
esphome: | |
name: bed-occupancy | |
platform: ESP8266 | |
board: d1_mini_lite | |
globals: | |
- id: initial_zero | |
type: float | |
restore_value: yes | |
# NOTE: make sure to align this value to the one used in "calibrate_linear" below! | |
initial_value: '1822466' | |
- id: auto_tare_enabled | |
type: bool | |
restore_value: yes | |
initial_value: 'true' | |
- id: auto_tare_difference | |
type: float | |
restore_value: yes | |
initial_value: '0' | |
- id: manual_tare_flag | |
type: bool | |
restore_value: no | |
initial_value: 'false' | |
# Enable logging | |
logger: | |
level: ERROR | |
status_led: | |
pin: | |
number: GPIO2 | |
inverted: True | |
allow_other_uses: true | |
switch: | |
## Switch to enable/disable the auto tare feature | |
- platform: template | |
id: bed_occupancy_continuous_tare_enabled | |
name: "Bed Occupancy Continuous Tare Enabled" | |
lambda: |- | |
return id(auto_tare_enabled); | |
turn_on_action: | |
- lambda: |- | |
id(auto_tare_enabled) = true; | |
turn_off_action: | |
- lambda: |- | |
id(auto_tare_enabled) = false; | |
## Switch used to initiate a manual tare | |
- platform: template | |
id: bed_occupancy_manual_tare_action_switch | |
name: "Bed Occupancy Manual Tare Action" | |
lambda: |- | |
return id(manual_tare_flag); | |
turn_on_action: | |
- lambda: |- | |
id(auto_tare_difference) = id(initial_zero) - id(bed_occupancy_hx711_value_raw).state; | |
- switch.turn_off: bed_occupancy_manual_tare_action_switch | |
turn_off_action: | |
- lambda: |- | |
id(manual_tare_flag) = false; | |
## Sensor Configuration ## | |
sensor: | |
# template sensors from global variables | |
- platform: template | |
id: bed_occupancy_initial_zero | |
name: "Bed Occupancy Initial Zero" | |
lambda: |- | |
return id(initial_zero); | |
update_interval: 1s | |
- platform: template | |
id: bed_occupancy_auto_tare_difference | |
name: "Bed Occupancy Auto Tare Difference" | |
lambda: |- | |
return id(auto_tare_difference); | |
update_interval: 1s | |
# sensors imported from home assistant | |
- platform: homeassistant | |
id: homeassistant_initial_zero | |
entity_id: input_number.bed_occupancy_initial_zero | |
on_value: | |
then: | |
- lambda: |- | |
id(initial_zero) = x; | |
################################################ | |
# RAW Scale input | |
################################################ | |
#Bed 1 RAW | |
- platform: hx711 | |
name: "bed1" | |
id: bed1 | |
dout_pin: | |
number: D4 | |
allow_other_uses: true | |
clk_pin: D3 | |
gain: 128 | |
update_interval: 0.2s | |
internal: true | |
#Bed 2 RAW | |
- platform: hx711 | |
name: "bed2" | |
id: bed2 | |
dout_pin: D2 | |
clk_pin: D1 | |
gain: 128 | |
update_interval: 0.2s | |
internal: true | |
#Summed RAW | |
- platform: template | |
id: bed_occupancy_hx711_value_raw | |
lambda: |- | |
return (id(bed1).state + id(bed2).state); | |
internal: True | |
unit_of_measurement: kg | |
accuracy_decimals: 3 | |
update_interval: 0.2s | |
filters: | |
- sliding_window_moving_average: | |
window_size: 3 | |
send_every: 1 | |
on_value: | |
then: | |
- sensor.template.publish: | |
id: bed_occupancy_hx711_value | |
state: !lambda 'return id(bed_occupancy_hx711_value_raw).state;' | |
- if: | |
condition: | |
and: | |
- lambda: 'return id(auto_tare_enabled);' | |
# current Bed Occupancy value is below approx. 10KG (raw value -275743) aka nobody is standing on the scale | |
- lambda: 'return id(bed_occupancy_hx711_value).state < 10.0;' | |
then: | |
- if: | |
condition: | |
# current raw scale value is below expected zero value | |
- lambda: 'return id(bed_occupancy_hx711_value_raw).state < (id(initial_zero) - id(auto_tare_difference));' | |
then: | |
# INcrease Auto-Tare offset to slowly align real zero value with expected zero value | |
- lambda: |- | |
id(auto_tare_difference) += 10; | |
else: | |
# DEcrease Auto-Tare offset to slowly align real zero value with expected zero value | |
- lambda: |- | |
id(auto_tare_difference) -= 10; | |
# Mapped value to KG | |
- platform: template | |
id: bed_occupancy_hx711_value | |
name: "Bed Occupancy HX711 Value" | |
icon: mdi:scale | |
internal: False | |
filters: | |
# apply auto_tare difference | |
- lambda: 'return x + id(auto_tare_difference);' | |
# apply rough calibration | |
- calibrate_linear: | |
# retrieve these values by evaluating the raw values with loads of known mass. | |
# note that a bigger difference between measurements usually results in higher resolution, | |
# so measure 0 Kg and the highest known mass you have (like f.ex. your own weight, measured by a normal scale with good accuracy) | |
- 1822466 -> 0 | |
- 2095070 -> 40 | |
# map values below 0.1 to 0 (to decrease value changes due to random fluctuation) | |
- lambda: |- | |
if (x <= 0.1) { | |
return 0.0; | |
} else { | |
return x; | |
} | |
unit_of_measurement: kg | |
accuracy_decimals: 2 | |
update_interval: 2s | |
binary_sensor: | |
- platform: template | |
name: "Bed Occupied" | |
device_class: occupancy | |
icon: mdi:bed-king | |
lambda: |- | |
if (id(bed_occupancy_hx711_value).state > 20) { | |
return true; | |
} else { | |
return false; | |
} | |
- platform: template | |
name: "Bed Full" | |
icon: mdi:bed-king | |
lambda: |- | |
if (id(bed_occupancy_hx711_value).state > 50) { | |
return true; | |
} else { | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment