
modbus:  
  - name: M-TEC
    type: rtuovertcp
    host: 000.000.000.000 # suche nach deinem WR im Router nach "espressif_M-TEC" und uebernehme die IP

    port: 502 #M-TEC changed their Modbus to Port 502 with firmware V27.52.4.0! in der APP unter Firmware Slave zu finden
    timeout: 5
    # type: serial
    # baudrate: 9600
    # bytesize: 8
    # method: rtu
    # parity: N
    # port: /dev/ttyUSB1
    # stopbits: 1
    switches:
      - name:  Grid Injection Power Limit Switch  #0:Off;1:ON
        unique_id: grid_injection_power_limit_switch #RW
        slave: 252
        address: 25100
        write_type: holding
        scan_interval: 360
        command_on: 1
        command_off: 0
        verify:
            input_type: holding
            address: 25100
            state_on: 1
            state_off: 0
      - name:  Off-grid Battery SOC Protection Switch  #0:Off;1:ON
        unique_id: off_grid_battery_soc_protection_switch #RW
        slave: 252
        address: 52504
        write_type: holding
        scan_interval: 360
        command_on: 1
        command_off: 0
        verify:
            input_type: holding
            address: 52504
            state_on: 1
            state_off: 0
      - name:  On-grid Battery SOC Protection Switch  #0:Off;1:ON
        unique_id: on_grid_battery_soc_protection_switch #RW
        slave: 252
        address: 52502
        write_type: holding
        scan_interval: 360
        command_on: 1
        command_off: 0
        verify:
            input_type: holding
            address: 52502
            state_on: 1
            state_off: 0
      - name:  Priority Power Output Setting  #0：PV Output Priority ; 1：Battery Output Priority
        unique_id: priority_power_output_setting #RW
        slave: 252
        address: 50210
        write_type: holding
        scan_interval: 360
        command_on: 1
        command_off: 0
        verify:
            input_type: holding
            address: 50210
            state_on: 1
            state_off: 0


    sensors:
      - name:  Inverter SN
        unique_id: inverter_sn
        slave: 252
        address: 10000
        input_type: holding
        count: 10
        data_type: string
        scan_interval: 86400
        #lazy_error_count: 10
      - name:  Firmware Version1
        unique_id: firmware_version1
        slave: 252
        address: 10011
        input_type: holding
        count: 2
        data_type: custom
        structure: ">BBBB"
        scan_interval: 600
        #lazy_error_count: 10
      - name:  Firmware Version2
        unique_id: firmware_version2
        slave: 252
        address: 10013
        input_type: holding
        count: 2
        data_type: custom
        structure: ">BBBB"
        scan_interval: 60
        #lazy_error_count: 10

      - name:  Inverter Running Status #0:wait, wait for on-grid 1:check, self-check 2:On Grid 3:fault 4:flash, firmware update 5.Off Grid
        unique_id: inverter_running_status
        slave: 252
        address: 10105
        input_type: holding
        data_type: int16
        scan_interval: 10
        #lazy_error_count: 10
      - name:  Phase A Power on Meter
        unique_id: phase_a_power_on_meter
        slave: 252
        address: 10994
        input_type: holding
        state_class: measurement
        unit_of_measurement: kW
        device_class: power
        scale: 0.001
        precision: 3
        data_type: int32
        scan_interval: 10
        lazy_error_count: 10
      - name:  Phase B Power on Meter
        unique_id: phase_b_power_on_meter
        slave: 252
        address: 10996
        input_type: holding
        state_class: measurement
        unit_of_measurement: kW
        device_class: power
        scale: 0.001
        precision: 3
        data_type: int32
        scan_interval: 10
        lazy_error_count: 10
      - name:  Phase C Power on Meter
        unique_id: phase_c_power_on_meter
        slave: 252
        address: 10998
        input_type: holding
        state_class: measurement
        unit_of_measurement: kW
        device_class: power
        scale: 0.001
        precision: 3
        data_type: int32
        scan_interval: 10
        lazy_error_count: 10
      - name:  Total Power on Meter        #Grid power
        unique_id: total_power_on_meter
        slave: 252
        address: 11000
        input_type: holding
        state_class: measurement
        unit_of_measurement: kW
        device_class: power
        scale: 0.001
        precision: 3
        data_type: int32
        scan_interval: 10
        lazy_error_count: 10

      - name:  Grid Injection Energy on that day   #export/day
        unique_id: grid_injection_energy_on_that_day
        slave: 252
        address: 31000
        scale: 0.1
        precision: 2
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Grid Purchasing Energy on that day
        unique_id: grid_purchasing_energy_on_that_day
        slave: 252
        address: 31001
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Backup Output Energy on that day
        unique_id: backup_output_energy_on_that_day
        slave: 252
        address: 31002
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int16
        scan_interval: 300
        lazy_error_count: 10
      - name:  Battery Charge Energy on that day
        unique_id: battery_charge_energy_on_that_day
        slave: 252
        address: 31003
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Battery Discharge Energy on that day
        unique_id: battery_discharge_energy_on_that_day
        slave: 252
        address: 31004
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  PV Generation Energy on that day    #E-today from GUI
        unique_id: pv_generation_energy_on_that_day
        slave: 252
        address: 31005
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Loading Energy on that day
        unique_id: loading_energy_on_that_day
        slave: 252
        address: 31006
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int16
        scan_interval: 300
        lazy_error_count: 10
      - name:  Energy Purchased from Grid on that day  #on inverter side
        unique_id: energy_purchased_from_grid_on_that_day
        slave: 252
        address: 31008
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int16
        scan_interval: 300
        lazy_error_count: 10
      - name:  Total Energy injected to grid
        unique_id: total_energy_injected_to_grid
        slave: 252
        address: 31102
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int32
        scan_interval: 600
        lazy_error_count: 10
      - name:  Total Energy Purchased from Grid from Meter
        unique_id: total_energy_purchased_from_grid_from_meter
        slave: 252
        address: 31104
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int32
        scan_interval: 600
        lazy_error_count: 10
      - name:  Total Output Energy on backup port
        unique_id: total_output_energy_on_backup_port
        slave: 252
        address: 31106
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int32
        scan_interval: 3600
        lazy_error_count: 10
      - name:  Total Energy Charged to Battery
        unique_id: total_energy_charged_to_battery
        slave: 252
        address: 31108
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int32
        scan_interval: 600
        lazy_error_count: 10
      - name:  Total Energy Discharged from Battery
        unique_id: total_energy_discharged_from_battery
        slave: 252
        address: 31110
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int32
        scan_interval: 600
        lazy_error_count: 10
      - name:  Total PV Generation
        unique_id: total_pv_generation
        slave: 252
        address: 31112
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int32
        scan_interval: 600
        lazy_error_count: 10
      - name:  Total Loading Energy consumed at grid side  #totoal load consumption
        unique_id: total_loading_energy_consumed_at_grid_side
        slave: 252
        address: 31114
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int32
        scan_interval: 3600
        lazy_error_count: 10
      - name:  Total Energy Purchased from Grid at inverter side
        unique_id: total_energy_purchased_from_grid_at_inverter_side
        slave: 252
        address: 31118
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        data_type: int32
        scan_interval: 3600
        lazy_error_count: 10

      - name:  Total Grid-Injection Energy on Meter
        unique_id: total_grid_injection_energy_on_meter
        slave: 252
        address: 11002
        input_type: holding
        state_class: total_increasing
        unit_of_measurement: kWh
        device_class: energy
        scale: 0.01
        precision: 2
        data_type: int32
        scan_interval: 3600
        lazy_error_count: 10
      - name:  Total Purchasing Energy from Grid on Meter
        unique_id: total_purchasing_energy_from_grid_on_meter
        slave: 252
        address: 11004
        input_type: holding
        state_class: total_increasing
        unit_of_measurement: kWh
        device_class: energy
        scale: 0.01
        precision: 2
        data_type: int32
        scan_interval: 3600
        lazy_error_count: 10

      - name:  Grid Lines A/B Voltage
        unique_id: grid_lines_a_b_voltage
        slave: 252
        address: 11006
        input_type: holding
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Grid Lines B/C Voltage
        unique_id: grid_lines_b_c_voltage
        slave: 252
        address: 11007
        input_type: holding
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Grid Lines C/A Voltage
        unique_id: grid_lines_c_a_voltage
        slave: 252
        address: 11008
        input_type: holding
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        scale: 0.1
        precision: 1
        data_type: int16        
        scan_interval: 60
        lazy_error_count: 10
      - name:  Grid Phase A Voltage
        unique_id: grid_phase_a_voltage
        slave: 252
        address: 11009
        input_type: holding
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Grid Phase B Voltage
        unique_id: grid_phase_b_voltage
        slave: 252
        address: 11011
        input_type: holding
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Grid Phase C Voltage
        unique_id: grid_pase_c_voltage
        slave: 252
        address: 11013
        input_type: holding
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Grid Phase A Current
        unique_id: grid_phase_a_current
        slave: 252
        address: 11010
        input_type: holding
        state_class: measurement
        unit_of_measurement: A
        device_class: current
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Grid Phase B Current
        unique_id: grid_phase_b_current
        slave: 252
        address: 11012
        input_type: holding
        state_class: measurement
        unit_of_measurement: A
        device_class: current
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Grid Phase C Current
        unique_id: grid_phase_c_current
        slave: 252
        address: 11014
        input_type: holding
        state_class: measurement
        unit_of_measurement: A
        device_class: current
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  P AC
        unique_id: p_ac
        slave: 252
        address: 11016
        input_type: holding
        state_class: measurement
        unit_of_measurement: kW
        device_class: power
        scale: 0.001
        precision: 3
        data_type: int32
        scan_interval: 10
        lazy_error_count: 10
      - name:  Total PV Generation on that day  #energy today
        unique_id: total_pv_generation_on_that_day
        slave: 252
        address: 11018
        input_type: holding
        state_class: total_increasing
        device_class: energy
        unit_of_measurement: kWh
        scale: 0.1
        precision: 1
        data_type: int32
        scan_interval: 1800
        lazy_error_count: 10
      - name:  Total PV Generation from Installation  #energy total
        unique_id: total_pv_generation_from_installation
        slave: 252
        address: 11020
        input_type: holding
        state_class: total_increasing
        device_class: energy
        unit_of_measurement: kWh
        scale: 0.1
        precision: 1
        data_type: int32
        scan_interval: 3600
        lazy_error_count: 10
      - name:  PV Input Total Power        #PV power, panel actual power
        unique_id: pv_input_total_power
        slave: 252
        address: 11028
        input_type: holding
        state_class: measurement
        device_class: energy
        unit_of_measurement: kWh
        scale: 0.001
        precision: 3
        data_type: int32
        scan_interval: 10
        lazy_error_count: 10
      - name:  Temperature Sensor 1
        unique_id: temperature_sensor_1
        slave: 252
        address: 11032
        input_type: holding
        state_class: measurement
        unit_of_measurement: °C
        device_class: temperature
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 600
        lazy_error_count: 10
      - name:  Temperature Sensor 2
        unique_id: temperature_sensor_2
        slave: 252
        address: 11033
        input_type: holding
        state_class: measurement
        unit_of_measurement: °C
        device_class: temperature
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 600
        lazy_error_count: 10
      - name:  Temperature Sensor 3
        unique_id: temperature_sensor_3
        slave: 252
        address: 11034
        input_type: holding
        state_class: measurement
        unit_of_measurement: °C
        device_class: temperature
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 600
        lazy_error_count: 10
      - name:  Temperature Sensor 4
        unique_id: temperature_sensor_4
        slave: 252
        address: 11035
        input_type: holding
        state_class: measurement
        unit_of_measurement: °C
        device_class: temperature
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 600
        lazy_error_count: 10
      - name:  PV1 Voltage
        unique_id: pv1_voltage
        slave: 252
        address: 11038
        input_type: holding
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 300
        lazy_error_count: 10
      - name:  PV2 Voltage
        unique_id: pv2_voltage
        slave: 252
        address: 11040
        input_type: holding
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 300
        lazy_error_count: 10
      - name:  PV1 Current
        unique_id: pv1_current
        slave: 252
        address: 11039
        input_type: holding
        state_class: measurement
        unit_of_measurement: A
        device_class: current
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 300
        lazy_error_count: 10
      - name:  PV2 Current
        unique_id: pv2_current
        slave: 252
        address: 11041
        input_type: holding
        state_class: measurement
        unit_of_measurement: A
        device_class: current
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 300
        lazy_error_count: 10
      - name:  PV1 Input Power
        unique_id: pv1_input_power
        slave: 252
        address: 11062
        input_type: holding
        state_class: measurement
        unit_of_measurement: kW
        device_class: power
        scale: 0.001
        precision: 3
        data_type: int32
        scan_interval: 10
        lazy_error_count: 10
      - name:  PV2 Input Power
        unique_id: pv2_input_power
        slave: 252
        address: 11064
        input_type: holding
        state_class: measurement
        unit_of_measurement: kW
        device_class: power
        scale: 0.001
        precision: 3
        data_type: int32
        scan_interval: 10
        lazy_error_count: 10
      - name:  ARM Fault Flag1 #Table 3.3 Troubleshooting
        unique_id: arm_fault_flag1
        slave: 252
        address: 18000
        input_type: holding
        data_type: int32
        scan_interval: 60
        lazy_error_count: 10
      - name:  Fault Flag1 #Table 3.3 Troubleshooting
        unique_id: fault_flag1
        slave: 252
        address: 10112
        input_type: holding
        data_type: int32
        scan_interval: 60
        lazy_error_count: 10
      - name:  Fault Flag2 #Table 3.3 Troubleshooting
        unique_id: fault_flag2
        slave: 252
        address: 10114
        input_type: holding
        data_type: int32
        scan_interval: 60
        lazy_error_count: 10
      - name:  Fault Flag3 #Table 3.3 Troubleshooting
        unique_id: fault_flag3
        slave: 252
        address: 10116
        input_type: holding
        data_type: int32
        scan_interval: 60
        lazy_error_count: 10
        
      - name:  Total Backup P          #load power, total active power
        unique_id: total_backup_p
        slave: 252
        address: 30230
        input_type: holding
        state_class: measurement
        unit_of_measurement: kW
        device_class: power
        scale: 0.001
        precision: 3
        data_type: int32
        scan_interval: 10
        lazy_error_count: 10

      - name:  Battery V #DC Voltage
        unique_id: battery_v
        slave: 252
        address: 30254
        input_type: holding
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 300
        lazy_error_count: 10
      - name:  Battery I #DC Current
        unique_id: battery_i
        slave: 252
        address: 30255
        input_type: holding
        state_class: measurement
        unit_of_measurement: A
        device_class: current
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 300
        lazy_error_count: 10
      - name:  Battery P           #Battery Power
        unique_id: battery_p
        slave: 252
        address: 30258
        input_type: holding
        state_class: measurement
        unit_of_measurement: kW
        device_class: power
        scale: 0.001
        precision: 3
        data_type: int32
        scan_interval: 10
        lazy_error_count: 10
      - name:  Battery Mode #0:discharge;1:charge
        unique_id: battery_mode
        slave: 252
        address: 30256
        input_type: holding
        data_type: int16
        scan_interval: 10
        lazy_error_count: 10
#BMS registers
      - name:  BMS Software Version
        unique_id: bms_software_version
        slave: 252
        address: 32003
        input_type: holding
        data_type: int16
        scan_interval: 600
        lazy_error_count: 10
      - name:  BMS Hardware Version
        unique_id: bms_hardware_version
        slave: 252
        address: 32004
        input_type: holding
        data_type: int16
        scan_interval: 600
        lazy_error_count: 10
      - name:  Battery SOC
        unique_id: battery_soc
        slave: 252
        address: 33000
        input_type: holding
        state_class: measurement
        unit_of_measurement: '%'
        device_class: battery
        scale: 0.01
        precision: 2
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Battery SOH
        unique_id: battery_soh
        slave: 252
        address: 33001
        input_type: holding
        state_class: measurement
        unit_of_measurement: '%'
        device_class: battery
        scale: 0.01
        precision: 2
        data_type: int16
        scan_interval: 3600
        lazy_error_count: 10
      - name:  BMS Status
        unique_id: bms_status
        slave: 252
        address: 33002
        input_type: holding
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  BMS Pack Temperature
        unique_id: bms_pack_temperature
        slave: 252
        address: 33003
        input_type: holding
        state_class: measurement
        unit_of_measurement: °C
        device_class: temperature
        scale: 0.1
        precision: 1
        data_type: int16
        scan_interval: 300
        lazy_error_count: 10
      - name:  BMS Error Code
        unique_id: bms_error_code
        slave: 252
        address: 33016
        input_type: holding
        data_type: int32
        scan_interval: 60
        lazy_error_count: 10
      - name:  BMS Warn Code
        unique_id: bms_warn_code
        slave: 252
        address: 33018
        input_type: holding
        data_type: int32
        scan_interval: 60
        lazy_error_count: 10

#RW values
      - name:  Hybrid Inverter Working Mode Setting  #257=General mode; 258=Economic mode; 259=UPS mode; 512=Off grid mode
        unique_id: hybrid_inverter_working_mode_setting #RW
        slave: 252
        address: 50000
        input_type: holding
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10

      - name:  Grid Injection Power Limit Setting  #[0.0%-100.0%]
        unique_id: grid_injection_power_limit_setting #RW
        slave: 252
        address: 25103
        input_type: holding
        scale: 0.1    #spec: 1000
        precision: 1
        data_type: int16
        scan_interval: 110
        lazy_error_count: 10
      - name:  On-grid Battery End SOC  #[0.0%-100.0%]
        unique_id: on_grid_battery_end_soc #RW
        slave: 252
        address: 52503
        input_type: holding
        scale: 0.1    #spec: 1000
        precision: 1
        data_type: int16
        scan_interval: 360
        lazy_error_count: 10
      - name:  Off-grid Battery End SOC  #[0.0%-100.0%]
        unique_id: off_grid_battery_end_soc #RW
        slave: 252
        address: 52505
        input_type: holding
        scale: 0.1    #spec: 1000
        precision: 1
        data_type: int16
        scan_interval: 360
        lazy_error_count: 10
      - name:  RW BMS Status #Table 3.8
        unique_id: rw_bms_status
        slave: 252
        address: 53508
        input_type: holding
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  RW BMS ErrorCode #Table 3.9
        unique_id: rw_bms_errorcode
        slave: 252
        address: 53509
        input_type: holding
        data_type: int32
        scan_interval: 60
        lazy_error_count: 10
      - name:  RW BMS ProtectionCode #Table 3.9
        unique_id: rw_bms_protectioncode
        slave: 252
        address: 53511
        input_type: holding
        data_type: int32
        scan_interval: 60
        lazy_error_count: 10
      - name:  RW BMS WarnCode #Table 3.9
        unique_id: rw_bms_warncode
        slave: 252
        address: 53513
        input_type: holding
        data_type: int32
        scan_interval: 60
        lazy_error_count: 10

      - name:  Ecomode Period Enable Flag #bit0- bit5 stands for period1-period6, bit7-bit15 reserved; 0: disable 1: enable
      #1=one line; #3=2 lines; 7=4lines; 15=5lines; 31=6lines
        unique_id: ecomode_period_enable_flag
        slave: 252
        address: 53006
        input_type: holding
        data_type: int16
        scan_interval: 60
        lazy_error_count: 10
      - name:  Ecomode Period1 Struct
        unique_id: ecomode_period1_struct
        slave: 252
        address: 53007
        input_type: holding
        count: 7
        data_type: custom
        structure: ">HHHHHHH"
        scan_interval: 60
        lazy_error_count: 10
      - name:  Ecomode Period2 Struct
        unique_id: ecomode_period2_struct
        slave: 252
        address: 53014
        input_type: holding
        count: 7
        data_type: custom
        structure: ">HHHHHHH"
        scan_interval: 60
        lazy_error_count: 10
#how to write data:
#service: modbus.write_register
#data:
#  hub: 
#  address: 53007
#  slave: 252
#  value:
#    - 2
#    - 65535
#    - 65535
#    - 800
#    - 65535
#    - 3
#    - 5
#or value: [2,65535,65535,800,65535,3,5]

sensor:
  - platform: template
    sensors:
      battery_mode_text:
         friendly_name:  Battery Mode
         unique_id: battery_mode_text
         value_template: >
            {% if states('sensor.battery_mode') == '0' %}
                discharge
            {% else %}
                charge
            {% endif %}
      hybrid_inverter_working_mode_setting_text:
         friendly_name:  Hybrid Inverter Working Mode
         value_template: >
            {% if states('sensor.hybrid_inverter_working_mode_setting') == '257' %}
                General mode
            {% elif states('sensor.hybrid_inverter_working_mode_setting') == '258' %}
                Economic mode
            {% elif states('sensor.hybrid_inverter_working_mode_setting') == '259' %}
                UPS mode
            {% elif states('sensor.hybrid_inverter_working_mode_setting') == '512' %}
                Offgrid mode
            {% endif %}
      inverter_running_status_text:
         friendly_name:  Inverter Running Status
         value_template: >
            {% if states('sensor.inverter_running_status') == '0' %}
                wait
            {% elif states('sensor.inverter_running_status') == '1' %}
                check
            {% elif states('sensor.inverter_running_status') == '2' %}
                On Grid
            {% elif states('sensor.inverter_running_status') == '3' %}
                fault
            {% elif states('sensor.inverter_running_status') == '4' %}
                flash
            {% elif states('sensor.inverter_running_status') == '5' %}
                Off Grid
            {% endif %}
      battery_soc_eta:
         friendly_name:  Battery SOC ETA
         unique_id: battery_soc_eta
         value_template: >
            {% set battery_SOC = float(states("sensor.battery_soc"), default=100) %}
            {% set battery_SOC_last = float(states("sensor.battery_soc_5min_state"), default=0) %}
            {% set timediff = (states.sensor.battery_soc.last_updated.timestamp() - as_datetime(states("sensor.battery_soc_5min_last_updated")).timestamp())/60 %}
            {% set percdiff = battery_SOC - battery_SOC_last %}
            {% if battery_SOC>99: %}
                full
            {% elif percdiff==0 or timediff==0: %}
                {{ states('sensor.battery_soc_eta') }}
            {% elif battery_SOC<=20: %}
                almost dicharged
            {% elif percdiff<=0: %}
                {% set percmissing = battery_SOC-20 %}
                {% set timemissing = (percmissing / (percdiff/timediff))|round  %}
                ↓{{ as_local(as_datetime(as_timestamp(utcnow()) - timemissing*60)).strftime("%Y-%m-%d %H:%M") }}↓
            {% else %}
                {% set percmissing = 100-battery_SOC %}
                {% set timemissing = (percmissing / (percdiff/timediff))|round  %}
                ↑{{ as_local(as_datetime(as_timestamp(utcnow()) + timemissing*60)).strftime("%Y-%M-%d %H:%M") }}↑
            {% endif %}
      grid_injection_energy_on_that_day_15min:   #sell in last 15min
         friendly_name:  Grid Injection 15min
         unique_id: grid_injection_energy_on_that_day_15min
         unit_of_measurement: kW
         device_class: power
         value_template: >
            {% set diff = (float(states("sensor.grid_injection_energy_on_that_day"), default=0) - float(states("sensor.grid_injection_energy_on_that_day_15min_state"), default=0) ) %}
            {% set timediff = (states.sensor.grid_injection_energy_on_that_day.last_updated.timestamp() - as_datetime(states("sensor.grid_injection_energy_on_that_day_15min_last_updated")).timestamp())/60 %}
            {% if timediff==0 or diff<=0: %}
                {{ float(0) }}
            {% else %}
                {% set res = (diff/timediff*60) %}
                {% if res>15: %}
                    {{ float(0) }}
                {% else %}
                    {{ res| round(1) }}
                {% endif %}
            {% endif %}
         attribute_templates:
           diff: >
            {% set diff = (float(states("sensor.grid_injection_energy_on_that_day"), default=0) - float(states("sensor.grid_injection_energy_on_that_day_15min_state"), default=0) ) %}
            {{ diff }}
           timediff: >
            {% set timediff = (states.sensor.grid_injection_energy_on_that_day.last_updated.timestamp() - as_datetime(states("sensor.grid_injection_energy_on_that_day_15min_last_updated")).timestamp())/60 %}
            {{ timediff }}           
      home_consumption_now:
         friendly_name:  Home Consumption Now
         unique_id: home_consumption_now
         unit_of_measurement: kW
         device_class: power
         value_template: >
            {% set diff = (float(states("sensor.p_ac"), default=0) 
                         - float(states("sensor.total_power_on_meter"), default=0)) | round(3) %}
            {% if diff>0: %}
                {{ diff }}
            {% else %}
                {{ (float(states("sensor.home_consumption_now"), default=0)) }}
            {% endif %}
         
      home_consumption_today:
         friendly_name:  Home Consumption Today
         unique_id: home_consumption_today
         unit_of_measurement: kWh
         device_class: energy
         value_template: '{{ float(states(''sensor.loading_energy_on_that_day''), default= float(states(''sensor.home_consumption_today''), default=-10)) 
                         + float(states(''sensor.backup_output_energy_on_that_day''), default=0) }}'
      home_consumption_today2:
         friendly_name:  Home Consumption Today2
         unique_id: home_consumption_today2
         unit_of_measurement: kWh
         device_class: energy
         value_template: '{{ (float(states(''sensor.pv_generation_energy_on_that_day''), default= float(states(''sensor.home_consumption_today2''), default=-10)) 
                         + float(states(''sensor.grid_purchasing_energy_on_that_day''), default=0)
                         - float(states(''sensor.grid_injection_energy_on_that_day''), default=0)
                         - float(states(''sensor.battery_charge_energy_on_that_day''), default=0) 
                         + float(states(''sensor.battery_discharge_energy_on_that_day''), default=0)) | round(3) }}'

      battery_p_w:
         friendly_name:  Battery P Watt
         unique_id: battery_p_w
         unit_of_measurement: W
         device_class: power
         value_template: '{{ (states(''sensor.battery_p'')|float(0)*1000)|round(0) }}'
      total_power_on_meter_w:
         friendly_name:  Total Power On Meter Watt
         unique_id: total_power_on_meter_w
         unit_of_measurement: W
         device_class: power
         value_template: '{{ (states(''sensor.total_power_on_meter'')|float(0)*-1000)|round(0) }}'
      pv1_input_power_w:
         friendly_name:  PV1 Input Power Watt
         unique_id: pv1_input_power_w
         unit_of_measurement: W
         device_class: power
         value_template: '{{ (states(''sensor.pv1_input_power'')|float(0)*1000)|round(0) }}'
      pv2_input_power_w:
         friendly_name:  PV2 Input Power Watt
         unique_id: pv2_input_power_w
         unit_of_measurement: W
         device_class: power
         value_template: '{{ (states(''sensor.pv2_input_power'')|float(0)*1000)|round(0) }}'
      home_consumption_now_w:
         friendly_name:  Home Consumption Now Watt
         unique_id: home_consumption_now_w
         unit_of_measurement: W
         device_class: power
         value_template: '{{ (states(''sensor.home_consumption_now'')|float(0)*1000)|round(0) }}'
      phase_a_power_on_meter_w:
         friendly_name:  Phase A Power on Meter Watt
         unique_id: phase_a_power_on_meter_w
         unit_of_measurement: W
         device_class: power
         value_template: '{{ (states(''sensor.phase_a_power_on_meter'')|float(0)*1000)|round(0) }}'
      phase_b_power_on_meter_w:
         friendly_name:  Phase B Power on Meter Watt
         unique_id: phase_b_power_on_meter_w
         unit_of_measurement: W
         device_class: power
         value_template: '{{ (states(''sensor.phase_b_power_on_meter'')|float(0)*1000)|round(0) }}'
      phase_c_power_on_meter_w:
         friendly_name:  Phase C Power on Meter Watt
         unique_id: phase_c_power_on_meter_w
         unit_of_measurement: W
         device_class: power
         value_template: '{{ (states(''sensor.phase_c_power_on_meter'')|float(0)*1000)|round(0) }}'
      sunsync_grid_connected:
         friendly_name:  Sunsync Grid Connected
         unique_id: sunsync_grid_connected
         value_template: >
           {% if states('sensor.inverter_running_status') == '2' %} 
              1
           {% else %}
              0
           {% endif %}

#jeden riadok 1 - 2,65535,65535,860,65535,1,7; cas je 00:01-00:07
#druhy riadok 3 - 1,0,65535,235,65535,5915,5937; cas je 23:27-23:49; 23.5%
#druhy riadok 3 - 2,65535,65535,860,65535,0,256; cas je 00:00-01:00
#druhy riadok 3 - 2,65535,65535,860,65535,512,768; cas je 02:00-03:00
#
      ecomode_period1_chargedischarge_setting:
         friendly_name:  Ecomode Period1 ChargeDischarge Setting #Period1: 0:NONE 1:charge 2:discharge
         unique_id: ecomode_period1_chargedischarge_setting
         value_template: '{{ states(''sensor.ecomode_period1_struct'').split('','')[0]|default(-1) }}'
      ecomode_period1_battery_charge_by:   
         friendly_name:  Ecomode Period1 Battery Charge By #0:PV 1:PV+GRID
         unique_id: ecomode_period1_battery_charge_by
         value_template: '{{ states(''sensor.ecomode_period1_struct'').split('','')[1]|default(-1) }}'
      ecomode_period1_power_limit:   
         friendly_name:  Ecomode Period1 Power Limit #[0.0-100.0%]
         unique_id: ecomode_period1_power_limit
         unit_of_measurement: '%'
         value_template: '{{ states(''sensor.ecomode_period1_struct'').split('','')[3]|float(0)/10|default(-1) }}'
      ecomode_period1_start_time:
         friendly_name:  Ecomode Period1 Start Time #High 8bits(Hour):[0,23] Low 8bits(Mins):[0,59]
         unique_id: ecomode_period1_start_time
         value_template: '{{ ((states(''sensor.ecomode_period1_struct'').split('','')[5]|int//256)|string).zfill(2) }}:{{ ((states(''sensor.ecomode_period1_struct'').split('','')[5]|int%256)|string).zfill(2) }}'
      ecomode_period1_end_time:
         friendly_name:  Ecomode Period1 End Time #High 8bits(Hour):[0,23] Low 8bits(Mins):[0,59]
         unique_id: ecomode_period1_end_time
         value_template: '{{ ((states(''sensor.ecomode_period1_struct'').split('','')[6]|int//256)|string).zfill(2) }}:{{ ((states(''sensor.ecomode_period1_struct'').split('','')[6]|int%256)|string).zfill(2) }}'
      ecomode_period_enable_flag_bin:
         friendly_name:  Ecomode Period Enable Flag Binary
         unique_id: ecomode_period_enable_flag_bin
         value_template: '{{ (''{:b}''.format(states(''sensor.ecomode_period_enable_flag'')|int)).ljust(6, ''0'') }}'


#Normal help
#PV power is sufficient, power from the PV will firstly supply loads, then excess power charge battery, 
#and any surplus power will be fed to the grid. 
#PV power is insufficient to satisfy loads, the battery will discharge power to fill the power gap, 
#and the grid will join in if it’s still not enough.

#Economic help
#This mode is typically used in areas where with peak and valley electricity prices to assist clients in optimizing their energy costs. 
#By configuring the App, customers can charge power from the grid during valley hours. 
#Customers can also discharge power during peak hours by configuring the App, 
#and the battery will discharge electricity to supply loads or feed to the grid in this instance.
#standardne bez casov vobec nepouziva baterku / nenabija/nevybija

#Off-grid mode
#In the pure off-grid mode, power from PV will supply the back-up loads first and then charge the battery if there's surplus power. 
#When the power from PV isn’t enough, the battery will discharge to supply back-up loads together with PV. 
#Note: If there’s no grid connection when configuring the hybrid system, please set the work mode to “Off-grid” to enable 
#the power output in back-up port and switch to other modes when grid comes back.

# UPS mode
# Power grid is connected, power from PV or grid will firstly charge the battery until it’s full, 
# and loads will be supplied by the grid during charging period. Battery will not discharge power when the grid is connected. 
# Power grid fails, and PV power is insufficient to meet the loads' consumption, the battery will discharge power to supply 
# loads connected to the back-up port.

# Minimal import from grid alghoritm
#aim is not to buy energy for the inverter when not needed. Inverter is switched to Off-grid mode when the battery SOC is between 30-99
#inverter takes cca 1,7kWh/day or 70W/h
#usefull when household consumption is low and battery storage can last for whole night
#you can save ~600Wh-1100Wh/day
#by default is this script disabled (should be)
#preconditions, not checked in alghoritm:
# none
#turn on Off-grid mode => battery charges from PV, energy to household goes from PV and battery
#- *after sunset 
#-  or *solar production=0 for 1h - todo
#- solar forecast for next day > 5kWh - todo
#- battery SOC > 60
#- is General mode
#turn on General mode => when battery full, energy goes also to grid
#- is not General mode
#- *battery is below 30%   -> we will need grid
#- *battery is below 25%   -> just for sure, critical event
#- *battery is over 99%    -> we can export to grid
#* = event, withou asterix are conditions
automation:
- id: '1679907434474'
  alias:  Minimal import from grid set General Mode
  description: turn on General mode => when battery full, energy goes also to grid
  trigger:
  - platform: numeric_state
    entity_id: sensor.battery_soc
    above: 99.8
  - platform: numeric_state
    entity_id: sensor.battery_soc
    below: 30
  - platform: numeric_state
    entity_id: sensor.battery_soc
    below: 25
  condition:
  - condition: not
    conditions:
    - condition: state
      entity_id: sensor.hybrid_inverter_working_mode_setting_text
      state: General mode
  action:
  - service: script.mode_general
    data: {}
    enabled: false
  mode: single
- id: '1679935088587'
  alias:  Minimal import from grid set Off-grid Mode
  description: ''
  trigger:
  - platform: sun
    event: sunset
    offset: -01:00:00
  condition:
  - condition: numeric_state
    entity_id: sensor.battery_soc
    above: 60
  - condition: state
    entity_id: sensor.hybrid_inverter_working_mode_setting_text
    state: General mode
  action:
  - service: script.mode_offgrid
    data: {}
    enabled: false
  mode: single

script:
  mode_ups:
    alias: mode_ups
    sequence:
    - service: modbus.write_register
      data:
        address: 50000
        hub: 
        slave: 252
        value: 259
    mode: single
    icon: mdi:alpha-w-box
  mode_economic:
    alias: mode_economic
    sequence:
    - service: modbus.write_register
      data:
        address: 50000
        hub: 
        slave: 252
        value: 258
    mode: single
    icon: mdi:alpha-w-box
  mode_general:
    alias: mode_general
    sequence:
    - service: modbus.write_register
      data:
        address: 50000
        hub: 
        slave: 252
        value: 257
    mode: single
    icon: mdi:alpha-w-box
  mode_offgrid:
    alias: mode_offgrid
    sequence:
    - service: modbus.write_register
      data:
        address: 50000
        hub: 
        slave: 252
        value: 512
    mode: single
    icon: mdi:alpha-w-box
  # set_grid_injection_power_limit_switch:
    # alias:  Set Grid Injection Power Limit Switch
    # sequence:
    # - service: modbus.write_register
      # data:
        # address: 25100
        # hub: 
        # slave: 252
        # value: '{{ switch }}'
    # mode: single
    # icon: mdi:alpha-w-box
    
  set_grid_injection_power_limit_setting:
    alias:  Set Grid Injection Power Limit Setting
    sequence:
    - service: modbus.write_register
      data:
        address: 25103
        hub: 
        slave: 252
        value: '{{ float(limit)*10 }}'
    mode: single
    icon: mdi:alpha-w-box

  charge_battery_from_grid:
    alias: charge_battery_from_grid
    sequence:
    - service: modbus.write_register
      data:
        hub: 
        slave: 252
        address: 53006
        value: 1
    - service: modbus.write_register
      data:
        hub: 
        slave: 252
        address: 53007
        value:
        - 1
        - 1
        - 65535
        - 1000
        - 65535
        - 0
        - 5947
    - service: script.mode_economic
      data: {}
    - wait_for_trigger:
      - platform: state
        entity_id:
        - sensor.hybrid_inverter_working_mode_setting
        to: '258'
      timeout:
        hours: 0
        minutes: 10
        seconds: 0
        milliseconds: 0
    - if:
      - condition: state
        entity_id: sensor.battery_mode   #0:discharge;1:charge
        state: '0'
      then:
      - wait_for_trigger:
        - platform: state
          entity_id:
          - sensor.battery_mode
          to: '1'
        timeout:
          hours: 0
          minutes: 10
          seconds: 0
          milliseconds: 0
    - wait_for_trigger:
      - platform: state
        entity_id:
        - sensor.battery_mode
        to: '0'
      timeout:
        hours: 3
        minutes: 0
        seconds: 0
        milliseconds: 0
    - service: script.mode_general
      data: {}
    mode: single
 
