Skip to content

Daily Power Usage / Generation + EG4 Config #3

@Joannou1

Description

@Joannou1

Just wanted to say thank you for creating this!
I was able to dig into their API's a bit further and I created a way to extract the daily generation / consumption.

It was tricky because their API needs a signature that matches the request + some other stuff.
The code to generate a signature and pull daily data is as follows:

dessmonitor_api_uri_bat_active_power: >-  
  {% set dess_param = "BATTERY_ACTIVE_POWER" %}
  {% set dess_action = "querySPDeviceKeyParameterOneDay" %}
  {% set dess_salt = "1736254948715" %}
  {% set dess_secret = "8d424eb86exxxxxxxxxedde7ac98f086a" %}
  {% set dess_token = "0058f23badac72018153bc5f7fcxxxxxxxxxxxxd737318c4638c985d" %}
  {% set dess_pn = "Q123456789" %}
  {% set dess_sn = "Q12345678912345678" %}

  {% set current_date = (as_timestamp(now())) | timestamp_custom("%Y-%m-%d", True) %}
  {% set signature = sha1(dess_salt + dess_secret + dess_token + "&action=" + dess_action + "&source=1&pn=" + dess_pn + "&sn=" + dess_sn + "&devcode=2376&devaddr=1&i18n=en_US&parameter=" + dess_param + "&chartStatus=false&date=" + current_date) %}

  {% set baseURL = "https://web.dessmonitor.com/public/?sign=" + signature + "&salt=" + dess_salt + "&token=" + dess_token + "&action=" + dess_action + "&source=1&pn=" + dess_pn + "&sn=" + dess_sn + "&devcode=2376&devaddr=1&i18n=en_US&parameter=" + dess_param + "&chartStatus=false&date=" + current_date %}
  {{ baseURL }}

Spook Required for SHA1

dess_param - Query parameter. Other options are: PV_OUTPUT_POWER and LOAD_ACTIVE_POWER
dess_action - The daily query.
dess_salt - The current timestamp, but it looks like it can be any number. It doesn't appear that it needs to change either.

dess_secret - This one is tricky. I'm not actually sure how to decode the one store in cookies. It seems like base64, but it doesn't decode to the string that I have above. I used Chrome devtools to override their JS and print out the secret.
Can be found on line 28065 in their app.js. Search for , s = g["a"].state.user.secret

dess_token - Your token
dess_pn - Part number?
dess_sn - Serial number?

There's also devcode and devaddr, which I'm unsure what they do, but leaving as they were in the request.

I was able to put the above code in secrets.yaml and use it as the resource template.

  - platform: rest
    scan_interval: 120
    name: Battery Charge
    resource_template: !secret dessmonitor_api_uri_bat_active_power
    method: GET
    json_attributes_path: "$.dat"
    json_attributes:
      - detail
    value_template: "OK"

Now to actually use the data, I had to take their data intervals, get an average, convert to watt hours then sum up.
Since their intervals aren't exactly 5 minutes, sometimes 5 minutes and change, I had to create a time difference and get a hourly fractional to make 5 minute watt hour measurements.

- sensor:
    - name: "Daily Battery Discharge Power"
      state: >
        {% set dates = state_attr('sensor.trailer_battery_charge','detail') %}
        {% set ns = namespace(prev_power=0) %}
        {% set ns = namespace(prev_time=0) %}
        {% set ns = namespace(total_power_discharge=0) %}

        {% for i in range(0, dates | count, 1) %}
            {% set curr_time = dates[i]['ts'] %}
            {% set curr_power = dates[i]['val'] | float %}
            
            {#{curr_time + " / " + (curr_power | string)}#}

            {% if i > 0 %}
              {% set pow_diff = ((ns.prev_power + curr_power) / 2) %}
              {% set time_diff = ((curr_time | as_datetime).astimezone() - (ns.prev_time | as_datetime).astimezone()).total_seconds() %}

              {#
              Prev Power: {{ns.prev_power}}
              Curr Time: {{(curr_time | as_datetime).astimezone()}}
              Prev Time: {{ns.prev_time | as_datetime}}
              Power Diff: {{pow_diff}}
              Time Diff: {{time_diff}}
              #}
             

              {% if pow_diff < 0 %}
                {% set ns.total_power_discharge = ns.total_power_discharge + ((pow_diff * (time_diff / 3600) * 1000)) %}
                {#
                Total Power: {{ns.total_power_discharge}} Wh
                #}
              {% endif %}
            {% else %}
              {% set ns.total_power_discharge = ns.total_power_discharge %}
            {% endif %}
            
            {% set ns.prev_time = curr_time %}
            {% set ns.prev_power = curr_power %}
        {% endfor %}
        
        {{ ns.total_power_discharge }}

      unit_of_measurement: "W"
      device_class: "energy"
      state_class: "total_increasing"

{% if pow_diff < 0 %} can be replaced with {% if pow_diff > 0 %} to count charge.

This is my first time writing any substantial code for Home Assistant, so apologies if it's not as nice as it could or should be :p

The end result are a bunch of nice daily readings that can be hooked up to the energy flow card!
image
image

I'm not confident enough to submit it as a PR, so I figure I'd leave the code here if you or anyone wants to integrate it a bit better than I did.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions