How to power the Lynx distributor and read the fuse states with esphome

Yes, but that is Not the Problem. The problem is Not that I can‘t read the State of the second (B).

For now, I only want to read the state of A (0x08). That work only If I have A connectet alone. If I have A and B connected it fails.

Good News, with two 10k pullup resistors between the gpios and Vbus it works

Now I have to rewrite the code to read both, A and B

Updated version for 2 distributor with DIP Switches set to A and B:

#https://www.wemos.cc/en/latest/s2/s2_mini.html
#https://github.com/twam/dbus-lynx-distributor
#https://community.victronenergy.com/t/how-to-power-the-lynx-distributor-and-read-the-fuse-states-with-esphome/3534

#rj10
# pin 1 yellow = VBUS
# pin 2 green  = SDA = GPIO33
# pin 3 red    = SCL = GPIO35
# pin 4 black  = GND

esphome:
  name: lynx
  comment: Victron distributor status

esp32:
  board: lolin_s2_mini
  variant: ESP32S2
  framework:
    type: esp-idf

logger:
  level: INFO
  hardware_uart: USB_CDC

debug:
  update_interval: 120s

api:

ota:
  platform: esphome

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  output_power: 12 #Formerly: 8.5

  ap:
    ssid: Lynxfallback
    password: !secret wifi_password
captive_portal:

light:
  - platform: status_led
    name: "Status LED"
    id: lynx_status_led
    icon: "mdi:alarm-light"
    restore_mode: ALWAYS_OFF
    pin:
      number: GPIO15
      inverted: false

i2c:
  - id: bus_a
    sda: GPIO33
    scl: GPIO35
    frequency: 50kHz
    scan: true

#i2c_device:
#  id: lynx_a
#  address: 0x08
#  #Lynx A = 0x08
#  #Lynx B = 0x09
#  #Lynx C = 0x10
#  #Lynx D = 0x11

time:
  - platform: homeassistant
    id: the_time
    on_time:
      seconds: /30
      then:
        - lambda: |-
            uint8_t data;
            esphome::i2c::I2CDevice dev;
            dev.set_i2c_bus(bus_a);
            dev.set_i2c_address(0x08);
            auto err = dev.read(&data, 1);
            if (err == esphome::i2c::ERROR_OK) {
              ESP_LOGI("lynx", "Read %x=%x", 0x08, data);
              id(lynx_a_busbar_no_supply).publish_state((data & 0x02) != 0);
              id(lynx_a_fuse_1_broken).publish_state((data & 0x10) != 0);
              id(lynx_a_fuse_2_broken).publish_state((data & 0x20) != 0);
              id(lynx_a_fuse_3_broken).publish_state((data & 0x40) != 0);
              id(lynx_a_fuse_4_broken).publish_state((data & 0x80) != 0);
            } else
              ESP_LOGE("lynx", "Read failed");

  - platform: homeassistant
    id: the_timeb
    on_time:
      seconds: /30
      then:
        - lambda: |-
            uint8_t data;
            esphome::i2c::I2CDevice dev;
            dev.set_i2c_bus(bus_a);
            dev.set_i2c_address(0x09);
            auto err = dev.read(&data, 1);
            if (err == esphome::i2c::ERROR_OK) {
              ESP_LOGI("lynx", "Read %x=%x", 0x09, data);
              id(lynx_b_busbar_no_supply).publish_state((data & 0x02) != 0);
              id(lynx_b_fuse_1_broken).publish_state((data & 0x10) != 0);
              id(lynx_b_fuse_2_broken).publish_state((data & 0x20) != 0);
              id(lynx_b_fuse_3_broken).publish_state((data & 0x40) != 0);
              id(lynx_b_fuse_4_broken).publish_state((data & 0x80) != 0);
            } else
              ESP_LOGE("lynx", "Read failed");


binary_sensor:
  - platform: template
    name: "lynx A busbar no supply"
    id: lynx_a_busbar_no_supply
    icon: mdi:flash-alert

  - platform: template
    name: "lynx A fuse 1 broken"
    id: lynx_a_fuse_1_broken
    icon: mdi:fuse-alert

  - platform: template
    name: "lynx A fuse 2 broken"
    id: lynx_a_fuse_2_broken
    icon: mdi:fuse-alert

  - platform: template
    name: "lynx A fuse 3 broken"
    id: lynx_a_fuse_3_broken
    icon: mdi:fuse-alert

  - platform: template
    name: "lynx A fuse 4 broken"
    id: lynx_a_fuse_4_broken
    icon: mdi:fuse-alert

  - platform: template
    name: "lynx B busbar no supply"
    id: lynx_b_busbar_no_supply
    icon: mdi:flash-alert

  - platform: template
    name: "lynx B fuse 1 broken"
    id: lynx_b_fuse_1_broken
    icon: mdi:fuse-alert

  - platform: template
    name: "lynx B fuse 2 broken"
    id: lynx_b_fuse_2_broken
    icon: mdi:fuse-alert

  - platform: template
    name: "lynx B fuse 3 broken"
    id: lynx_b_fuse_3_broken
    icon: mdi:fuse-alert

  - platform: template
    name: "lynx B fuse 4 broken"
    id: lynx_b_fuse_4_broken
    icon: mdi:fuse-alert

  - platform: gpio
    pin:
      number: GPIO0
      inverted: true
      mode:
        input: true
        pullup: true
    filters:
      - delayed_on_off: 100ms
    name: "lynx button 0"
    on_click:
    - logger.log: "Button 0 pressed"
    - light.turn_on: lynx_status_led
    - delay: 100ms
    - light.turn_off: lynx_status_led

sensor:
  - platform: wifi_signal
    name: "lynx Wi-Fi Signal"
    update_interval: 120s

  - platform: internal_temperature
    name: "lynx tcore"
    update_interval: 300s

  - platform: uptime
    type: seconds
    name: "lynx Uptime"
    update_interval: 300s

text_sensor:
  - platform: version
    name: "lynx ESPHome Version"
    hide_timestamp: true

  - platform: debug
    device:
      name: "lynx Device Info"
      id: lynx_device_info
    reset_reason:
      name: "lynx Reset Reason"
      id: lynx_reset_reason