ScymnusRIP

Smart Scale edited

Dec 30th, 2021 (edited)
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Node configuration ##
  2. # Source https://gist.github.com/markusressel/e888544d4404fbed4c841ec83df638bb
  3. # markusressel/esphome_hx711_smart_scale.yaml
  4.  
  5. esphome:
  6.   name: scale-01
  7.   platform: ESP8266
  8.   board: esp12e
  9.  
  10. globals:
  11.   - id: scale_01_initial_zero
  12.     type: float
  13.     restore_value: yes
  14.     # NOTE: make sure to align this value to the one used in "calibrate_linear" below!
  15.     initial_value: '-31060'  #'-481989'
  16.    
  17.   - id: scale_01_auto_tare_enabled
  18.     type: bool
  19.     restore_value: yes
  20.     initial_value: 'true'
  21.  
  22.   - id: scale_01_auto_tare_difference
  23.     type: float
  24.     restore_value: yes
  25.     initial_value: '0'
  26.    
  27.   - id: scale_01_manual_tare_flag
  28.     type: bool
  29.     restore_value: no
  30.     initial_value: 'false'
  31.  
  32. wifi:
  33.   ssid: !secret wifi_ssid
  34.   password: !secret wifi_password
  35.  
  36. #wifi:
  37.  # fast_connect: True  # needed for hidden SSID
  38.   #networks:
  39.    # - ssid: 'YourSSID'
  40.     #  bssid: '00:00:00:00:00:00'
  41.      # password: 'YourPassword'
  42.       #hidden: true
  43.   #manual_ip:
  44.   #  static_ip: 192.168.5.49
  45.   #  gateway: 192.168.5.1
  46.   #  subnet: 255.255.255.0
  47.  
  48. api:
  49.   password: "YourApiPassword"
  50.  
  51. # Enable logging
  52. logger:
  53.   level: ERROR
  54.  
  55.  
  56. ota:
  57.   password: "06e5dcb2cfe4950d74460306be7606c1"
  58.  
  59. status_led:
  60.   pin:
  61.     number: GPIO2
  62.     inverted: True
  63.    
  64. switch:
  65.  ## Switch to enable/disable the auto tare feature
  66.   - platform: template
  67.     id: smart_scale_01_continuous_tare_enabled
  68.     name: "Smart Scale 01 Continuous Tare Enabled"
  69.     lambda: |-
  70.       return id(scale_01_auto_tare_enabled);
  71.     turn_on_action:
  72.       - lambda: |-
  73.           id(scale_01_auto_tare_enabled) = true;
  74.     turn_off_action:
  75.       - lambda: |-
  76.           id(scale_01_auto_tare_enabled) = false;
  77.  
  78.   ## Switch used to initiate a manual tare
  79.   - platform: template
  80.     id: smart_scale_01_manual_tare_action_switch
  81.     name: "Smart Scale 01 Manual Tare Action"
  82.     lambda: |-
  83.       return id(scale_01_manual_tare_flag);
  84.     turn_on_action:
  85.       - lambda: |-
  86.           id(scale_01_auto_tare_difference) = id(scale_01_initial_zero) - id(smart_scale_01_hx711_value_raw).state;
  87.       - switch.turn_off: smart_scale_01_manual_tare_action_switch
  88.     turn_off_action:
  89.       - lambda: |-
  90.           id(scale_01_manual_tare_flag) = false;
  91. ## Sensor Configuration ##
  92. sensor:
  93.  # template sensors from global variables
  94.   - platform: template
  95.     id: smart_scale_01_initial_zero
  96.     name: "Smart Scale 01 Initial Zero"
  97.     lambda: |-
  98.       return id(scale_01_initial_zero);
  99.     update_interval: 1s
  100.    
  101.   - platform: template
  102.     id: smart_scale_01_auto_tare_difference
  103.     name: "Smart Scale 01 Auto Tare Difference"
  104.     lambda: |-
  105.       return id(scale_01_auto_tare_difference);
  106.     update_interval: 1s
  107.    
  108.   # sensors imported from home assistant
  109.   - platform: homeassistant
  110.     id: homeassistant_scale_01_initial_zero
  111.     entity_id: input_number.smart_scale_01_initial_zero
  112.     on_value:
  113.       then:
  114.         - lambda: |-
  115.             id(scale_01_initial_zero) = x;
  116.  
  117.   # RAW Scale input
  118.   - platform: hx711
  119.     id: smart_scale_01_hx711_value_raw
  120.     internal: True #   <-- change this to False to see raw data
  121.     dout_pin: MISO #D0
  122.     clk_pin: SCK #D1
  123.     gain: 128
  124.     unit_of_measurement: g
  125.     accuracy_decimals: 6
  126.     update_interval: 0.2s
  127.     filters:
  128.       - sliding_window_moving_average:
  129.           window_size: 3
  130.           send_every: 1
  131.     on_value:
  132.       then:
  133.         - sensor.template.publish:
  134.             id: smart_scale_01_hx711_value
  135.             state: !lambda 'return id(smart_scale_01_hx711_value_raw).state;'
  136.         - if:
  137.             condition:
  138.               and:
  139.                 - lambda: 'return id(scale_01_auto_tare_enabled);'
  140.                 # current smart scale value is below approx. 10KG (raw value -275743) aka nobody is standing on the scale
  141.                 - lambda: 'return id(smart_scale_01_hx711_value).state < 10.0;'
  142.             then:
  143.               - if:
  144.                   condition:
  145.                    # current raw scale value is below expected zero value
  146.                     - lambda: 'return id(smart_scale_01_hx711_value_raw).state < (id(scale_01_initial_zero) - id(scale_01_auto_tare_difference));'
  147.                   then:
  148.                    # INcrease Auto-Tare offset to slowly align real zero value with expected zero value
  149.                     - lambda: |-
  150.                         id(scale_01_auto_tare_difference) += 1.0;
  151.                   else:
  152.                    # DEcrease Auto-Tare offset to slowly align real zero value with expected zero value
  153.                     - lambda: |-
  154.                         id(scale_01_auto_tare_difference) -= 1.0;
  155.    
  156.   # Mapped value to g
  157.   - platform: template
  158.     id: smart_scale_01_hx711_value
  159.     name: "Smart Scale 01 HX711 Value"
  160.     internal: False
  161.     filters:
  162.      # apply auto_tare difference
  163.       - lambda: 'return x + id(scale_01_auto_tare_difference);'
  164.       # apply rough calibration
  165.       - calibrate_linear:
  166.          # retrieve these values by evaluating the raw values with loads of known mass.
  167.           # note that a bigger difference between measurements usually results in higher resolution,
  168.           # 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)
  169.           #- -481989 -> 0
  170.           #- 1339163 -> 88.3
  171.             - -31060 -> 0.00000
  172.             #- -39684 -> 1.00000
  173.             - -16974 -> 10.00000
  174.             - 59674 -> 50.00000
  175.             - 349441 -> 200.00000
  176.             - 441888  -> 250.00000
  177.             - 733483 -> 400.00000
  178.             - 926142 -> 500.00000
  179.             - 1310869 -> 700.00000
  180.             - 1694630 -> 900.00000
  181.             - 1886776 -> 1000.00000
  182.       # map values below 0.1 to 0 (to decrease value changes due to random fluctuation)
  183.       - lambda: |-
  184.           if (x <= 0.1) {
  185.             return 0.0;
  186.           } else {
  187.             return x;
  188.           }
  189.          
  190.     unit_of_measurement: g
  191.     accuracy_decimals: 6
  192.     update_interval: 0.2s
  193.     force_update: True
Add Comment
Please, Sign In to add comment