freakent / dbus-mqtt-devices

A driver for Victron VenusOS GX devices to support use of dbus-mqtt/flashmq-mqtt
MIT License
111 stars 24 forks source link

sending data from ESP32 to cerbo gx #39

Closed sheldonadjah closed 1 year ago

sheldonadjah commented 1 year ago

Hi there, I'm working on a solar project which its battery voltage is 384VDC. i will design a esp32 board to monitor the battery voltage, power from inverter, and also start and stop generator when needed. but i will like to remotely monitor the esp32 through a cerbo gx. can you assist me ?

stundenblume commented 1 year ago

Hi, you can use this driver to create a new battery and write data via MQTT to that "battery device". The battery will be shown as if you use a Victron supported battery. Sebastian

mtucker502 commented 1 year ago

Hi there, I'm working on a solar project which its battery voltage is 384VDC. i will design a esp32 board to monitor the battery voltage, power from inverter, and also start and stop generator when needed. but i will like to remotely monitor the esp32 through a cerbo gx. can you assist me ?

Are you using anything such as esphome on the esp32?

stundenblume commented 1 year ago

Yes, I use Tasmota. There I can provide some help.

mtucker502 commented 1 year ago

I use esphome and hopefully this config will help you get pointed in the right direction.

Special thanks to @ssieb who helped me get this working.

web_server:
  port: 80
  include_internal: true
  local: true
  version: 2
  id: webserver

substitutions:
# Change the disp_name to something you want  
  disp_name: Cottage
# Interval of how often the power is updated
  update_time: 10s
# Current transformer calibrations:
  current_cal: '24900'   # self calibrated
# AC Transformer voltage calibration 9VAC Jameco 157041: 38302
# For meter <= v1.3:
#         42080 - 9v AC Transformer - Jameco 112336
#         32428 - 12v AC Transformer - Jameco 167151
# For meter > v1.4:
#         37106 - 9v AC Transformer - Jameco 157041
#         38302 - 9v AC Transformer - Jameco 112336
#         29462 - 12v AC Transformer - Jameco 167151
# For Meters >= v1.4 rev.3
#         3920 - 9v AC Transformer - Jameco 157041

# For Meters >= v1.4 rev3: 3920
  # voltage_cal: '38302' # reports 655.3V
  # voltage_cal: '7014'  # reports 211.1V -- should change to 3987.115111321648508
  voltage_cal: '3953'
  client_id: 'ctsensor'
  sensor_name: 'L1'

globals:
  - id: vrm_portal_id
    type: std::string
    restore_value: no
    initial_value: ''
  - id: sensor_id
    type: int
    restore_value: no
    initial_value: '0'

esphome:
  name: cottage-ct-main

esp32:
  board: nodemcu-32s
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret api_password
  encryption:
    key: !secret api_key
ota:
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  use_address: 10.52.84.100

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${disp_name} Fallback Hotspot"
    password: !secret ap_password

captive_portal:

spi:
  clk_pin: 18
  miso_pin: 19
  mosi_pin: 23

sensor:
  - platform: wifi_signal
    name: ${disp_name} WiFi Signal
    id: wifi_signal_strength
    update_interval: 60s

  - platform: atm90e32
    cs_pin: 5
    current_phases: 2
    phase_a:
      voltage:
        name: ${disp_name} Volts
        id: "ct1volts"
        accuracy_decimals: 1
        on_value:
          - mqtt.publish_json:
              # topic: W/${vrm_portal_id}/grid/${sensor_id}/Ac/L1/Voltage
              topic: !lambda return str_sprintf("W/%s/grid/%d/Ac/L1/Voltage", id(vrm_portal_id).c_str(), id(sensor_id));
              payload: |-
                root["value"] = id(ct1volts).state;
          - mqtt.publish_json:
              # topic: W/${vrm_portal_id}/grid/${sensor_id}/Ac/L2/Voltage
              topic: !lambda return str_sprintf("W/%s/grid/%d/Ac/L2/Voltage", id(vrm_portal_id).c_str(), id(sensor_id));
              payload: |-
                root["value"] = id(ct1volts).state;

      current:
        name: ${disp_name} CT1 Amps
        id: "ct1Amps"
# The max value for current that the meter can output is 65.535. If you expect to measure current over 65A, 
# divide the gain_ct by 2 (120A CT) or 4 (200A CT) and multiply the current and power values by 2 or 4 by uncommenting the filter below
        # filters:
        #   - multiply: 4
        on_value:
          - mqtt.publish_json:
              # topic: W/${vrm_portal_id}/grid/${sensor_id}/Ac/L1/Current
              topic: !lambda return str_sprintf("W/%s/grid/%d/Ac/L1/Current", id(vrm_portal_id).c_str(), id(sensor_id));
              payload: |-
                root["value"] = id(ct1Amps).state;
      power:
        name: ${disp_name} CT1 Watts
        accuracy_decimals: 1
        id: "ct1Watts"
        # filters:
        #   - multiply: 4
        on_value:
          - mqtt.publish_json:
              # topic: W/${vrm_portal_id}/grid/${sensor_id}/Ac/L1/Power
              topic: !lambda return str_sprintf("W/%s/grid/%d/Ac/L1/Power", id(vrm_portal_id).c_str(), id(sensor_id));
              payload: |-
                root["value"] = id(ct1Watts).state;
      gain_voltage: ${voltage_cal}
      gain_ct: 24900 #${current_cal}
    phase_c:
      current:
        name: ${disp_name} CT2 Amps
        id: "ct2Amps"
# The max value for current that the meter can output is 65.535. If you expect to measure current over 65A, 
# divide the gain_ct by 2 (120A CT) or 4 (200A CT) and multiply the current and power values by 2 or 4 by uncommenting the filter below
        # filters:
        #   - multiply: 4
        on_value:
          - mqtt.publish_json:
              # topic: W/${vrm_portal_id}/grid/${sensor_id}/Ac/L2/Current
              topic: !lambda return str_sprintf("W/%s/grid/%d/Ac/L2/Current", id(vrm_portal_id).c_str(), id(sensor_id));
              payload: |-
                root["value"] = id(ct2Amps).state;
      power:
        name: ${disp_name} CT2 Watts
        accuracy_decimals: 1
        id: "ct2Watts"
        # filters:
        #   - multiply: 4
        on_value:
          - mqtt.publish_json:
              # topic: W/${vrm_portal_id}/grid/${sensor_id}/Ac/L2/Power
              topic: !lambda return str_sprintf("W/%s/grid/%d/Ac/L2/Power", id(vrm_portal_id).c_str(), id(sensor_id));
              payload: |-
                root["value"] = id(ct2Watts).state;
      gain_voltage: ${voltage_cal}
      gain_ct: 24858 #${current_cal}
    frequency:
      name: ${disp_name} Freq
    line_frequency: 60Hz
    gain_pga: 4X
    update_interval: ${update_time}

  - platform: template
    name: ${disp_name} Total Amps
    id: "totalAmps"
    lambda: return id(ct1Amps).state + id(ct2Amps).state;
    accuracy_decimals: 2
    unit_of_measurement: A
    update_interval: ${update_time}  
    device_class: current
    on_value:
      - mqtt.publish_json:
          # topic: W/${vrm_portal_id}/grid/${sensor_id}/Ac/Current
          topic: !lambda return str_sprintf("W/%s/grid/%d/Ac/Current", id(vrm_portal_id).c_str(), id(sensor_id));
          payload: |-
            root["value"] = id(totalAmps).state;

  - platform: template
    name: ${disp_name} Total Watts
    id: "totalWatts"
    lambda: return id(ct1Watts).state + id(ct2Watts).state;
    accuracy_decimals: 0
    unit_of_measurement: W
    device_class: power
    update_interval: ${update_time}
    on_value:
      - mqtt.publish_json:
          # topic: W/${vrm_portal_id}/grid/${sensor_id}/Ac/Power
          topic: !lambda return str_sprintf("W/%s/grid/%d/Ac/Power", id(vrm_portal_id).c_str(), id(sensor_id));
          payload: |-
            root["value"] = id(totalWatts).state;

  - platform: total_daily_energy
    name: ${disp_name} Total kWh
    id: "totalDailyEnergy"
    power_id: totalWatts
    filters:
      - multiply: 0.001
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing
    on_value:
      - mqtt.publish_json:
          # topic: W/${vrm_portal_id}/grid/${sensor_id}/Ac/Energy/Forward
          topic: !lambda return str_sprintf("W/%s/grid/%d/Ac/Energy/Forward", id(vrm_portal_id).c_str(), id(sensor_id));
          payload: |-
            root["value"] = id(totalDailyEnergy).state;

  - platform: total_daily_energy
    name: ${disp_name} CT1 Total kWh
    id: "totalL1DailyEnergy"
    power_id: ct1Watts
    filters:
      - multiply: 0.001
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing
    on_value:
      - mqtt.publish_json:
          # topic: W/${vrm_portal_id}/grid/${sensor_id}/Ac/L1/Energy/Forward
          topic: !lambda return str_sprintf("W/%s/grid/%d/Ac/L1/Energy/Forward", id(vrm_portal_id).c_str(), id(sensor_id));
          payload: |-
            root["value"] = id(totalL1DailyEnergy).state;

  - platform: total_daily_energy
    name: ${disp_name} CT2 Total kWh
    id: "totalL2DailyEnergy"
    power_id: ct2Watts
    filters:
      - multiply: 0.001
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing
    on_value:
      - mqtt.publish_json:
          # topic: W/${vrm_portal_id}/grid/${sensor_id}/Ac/L2/Energy/Forward
          topic: !lambda return str_sprintf("W/%s/grid/%d/Ac/L2/Energy/Forward", id(vrm_portal_id).c_str(), id(sensor_id));
          payload: |-
            root["value"] = id(totalL2DailyEnergy).state;

time:
  - platform: sntp
    id: sntp_time    

switch:
  - platform: restart
    name: ${disp_name} Restart   

mqtt:
  topic_prefix: grid
  broker: 10.52.84.212
  discovery: false  # we do not need homeassistant here
  birth_message:
    topic: device/${client_id}/Status
    # topic: grid/status
    # payload: online
    payload: '{"clientId": "${client_id}", "connected": 1, "version": "1", "services": {"L1": "grid"}}'
    # retain: False
  will_message:
    topic: device/${client_id}/Status
    payload: '{"clientId": "${client_id}", "connected": 0, "version": "1"}'
  on_json_message:
    topic: device/${client_id}/DBus
    then:
      - globals.set:
          id: vrm_portal_id
          value: !lambda |-
            return x["portalId"];
      - globals.set:
          id: sensor_id
          value: !lambda |-
            return x["deviceInstance"]["${sensor_name}"];
mtucker502 commented 1 year ago

@stundenblume Did the esphome configuration help you?

stundenblume commented 1 year ago

@mtucker502 I'm a happy Tasmota user. @sheldonadjah is the one who needed help.

freakent commented 1 year ago

Can this be closed now?

sheldonadjah commented 1 year ago

yes this was very helpful. sorry for the late respond. i went out of town.