substitutions:
  batt_number: "2"
  friendly_name: "BMS Bateria ${batt_number}"

esphome:
  name: batt${batt_number}
  comment: TinyBMS Bateria ${batt_number}

esp32:
  board: esp32dev
  framework:
    type: arduino
# WiFi connection
# Enable Home Assistant API
api:

ota:

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "TinyBMS Fallback Hotspot"
    password: !secret wifi_password

captive_portal:
# Enable logging
logger:
  baud_rate: 0

# Enable Web server
web_server:
  port: 80

# Sync time with Home Assistant
time:
  - platform: homeassistant
    id: homeassistant_time

uart:
  id: mod_bus
  tx_pin: TX
  rx_pin: RX
  baud_rate: 115200
  parity: NONE
  stop_bits: 1
  data_bits: 8

modbus:
  id: modbus1
  uart_id: mod_bus
  send_wait_time: 200ms

modbus_controller:
  id: TinyBMS
  ## the Modbus device addr
  address: 0xAA
  modbus_id: modbus1
  update_interval: 5s

spi:
  clk_pin: GPIO27
  mosi_pin: GPIO14
  miso_pin: GPIO32 ##
  id: ili9341_spi

font:
  - file: './config/slkscr.ttf'
    id: font1
    size: 8

  - file: './config/BebasNeue-Regular.ttf'
    id: font2
    size: 40

display:
  - platform: st7735
    model: "INITR_18BLACKTAB"
    id: oled
    reset_pin: GPIO32 ##
    cs_pin: GPIO13
    dc_pin: GPIO12
    rotation: 90
    device_width: 128
    device_height: 160
    col_start: 0
    row_start: 0
    eight_bit_color: true
    update_interval: 5s
    lambda: |-
      auto white = Color(255, 255, 255);
      auto green = Color (0,255,0);
      auto blue = Color(0, 0, 255);
      auto dark_blue = Color(0, 0, 64);
      auto upcell = Color(255, 0, 255);
      auto normalcell = Color(0, 0, 255 );
      auto downcell = Color(0, 255, 255 );
      auto color = Color(0,0,0);
          
      float cells[] = {
        id(bms_cell_1_v_${batt_number}).state,
        id(bms_cell_2_v_${batt_number}).state,
        id(bms_cell_3_v_${batt_number}).state,
        id(bms_cell_4_v_${batt_number}).state,
        id(bms_cell_5_v_${batt_number}).state,
        id(bms_cell_6_v_${batt_number}).state,
        id(bms_cell_7_v_${batt_number}).state,
        id(bms_cell_8_v_${batt_number}).state,
        id(bms_cell_9_v_${batt_number}).state,
        id(bms_cell_10_v_${batt_number}).state,
        id(bms_cell_11_v_${batt_number}).state,
        id(bms_cell_12_v_${batt_number}).state,
        id(bms_cell_13_v_${batt_number}).state,
        id(bms_cell_14_v_${batt_number}).state,
        id(bms_cell_15_v_${batt_number}).state,
        id(bms_cell_16_v_${batt_number}).state
      };
      float cellmin = 5;
      float cellmax = 0;
      for (int cell = 0; cell < 16; cell++){
        if (cells[cell] <  cellmin) cellmin = cells[cell];
        if (cells[cell] >  cellmax) cellmax = cells[cell];
      }

      for (int cell = 0; cell < 16; cell++){
        if (cells[cell] <=  cellmin){
          color = downcell;
        }else if (cells[cell] >=  cellmax){
          color = upcell;
        }else{
          color = normalcell;
        }
        it.filled_rectangle(  0+cell*10,   (45-round(((cells[cell])-3)/0.025)), 10,round(((cells[cell])-3)/0.025), color); //imprime barra celda con su color
      };
    
      for (int i = 0; i<160; i=i+20) {
        it.rectangle(   0+i, 5,10, 40, dark_blue); // borde columnas
      }
      
      it.rectangle(  0, 5, 160, 40, dark_blue); //marco      
      it.line(0, 15, 160, 15, dark_blue); //renglones columnas
      it.line(0, 25, 160, 25, dark_blue);
      it.line(0, 35, 160, 35, dark_blue);      
      
      it.printf(80, 81, id(font2), white,TextAlign::BASELINE_CENTER, "%s", id(bms_online_status_text_${batt_number}).state.c_str() );
      
      it.print ( 5,  95, id(font1), white,TextAlign::BASELINE_LEFT, "SOC:");
      it.printf( 38,  95, id(font1), green,TextAlign::BASELINE_LEFT, "%.1f%%", id(battery_soc_${batt_number}).state);

      it.print(5, 106, id(font1), white,TextAlign::BASELINE_LEFT, "Volt:");
      it.printf(38, 106, id(font1), green,TextAlign::BASELINE_LEFT, "%.2fV", id(battery_volt_${batt_number}).state);

      it.print(5, 117, id(font1), white,TextAlign::BASELINE_LEFT, "Amp:");
      it.printf(38, 117, id(font1), green,TextAlign::BASELINE_LEFT, "%.2fA",  id(battery_amp_${batt_number}).state);

      it.print(5, 128, id(font1), white,TextAlign::BASELINE_LEFT, "Temp1:");
      it.printf(38, 128, id(font1), green,TextAlign::BASELINE_LEFT, "%.1fC",id(battery_temp_1_${batt_number}).state);
      
      it.print(78, 95,  id(font1), white,TextAlign::BASELINE_LEFT,  "Cell Dif:");
      it.printf(155, 95,  id(font1), green,TextAlign::BASELINE_RIGHT,  "%.0fmV", id(bms_cell_diff_${batt_number}).state);

      it.print(78, 106,  id(font1), white,TextAlign::BASELINE_LEFT, "Min.Cell:");
      it.printf(155, 106,  id(font1), downcell,TextAlign::BASELINE_RIGHT, "%.2fV",  id(minimal_cell_voltage_${batt_number}).state);

      it.print(78, 117,  id(font1), white,TextAlign::BASELINE_LEFT, "Max.Cell:");
      it.printf(155, 117,  id(font1), upcell,TextAlign::BASELINE_RIGHT, "%.2fV",  id(maximal_cell_voltage_${batt_number}).state);

      it.print(78, 128,  id(font1), white,TextAlign::BASELINE_LEFT, "Temp2:"); 
      it.printf(155, 128,  id(font1), green,TextAlign::BASELINE_RIGHT, "%.1fC",     id(battery_temp_2_${batt_number}).state); 
      
switch:
  - platform: gpio
    id: salida_led_display_bms_${batt_number}
    pin: GPIO4
    name: Salida Led Display BMS ${batt_number}
    internal: true
    
  - platform: restart
    name: Batt ${batt_number} BMS Restart
    id: batt_${batt_number}_bms_restart

binary_sensor:
  - platform: gpio
    name: "Display"
    id: display_on_off
    internal: true
    pin:
      number: GPIO15
      mode: INPUT_PULLDOWN
    on_multi_click:
    - timing:
        - ON for 3s to 10s
        - OFF for at least 0.5s
      then:
        - lambda: id(batt_${batt_number}_bms_restart).turn_on();
        - logger.log: "Single Long Clicked"
    - timing:
        - ON for at most 1s
        - OFF for at least 0.5s
      then:
        - lambda: id(salida_led_display_bms_${batt_number}).turn_on();


sensor:

  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Balancing Decision Bits V ${batt_number}
    id: Balancing_Decision_Bits_v_${batt_number}
    register_type: holding
    address: 51
    register_count: 1
    value_type: U_WORD
    
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Real Balancing Bits V ${batt_number}
    id: Real_Balancing_Bits_v_${batt_number}
    register_type: holding
    address: 52

  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Internal Temp 1 ${batt_number}
    id: BMS_Internal_temp_1_${batt_number}
    register_type: holding
    address: 48
    register_count: 1
    unit_of_measurement: "ºC"
    value_type: U_WORD
    accuracy_decimals: 1
    filters:
      - multiply: 0.1 

  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: Battery Volt ${batt_number}
    id: battery_volt_${batt_number}
    register_type: holding
    address: 36
    register_count: 2
    unit_of_measurement: "V"
    value_type: FP32_R
    accuracy_decimals: 3
    
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: Battery Amp ${batt_number}
    id: battery_amp_${batt_number}
    register_type: holding
    address: 38
    register_count: 2
    unit_of_measurement: "A"
    value_type: FP32_R
    accuracy_decimals: 2
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: Minimal Cell Voltage ${batt_number}
    id: minimal_cell_voltage_${batt_number}
    register_type: holding
    address: 40
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.001 
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: Maximal Cell Voltage ${batt_number}
    id: maximal_cell_voltage_${batt_number}
    register_type: holding
    address: 41
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.001 
  
      
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: Battery Temp 1 ${batt_number}
    id: battery_temp_1_${batt_number}
    register_type: holding
    address: 42
    register_count: 1
    unit_of_measurement: "ºC"
    value_type: U_WORD
    accuracy_decimals: 1
    filters:
      - multiply: 0.1 
      
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: Battery Temp 2 ${batt_number}
    id: battery_temp_2_${batt_number}
    register_type: holding
    address: 43
    register_count: 3 #agrego registros para llenar los huecos y que haga una sola lectura
    unit_of_measurement: "ºC"
    value_type: U_WORD
    accuracy_decimals: 1
    filters:
      - multiply: 0.1 
      
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: Battery SOC ${batt_number}
    id: battery_soc_${batt_number}
    register_type: holding
    address: 46
    register_count: 4 #+2
    unit_of_measurement: "%"
    value_type: U_DWORD_R
    accuracy_decimals: 0
    filters:
      - multiply: 0.000001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Online Status ${batt_number}
    id: bms_online_status_${batt_number}
    register_type: holding
    address: 50
    register_count: 2
    unit_of_measurement: "%"
    value_type: U_WORD
  
  
  ## leo tensiones celdas
  
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 1 V ${batt_number}
    id: bms_cell_1_v_${batt_number}
    register_type: holding
    address: 0
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 2 V ${batt_number}
    id: bms_cell_2_v_${batt_number}
    register_type: holding
    address: 1
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 3 V ${batt_number}
    id: bms_cell_3_v_${batt_number}
    register_type: holding
    address: 2
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
      
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 4 V ${batt_number}
    id: bms_cell_4_v_${batt_number}
    register_type: holding
    address: 3
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 5 V ${batt_number}
    id: bms_cell_5_v_${batt_number}
    register_type: holding
    address: 4
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 6 V ${batt_number}
    id: bms_cell_6_v_${batt_number}
    register_type: holding
    address: 5
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 7 V ${batt_number}
    id: bms_cell_7_v_${batt_number}
    register_type: holding
    address: 6
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 8 V ${batt_number}
    id: bms_cell_8_v_${batt_number}
    register_type: holding
    address: 7
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 9 V ${batt_number}
    id: bms_cell_9_v_${batt_number}
    register_type: holding
    address: 8
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 10 V ${batt_number}
    id: bms_cell_10_v_${batt_number}
    register_type: holding
    address: 9
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 11 V ${batt_number}
    id: bms_cell_11_v_${batt_number}
    register_type: holding
    address: 10
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 12 V ${batt_number}
    id: bms_cell_12_v_${batt_number}
    register_type: holding
    address: 11
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 13 V ${batt_number}
    id: bms_cell_13_v_${batt_number}
    register_type: holding
    address: 12
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 14 V ${batt_number}
    id: bms_cell_14_v_${batt_number}
    register_type: holding
    address: 13
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 15 V ${batt_number}
    id: bms_cell_15_v_${batt_number}
    register_type: holding
    address: 14
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
  
  - platform: modbus_controller
    modbus_controller_id: TinyBMS
    name: BMS Cell 16 V ${batt_number}
    id: bms_cell_16_v_${batt_number}
    register_type: holding
    address: 15
    register_count: 1
    unit_of_measurement: "V"
    value_type: U_WORD
    accuracy_decimals: 3
    filters:
      - multiply: 0.0001
      
  - platform: template
    name: BMS Cell Diff ${batt_number}
    id: bms_cell_diff_${batt_number}
    update_interval: 5s
    unit_of_measurement: "mV"
    accuracy_decimals: 0
    lambda: |-
      return {((id(maximal_cell_voltage_${batt_number}).state - id(minimal_cell_voltage_${batt_number}).state))*1000};

text_sensor:
  - platform: template
    name: BMS Online Status Text ${batt_number}
    id: bms_online_status_text_${batt_number}
    update_interval: 5s
    lambda: |-
      if (     id(bms_online_status_${batt_number}).state == 145){return {"Charging"};}
      else if (id(bms_online_status_${batt_number}).state == 146){return {"Full"};}
      else if (id(bms_online_status_${batt_number}).state == 147){return {"Discharging"};}
      else if (id(bms_online_status_${batt_number}).state == 150){return {"Regenerate"};}
      else if (id(bms_online_status_${batt_number}).state == 151){return {"Stationary"};}
      else {return {"Fault"};}
