Reading Energy Data from HAN Port?

Hi,

you can change the IP and port during the configuration of the integration.

I just answered to a similar bug report on GitHub in ha-ppc-smgw#28.
Could you check if your network is configured correctly so that you could reach 192.168.168.168 from your devices? If you haven’t changed the IP ranges in your router then you probably can’t connect to the GW since the default is mostly 192.168.178.0/24 (For AVM Fritz!Boxes).

You have three/(four) choices if this is the case:

  • Change your networks range to 192.168.168.0/24
  • Resize your network to 192.168.168.0/19 if you have devices that rely on the current network IPs (Which gives you a huge network and maybe issues due to a strange home networking range)
  • Deploy a reverse proxy or NAT to a dedicated network just for the SMGW integration (This is what I ended up with - see the code for this)
  • Ask your electricity grid provider to either change the IP of the GW or to activate DHCP (Which would solve all of these issues)

Sadly it seems to be not required (Or advised by PPC) to have DHCP activated in the SMGWs. I am not sure why this was done like this - not so tech-savy users will in this case not be able to interact with their Gateway at all…

1 Like

I investigated a bit and found the m2m interface for the Theben Conexa 3.0 at /smgw/m2m/.
The endpoint is always the same but the query is sent in the body with the key method. So to get the current readings you need to do two requests.
First get the usage-point-id via method user-info. Make sure to use Digest Auth (for trying out I used Postman):

POST /smgw/m2m/ HTTP/1.1
Host: 192.168.66.99
Authorization: Digest username=...
Accept: application/json
Content-Type: application/json

{
    "method": "user-info"
}  

this should return something like this

{
    "elapsed-time": "1089 miliseconds",
    "method": "user-info",
    "user-info": {
        "usage-points": [
            {
                "billing-Periods": [
                    {
                        "end-time": "2024-02-01T00:00:00Z",
                        "start-time": "2024-01-10T18:00:00Z"
                    },
                    {
                         "start-time": "2024-01-01T00:00:00Z"
                    }
                ],
                "capture-time": "00:00:00",
                "delivery-id": "xxx",
                "end-time": "2025-02-10T22:00:00Z",
                "meter": [
                    {
                        "meter-id": "xxx"
                    }
                ],
                "metering-point-id": "xxx",
                "on-demand-profile-configured": "true",
                "start-time": "2024-01-01T00:00:00Z",
                "taf-number": "1",
                "taf-state": "running",
                "usage-point-id": "abc",
                "usage-point-name": "xxx"
            },
            ...
        ]
    },
    "version": "1.3.0"
}

note that there could be several usage-points. Check for the one with "taf-state": "running" and note down the usage-point-id.

Now you can query the latest readings like so:

POST /smgw/m2m/ HTTP/1.1
Host: 192.168.66.99
Authorization: Digest username=...
Accept: application/json
Content-Type: application/json

{
    "method": "readings",
    "database": "origin",
    "usage-point-id": "abc",
    "last-reading": "true"
}

This will get the latest reading:

{
    "elapsed-time": "44 miliseconds",
    "method": "readings",
    "readings": {
        "channels": [
            {
                "obis": "xxx",
                "readings": [
                    {
                        "capture-time": "2025-02-20T20:14:57Z",
                        "cosem-status": "xxx",
                        "meter-status": "xxx",
                        "owner-number": "xxx",
                        "signature": "xxx",
                        "smgw-status": "xxx",
                        "target-time": "2025-02-20T20:15:00Z",
                        "value": "123456"
                    }
                ]
            }
        ],
        "records": "1"
    },
    "version": "1.3.0"
}

The value is the energy reading. The last 4 digits are decimal places so the example above would be 12,3456 kWh.
In my case it gave me a new reading every quarter of an hour but this might vary.

If this is working setting up a REST sensor within Home Assistant is straight forward:

sensor:
  - platform: rest
    name: energy
    unique_id: my_energy_data
    unit_of_measurement: kWh
    scan_interval: 900 # 15 min
    timeout: 20
    resource: https://192.168.66.99/smgw/m2m/
    method: POST
    username: <username>
    password: <password>
    authentication: digest
    verify_ssl: false # the SMGW uses a self-signed cert
    headers:
      Content-Type: application/json
      Accept: application/json
    payload: |
        {
            "method": "readings",
            "database": "origin",
            "usage-point-id": "abcd",
            "last-reading": "true"
        }
    value_template: "{{ float(value_json.readings.channels[0].readings[0].value) / 10000 }}"

This setup is running successful for me quite a while now without any issues. I’m not doing the usage-point-id lookup for the reading but not sure if the usage-point-id might change.

Some notes:

  • For this to work, the home assistant server needs to be in the same subnet as the SMGW (in this case 192.168.66.0). Querying from another subnet e.g. 192.168.0.0 won’t work.
  • There are more methods and parameters to discover like the device log, or daily readings but I didn’t bother as my goal was just to get the latest readings.

Hope this helps someone!

2 Likes

After being able to connect to the smgw via ethernet, i wrote some python code to retrieve the data, see this issue in Github.

Great solution.

I have listed the possible use cased in this issue over in project of @jannickfahlbusch in GitHub.

We are trying to implement them in his integration, but your manual approach with the REST integration is very nice.

@mbaeuerle : Thanks for the code. How long does the response from the Theben Connexa take for you? For me, it takes 8-10 seconds with the requests library, just to query the firmware version.

Maybe that’s where my problem lies, why I can’t get to it with httpx.

Thanks for the tip. I have now integrated it with me and it works.

Maybe this module will do it for Kamstrup meters
https://shop.watts.dk/products/watts-live-kamstrup