Integration of Sunlit battery

Hey, first of all in order to make sure you dont have to blackout the key evertime you can use and

    headers:
      Content-Type: application/json
      User-Agent: SunLit/1.5.3 (iOS)
      Authorization: !secret sunlit_token

and in the secrets.yaml file you need to add:

sunlit_token: "Bearer ey...DA"

Furthermore, I have integrated the MPPTs myself and noticed that the payload needs to include the following:

    payload: '{"spaceId": 99999, "deviceId": 99999}'

so my new sensors.yaml looks like this:

  - platform: rest
    name: "SunLit SoC"
    resource: "https://api.sunlitsolar.de/rest/v1.5/space/index"
    method: POST
    headers:
      Content-Type: application/json
      User-Agent: SunLit/1.5.3 (iOS)
      Authorization: !secret sunlit_token
    payload: '{"spaceId": 99999}'
    value_template: "{{ value_json.content.battery.batteryLevel }}"
    json_attributes:
      - content
    unit_of_measurement: "%"
    scan_interval: 180

  - platform: rest
    name: "SunLit MPPT"
    resource: "https://api.sunlitsolar.de/rest/v1.1/statistics/static/device"
    method: POST
    headers:
      Content-Type: application/json
      User-Agent: SunLit/1.5.3 (iOS)
      Authorization: !secret sunlit_mppt_token
    payload: '{"spaceId": 99999, "deviceId": 99999}'
    value_template: "{{ value_json.content.batteryMppt1Data.batteryMpptInPower }}"
    json_attributes:
      - content
    unit_of_measurement: "W"
    device_class: "power"
    scan_interval: 180

and my templates.yaml looks like this:

- sensor:
    - name: "MPPT 1 Spannung"
      unique_id: mppt1_voltage
      unit_of_measurement: "V"
      icon: mdi:flash
      state: >
        {% set attr = state_attr('sensor.sunlit_mppt', 'content') %}
        {% if attr and 'batteryMppt1Data' in attr %}
          {{ attr['batteryMppt1Data']['batteryMpptInVol'] }}
        {% else %}
          0
        {% endif %}

    - name: "MPPT 1 Strom"
      unique_id: mppt1_current
      unit_of_measurement: "A"
      icon: mdi:current-dc
      state: >
        {% set attr = state_attr('sensor.sunlit_mppt', 'content') %}
        {% if attr and 'batteryMppt1Data' in attr %}
          {{ attr['batteryMppt1Data']['batteryMpptInCur'] }}
        {% else %}
          0
        {% endif %}

    - name: "MPPT 1 Leistung"
      unique_id: mppt1_power
      unit_of_measurement: "W"
      device_class: power
      icon: mdi:power
      state: >
        {% set attr = state_attr('sensor.sunlit_mppt', 'content') %}
        {% if attr and 'batteryMppt1Data' in attr %}
          {{ attr['batteryMppt1Data']['batteryMpptInPower'] }}
        {% else %}
          0
        {% endif %}

    - name: "MPPT 2 Spannung"
      unique_id: mppt2_voltage
      unit_of_measurement: "V"
      icon: mdi:flash
      state: >
        {% set attr = state_attr('sensor.sunlit_mppt', 'content') %}
        {% if attr and 'batteryMppt2Data' in attr %}
          {{ attr['batteryMppt2Data']['batteryMpptInVol'] }}
        {% else %}
          0
        {% endif %}

    - name: "MPPT 2 Strom"
      unique_id: mppt2_current
      unit_of_measurement: "A"
      icon: mdi:current-dc
      state: >
        {% set attr = state_attr('sensor.sunlit_mppt', 'content') %}
        {% if attr and 'batteryMppt2Data' in attr %}
          {{ attr['batteryMppt2Data']['batteryMpptInCur'] }}
        {% else %}
          0
        {% endif %}

    - name: "MPPT 2 Leistung"
      unique_id: mppt2_power
      unit_of_measurement: "W"
      device_class: power
      icon: mdi:power
      state: >
        {% set attr = state_attr('sensor.sunlit_mppt', 'content') %}
        {% if attr and 'batteryMppt2Data' in attr %}
          {{ attr['batteryMppt2Data']['batteryMpptInPower'] }}
        {% else %}
          0
        {% endif %}

    - name: "MPPT Batterie 1 Leistung"
      unique_id: mppt_batt1_power
      unit_of_measurement: "W"
      device_class: power
      icon: mdi:power
      state: >
        {% set attr = state_attr('sensor.sunlit_mppt', 'content') %}
        {% if attr and 'battery1MpptData' in attr %}
          {{ attr['battery1MpptData']['batteryMpptInPower'] }}
        {% else %}
          0
        {% endif %}

    - name: "MPPT Batterie 2 Leistung"
      unique_id: mppt_batt2_power
      unit_of_measurement: "W"
      device_class: power
      icon: mdi:power
      state: >
        {% set attr = state_attr('sensor.sunlit_mppt', 'content') %}
        {% if attr and 'battery2MpptData' in attr %}
          {{ attr['battery2MpptData']['batteryMpptInPower'] }}
        {% else %}
          0
        {% endif %}

Your errors indicate that either:

  1. The REST API is not returning data
  2. The template is accessing attributes that don’t exist

To verify what you’re actually getting back from the API:

Go to:

Developer Tools → Templates

Enter this:

{{ state_attr('sensor.sunlit_bk215_soc', 'content') }}
  • If it returns None, the API call failed or returned no JSON
  • If it returns a dictionary, check what keys exist – e.g. batterySoc, inputPowerTotal, etc.

Hey,
that was the cause, and since I couldn’t verify the result, I was groping in the dark.
I couldn’t imagine that the word “bearer” belonged to the token. Which annoys me, though, because I had also seen that as a possible error, but forgot to ask.
grafik

Edit:
Very cool. A dream. I’m sure I can manage the rest with the templates. I hope this tip helps others too. Thank you so much again.
You’ve been doing this for a while… was 150 seconds the minimum interval? In the app, it seemed more like 90 to 120 seconds.
With more time, I’ll read everything out of the thing too!

Edit 2.
so i love my updatet Awtrix Pixelclock:
ergebnis-awtrix

Hi there,
I was following this thread and it was a great help so far. Now I have added some sensors of my battery in HA. Thanks for that. But I’m still struggling to get the SoC to be shown in the energy dashboard. Somehow I’m not able to add the battery into the system, it’s not shown for selection.
I am monitoring the PV production via zigbee plug. Any ideas?

Hello, from everything I’ve read about SOC in the Energy Dashboard, I’m sure there were requirements, but for some reason they haven’t been implemented. It’s only possible in the Power Flow Energy Dashboard via the HACS or the Power Flow Power Dashboard…

Now I’ve read everything in and tweaked things a bit. I already had a few other sensors and templates in the configuration.yaml, which is why I had problems mixing them up or having multiple ones. But now I have a nice result.

1 Like