substitutions:
  local_update_interval: 30s

##########################################################################################
# Text Sensors
# https://esphome.io/components/text_sensor/index.html
##########################################################################################
text_sensor:
  - platform: version
    name: "Version:"
    entity_category: "diagnostic"
    disabled_by_default: true

  - platform: wifi_info
    ip_address:
      icon: mdi:ip-network
      entity_category: diagnostic
      name: "IP Address:"
    ssid:
      name: "Connected SSID"
      icon: mdi:wifi-strength-2
      entity_category: diagnostic
      disabled_by_default: true
    mac_address:
      name: "MAC Address:"
      icon: mdi:network-pos
      entity_category: diagnostic
      disabled_by_default: true

#####################################################################################################
# Creates a sensor of the uptime of the device, in formatted days, hours, minutes and seconds
###################################################################################################
#  - platform: template
#    name: "Uptime (Days)"
#    entity_category: diagnostic
#    lambda: |-
#      int seconds = (id(uptime_sensor).state);
#      int days = seconds / (24 * 3600);
#      seconds = seconds % (24 * 3600);
#      int hours = seconds / 3600;
#      seconds = seconds % 3600;
#      int minutes = seconds /  60;
#      seconds = seconds % 60;
#      if ( days > 3650 ) {
#        return  "Starting up" ;
#      } else if ( days ) {
#        return  (String(days) +"d " + String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() ;
#      } else if ( hours ) {
#        return  (String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() ;
#      } else if ( minutes ) {
#        return  (String(minutes) +"m "+ String(seconds) +"s").c_str() ;
#      } else {
#        return  (String(seconds) +"s").c_str() ;
#      }
#    icon: mdi:clock-start

###############################################################################
# WIFI & MQTT CONNECT COUNTERS
# Replicates Tasmota-style "WiFi Connect Count" and "MQTT Connect Count"
# - Increments each time the connection succeeds.
# - Skips counting reconnects during the first 60s after boot (uptime filter).
# - Persists across reboots (restore_value: yes).
###############################################################################

globals:
  - id: wifi_connect_count
    type: int
    restore_value: yes
    initial_value: '0'

  - id: mqtt_connect_count
    type: int
    restore_value: yes
    initial_value: '0'

# Uptime sensor to use for filtering
sensor:
  - platform: uptime
    id: uptime_seconds
    update_interval: 10s
    internal: true

  - platform: template
    name: "WiFi Connect Count"
    id: wifi_connect_sensor
    icon: mdi:wifi
    state_class: total_increasing
    entity_category: diagnostic
    #unit_of_measurement: "connections"
    accuracy_decimals: 0
    lambda: |-
      return id(wifi_connect_count);

  - platform: template
    name: "MQTT Connect Count"
    id: mqtt_connect_sensor
    icon: mdi:cloud-outline
    state_class: total_increasing
    entity_category: diagnostic
    #unit_of_measurement: "connections"
    accuracy_decimals: 0
    lambda: |-
      return id(mqtt_connect_count);

wifi:
  on_connect:
    then:
      - if:
          condition:
            lambda: 'return (id(uptime_seconds).state > 60);'  # Only count if uptime > 60s
          then:
            - lambda: |-
                id(wifi_connect_count)++;
                ESP_LOGI("wifi", "WiFi reconnected, count=%d", id(wifi_connect_count));
            - component.update: wifi_connect_sensor

mqtt:
  on_connect:
    then:
      - if:
          condition:
            lambda: 'return (id(uptime_seconds).state > 60);'  # Only count if uptime > 60s
          then:
            - lambda: |-
                id(mqtt_connect_count)++;
                ESP_LOGI("mqtt", "MQTT reconnected, count=%d", id(mqtt_connect_count));
            - component.update: mqtt_connect_sensor
