substitutions:
  #name must only contain lowercase characters and no space
  name: "toilette-rulle"
  friendly_name: "Toilette Rulle"
  comment: "Alerte bei läschter Rulle"
  wifi_name: "Wifi-name-here"
  wifi_pwd: "password"
  ap_ssid: "toilette-rull"
  trigger_pin: GPIO15
  echo_pin: GPIO13
  calibration_sensor_name: "Kalibratioun"
  distance_sensor_name: "Distanz"
  binary_sensor_name: "Sensor fir Home Assistant"
  state_name: "Status"
  web_srv_grp_sensor_name: "Sensor & Kontroll"
  web_srv_grp_diagnostic_name: "Diagnos"
  firmware_sensor_name: "Firmware Versioun"

globals:
  - id: roll_present_text
    type: std::string
    initial_value: '"Nach op manst 1 do"'
  - id: roll_nonpresent_text
    type: std::string
    initial_value: '"Keng méi do"'

esp32:
  board: esp32dev
  framework:
    type: esp-idf

esphome:
  name: $name
  friendly_name: $friendly_name
  comment: $comment
  project:
    name: "hiscorebob.toilette_rulle"
    version: "2026.4.5"

preferences:
  flash_write_interval: 60min

web_server:
  version: 3
  log: False
  ota: False
  sorting_groups:
    - id: control
      name: $web_srv_grp_sensor_name
      sorting_weight: 10
    - id: status
      name: $web_srv_grp_diagnostic_name
      sorting_weight: 20
#must be enabled for improv_serial
logger:

improv_serial:
  next_url: http://{{ip_address}}

ota: 
  platform: esphome


api:
  reboot_timeout: 0s

wifi:
  min_auth_mode: WPA2
  ssid: $wifi_name
  password: $wifi_pwd
  fast_connect: False
  reboot_timeout: 0s
  ap:
    ssid: $ap_ssid
    password: ""

captive_portal:

number:
  - platform: template
    id: sensitivity
    name: $calibration_sensor_name
    optimistic: true
    restore_value: true
    min_value: 1
    max_value: 9
    initial_value: 6
    unit_of_measurement: "cm"
    step: 1
    icon: "mdi:tune-variant"
    web_server:
      sorting_group_id: control    

sensor:
  - platform: ultrasonic
    trigger_pin: $trigger_pin
    echo_pin: $echo_pin
    name: $distance_sensor_name
    id: distance
    unit_of_measurement: cm
    filters:
      - multiply: 100  # Convert meters to centimeters
    accuracy_decimals: 0
    update_interval: 3s
    timeout: 0.75m
    web_server:
      sorting_group_id: control
      
binary_sensor:
  - platform: template
    name: "$binary_sensor_name"
    id: toilet_paper_roll
    web_server:
      sorting_group_id: status
    icon: "mdi:home-assistant"
    lambda: |-
        if (id(distance).state < (id(sensitivity).state)) {
          id(rull_status).publish_state(id(roll_present_text));
          return false;
        } else {
          id(rull_status).publish_state(id(roll_nonpresent_text));
          return true;
        }

text_sensor:
 - platform: template
   name: $firmware_sensor_name
   id: esphome_project_version_text_detailed
   icon: "mdi:information-box"
   entity_category: "diagnostic"
   update_interval: 60s
   lambda: |-
      return { ESPHOME_PROJECT_VERSION };
   web_server:
     sorting_group_id: status
 - platform: template
   id: rull_status
   name: $state_name
   icon: "mdi:paper-roll"
   web_server:
     sorting_group_id: control