-
Notifications
You must be signed in to change notification settings - Fork 15
/
esphome_hx711_smart_scale.yaml
198 lines (178 loc) · 5.98 KB
/
esphome_hx711_smart_scale.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
## Node configuration ##
esphome:
name: smart_scale
platform: ESP8266
board: nodemcuv2
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: '-481989'
- id: auto_tare_enabled
type: bool
restore_value: yes
initial_value: 'true'
- id: auto_tare_difference
type: float
restore_value: yes
initial_value: '0'
wifi:
fast_connect: True # needed for hidden SSID
networks:
- ssid: 'YourSSID'
bssid: '00:00:00:00:00:00'
password: 'YourPassword'
hidden: true
#manual_ip:
# static_ip: 192.168.5.49
# gateway: 192.168.5.1
# subnet: 255.255.255.0
api:
password: "YourApiPassword"
# Enable logging
logger:
level: ERROR
ota:
platform: esphome
password: 'YourOtaPassword'
status_led:
pin:
number: GPIO2
inverted: True
button:
## Button used to initiate a manual tare
- platform: template
id: smart_scale_manual_tare_action_switch
name: "Smart Scale Manual Tare Action"
icon: mdi:scale-balance
on_press:
- lambda: |-
id(auto_tare_difference) = id(initial_zero) - id(smart_scale_hx711_value_raw).state;
- sensor.template.publish:
id: smart_scale_hx711_value_raw_diagnostic
state: !lambda 'return id(smart_scale_hx711_value_raw).state;'
switch:
## Switch to enable/disable the auto tare feature
- platform: template
id: smart_scale_continuous_tare_enabled
name: "Smart Scale Continuous Tare Enabled"
entity_category: "config"
lambda: |-
return id(auto_tare_enabled);
turn_on_action:
- lambda: |-
id(auto_tare_enabled) = true;
turn_off_action:
- lambda: |-
id(auto_tare_enabled) = false;
## Sensor Configuration ##
sensor:
# template sensors from global variables
- platform: template
id: smart_scale_initial_zero
name: "Smart Scale Initial Zero"
lambda: |-
return id(initial_zero);
update_interval: 1s
- platform: template
id: smart_scale_auto_tare_difference
name: "Smart Scale Auto Tare Zero Calibration Offset"
lambda: |-
return id(auto_tare_difference);
update_interval: 1s
- platform: template
id: smart_scale_auto_tare_deviance
entity_category: "diagnostic"
name: "Smart Scale Calibration Deviance"
lambda: |-
return (int((id(smart_scale_hx711_value_raw).state - (id(initial_zero) - id(auto_tare_difference)) ) / 100)) * 100;
update_interval: 1s
# sensors imported from home assistant
- platform: homeassistant
id: homeassistant_initial_zero
entity_id: input_number.smart_scale_initial_zero
on_value:
then:
- lambda: |-
id(initial_zero) = x;
# RAW Scale input
- platform: hx711
id: smart_scale_hx711_value_raw
internal: True
dout_pin: D0
clk_pin: D1
gain: 128
update_interval: 0.2s
filters:
- quantile:
window_size: 10
send_every: 1
send_first_at: 1
quantile: .9
on_value:
then:
- sensor.template.publish:
id: smart_scale_hx711_value
state: !lambda 'return id(smart_scale_hx711_value_raw).state;'
- if:
condition:
- lambda: |-
auto n = id(smart_scale_hx711_value_raw_diagnostic).state;
auto n_str = to_string(n);
return str_equals_case_insensitive(n_str, "NaN");
then:
- sensor.template.publish:
id: smart_scale_hx711_value_raw_diagnostic
state: !lambda 'return id(smart_scale_hx711_value_raw).state;'
- if:
condition:
and:
- lambda: 'return id(auto_tare_enabled);'
# current smart scale value is below approx. 10KG (raw value -275743) aka nobody is standing on the scale
- lambda: 'return id(smart_scale_hx711_value).state < 10.0;'
# only update if the current deviance is not 0
- lambda: 'return id(smart_scale_auto_tare_deviance).state != 0.0;'
then:
- if:
condition:
# current raw scale value is below expected zero value
- lambda: 'return id(smart_scale_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;
- platform: template
id: smart_scale_hx711_value_raw_diagnostic
name: "Smart Scale HX711 Raw Value"
entity_category: "diagnostic"
# Mapped value to KG
- platform: template
id: smart_scale_hx711_value
name: "Smart Scale HX711 Value"
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)
- -481989 -> 0
- 1339163 -> 88.3
# 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
# the sensor will automatically update through the 'sensor.template.publish' call
update_interval: never