substitutions:
  waterlevel_current_init: '4.06' # mA at 0mm
  waterlevel_range: '1000' # mm
  waterlevel_density: '1' # water

globals:
  - id: waterlevel_current_init
    type: float
    restore_value: yes
    initial_value: ${waterlevel_current_init}
  - id: waterlevel_range
    type: float
    restore_value: yes
    initial_value: ${waterlevel_range}
  - id: waterlevel_density
    type: float
    restore_value: yes
    initial_value: ${waterlevel_density}

ads1115:
  - address: 0x49
    id: ads1115_waterlevel

sensor:
  - platform: ads1115
    ads1115_id: ads1115_waterlevel
    id: waterlevel_tank_voltage
    internal: true
    #name: "Waterlevel Tank: Voltage"
    multiplexer: 'A0_GND'
    gain: 4.096 # max: 3.0V
    update_interval: 5s
    accuracy_decimals: 5
    <<: !include filter.yaml
  - platform: ads1115
    ads1115_id: ads1115_waterlevel
    id: waterlevel_system_voltage
    internal: true
    #name: "Waterlevel System: Voltage"
    multiplexer: 'A1_GND'
    gain: 4.096 # max: 3.0V
    update_interval: 5s
    accuracy_decimals: 5
    <<: !include filter.yaml

  # - platform: template
  #   name: "Waterlevel System: Current"
  #   id: waterlevel_tank_current
  #   unit_of_measurement: "mA"
  #   accuracy_decimals: 2
  #   update_interval: 5s
  #   lambda: !lambda 'return id(waterlevel_tank_voltage).state * 1024.0 / 120.0;' # V->mV, Sense Resistor: 120ohm'
  # - platform: template
  #   name: "Waterlevel Tank: Current"
  #   id: waterlevel_system_current
  #   unit_of_measurement: "mA"
  #   accuracy_decimals: 2
  #   update_interval: 5s
  #   lambda: !lambda 'return id(waterlevel_system_voltage).state * 1024.0 / 120.0;'  # V->mV, Sense Resistor: 120ohm

  - platform: template
    name: "Waterlevel: Tank"
    id: waterlevel_tank
    unit_of_measurement: "cm"
    accuracy_decimals: 2
    update_interval: 5s
    lambda: |-
      float dataCurrent = id(waterlevel_tank_voltage).state * 1024.0 / 120.0;  // Sense Resistor: 120ohm
      float depth = (dataCurrent - id(waterlevel_current_init)) * (id(waterlevel_range) / id(waterlevel_density) / 16.0) / 10;
      return depth;
  - platform: template
    name: "Waterlevel: System"
    id: waterlevel_system
    unit_of_measurement: "cm"
    accuracy_decimals: 2
    update_interval: 5s
    lambda: |-
      float dataCurrent = id(waterlevel_system_voltage).state * 1024.0 / 120.0; // Sense Resistor: 120ohm
      float depth = (dataCurrent - id(waterlevel_current_init)) * (id(waterlevel_range) / id(waterlevel_density) / 16.0) / 10;
      return depth;

prometheus:
  relabel:
    waterlevel_system:
      id: waterlevel_system
      name: "Waterlevel System"
    waterlevel_tank:
      id: waterlevel_tank
      name: "Waterlevel Tank"
