substitutions:
  devicename: og-sz-us-room

esphome:
  name: ${devicename}
  platform: ESP8266
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "78f0e056acef6b72d384251c368455f7"

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

  manual_ip:
    static_ip: 192.168.1.92
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${devicename}"
    password: "Q80sOv2BQybI"

captive_portal:

    
# Example configuration entry
light:
  - platform: neopixelbus
    id: neopixel
    name: "NeoPixel Light"
    type: RGB
    variant: WS2812
    pin: RX
    num_leds: 12
    internal: true

  - platform: partition
    name: "Status LED 01"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 0
        to: 0

  - platform: partition
    name: "Status LED 02"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 1
        to: 1

  - platform: partition
    name: "Status LED 03"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 2
        to: 2

  - platform: partition
    name: "Status LED 04"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 3
        to: 3

  - platform: partition
    name: "Status LED 05"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 4
        to: 4

  - platform: partition
    name: "Status LED 06"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 5
        to: 5

  - platform: partition
    name: "Status LED 07"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 6
        to: 6

  - platform: partition
    name: "Status LED 08"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 7
        to: 7

  - platform: partition
    name: "Status LED 09"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 8
        to: 8

  - platform: partition
    name: "Status LED 10"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 9
        to: 9

  - platform: partition
    name: "Status LED 11"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 10
        to: 10

  - platform: partition
    name: "Status LED 12"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 11
        to: 11

status_led:
  pin:
    number: GPIO2
    inverted: True
    
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'
    initial_value: '-906042'

  - 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'

switch:
  - platform: restart
    name: "${devicename} Neustart"
    
  ## Switch to enable/disable the auto tare feature
  - platform: template
    id: smart_scale_continuous_tare_enabled
    name: "Smart Scale 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: smart_scale_manual_tare_action_switch
    name: "Smart Scale Manual Tare Action"
    lambda: |-
      return id(manual_tare_flag);
    turn_on_action:
      - lambda: |-
          id(auto_tare_difference) = id(initial_zero) - id(smart_scale_hx711_value_raw).state;
      - switch.turn_off: smart_scale_manual_tare_action_switch
    turn_off_action:
      - lambda: |-
          id(manual_tare_flag) = 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 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.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: D2
    clk_pin: D3
    gain: 128
    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: smart_scale_hx711_value
            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;'
            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;
    
  # 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
          - 906042 -> 0
          - 903647 -> 0.523
          - 866398 -> 1.027
      # 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: 0.2s

  - platform: wifi_signal
    name: "${devicename} Wi-Fi Signal"
    update_interval: 60s

  - platform: uptime
    name: "${devicename} Uptime"
    update_interval: 60s