Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues reading the P1Packet() class #30

Open
sandnabba opened this issue Nov 4, 2022 · 5 comments · May be fixed by #32
Open

Issues reading the P1Packet() class #30

sandnabba opened this issue Nov 4, 2022 · 5 comments · May be fixed by #32

Comments

@sandnabba
Copy link
Contributor

Hello!

Next issue (after solving #29 ) is that the P1Packet class does not seem to work as expected.

Full output running the CLI:

emil@fasadmatarskapet:~/smeterd $ python3 -m smeterd read-meter --serial-baudrate 115200 --serial-bytesize 8 
DEBUG:smeterd.meter:Open serial connect to /dev/ttyUSB0 with: baudrate=115200, bytesize=8, parity=E, stopbits=1.0, xonxoff=False, timeout=10
INFO:smeterd.meter:New serial connection opened to /dev/ttyUSB0
INFO:smeterd.meter:Start reading lines
DEBUG:smeterd.meter:>> /AUX5U18SEH13F10029
DEBUG:smeterd.meter:>> 
DEBUG:smeterd.meter:>> 0-0:96.1.0(38303034363937333432383437313031303137333437)
DEBUG:smeterd.meter:>> 0-0:1.0.0(221104214240W)
DEBUG:smeterd.meter:>> 1-0:1.8.0(004715.829*kWh)
DEBUG:smeterd.meter:>> 1-0:2.8.0(000000.000*kWh)
DEBUG:smeterd.meter:>> 1-0:3.8.0(000000.024*kvarh)
DEBUG:smeterd.meter:>> 1-0:4.8.0(003708.969*kvarh)
DEBUG:smeterd.meter:>> 1-0:1.7.0(0000.742*kW)
DEBUG:smeterd.meter:>> 1-0:2.7.0(0000.000*kW)
DEBUG:smeterd.meter:>> 1-0:3.7.0(0000.000*kvar)
DEBUG:smeterd.meter:>> 1-0:4.7.0(0000.782*kvar)
DEBUG:smeterd.meter:>> 1-0:21.7.0(0000.156*kW)
DEBUG:smeterd.meter:>> 1-0:22.7.0(0000.000*kW)
DEBUG:smeterd.meter:>> 1-0:41.7.0(0000.440*kW)
DEBUG:smeterd.meter:>> 1-0:42.7.0(0000.000*kW)
DEBUG:smeterd.meter:>> 1-0:61.7.0(0000.144*kW)
DEBUG:smeterd.meter:>> 1-0:62.7.0(0000.000*kW)
DEBUG:smeterd.meter:>> 1-0:23.7.0(0000.000*kvar)
DEBUG:smeterd.meter:>> 1-0:24.7.0(0000.123*kvar)
DEBUG:smeterd.meter:>> 1-0:43.7.0(0000.000*kvar)
DEBUG:smeterd.meter:>> 1-0:44.7.0(0000.470*kvar)
DEBUG:smeterd.meter:>> 1-0:63.7.0(0000.000*kvar)
DEBUG:smeterd.meter:>> 1-0:64.7.0(0000.189*kvar)
DEBUG:smeterd.meter:>> 1-0:32.7.0(232.0*V)
DEBUG:smeterd.meter:>> 1-0:52.7.0(234.5*V)
DEBUG:smeterd.meter:>> 1-0:72.7.0(236.1*V)
DEBUG:smeterd.meter:>> 1-0:31.7.0(001.0*A)
DEBUG:smeterd.meter:>> 1-0:51.7.0(002.8*A)
DEBUG:smeterd.meter:>> 1-0:71.7.0(001.0*A)
DEBUG:smeterd.meter:>> !C956
INFO:smeterd.meter:Done reading one packet (containing 31 lines)
DEBUG:smeterd.meter:Total lines read from serial port: 31
DEBUG:smeterd.meter:Constructing P1Packet from raw data
INFO:smeterd.meter:Closing connection to `/dev/ttyUSB0`.
Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/emil/smeterd/smeterd/__main__.py", line 3, in <module>
    cli()
  File "/home/emil/.local/lib/python3.9/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/emil/.local/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/emil/.local/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/emil/.local/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/emil/.local/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/emil/smeterd/smeterd/cli/read_meter.py", line 74, in read_meter
    ('Total electricity consumed (high, {})'.format(elec_unit), int(packet['kwh']['high']['consumed'] * elec_unit_factor[elec_unit])),
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

If I'm using the --raw flag it just prints the packet and exits successfully.

I'm also trying to use the lib as a module, but I'm missing some examples of how to access the class variables.

The following works:

packet = meter.read_one_packet()

# Print L1 voltage:
print(packet.__getitem__('instantaneous')['l1']['volts']

But first using a function, then digging into a list feels a bit complicated. Is there a better way (using (vars() perhaps?), or is there any example somewhere?

Hope you can point me in the right direction, I'm not a Python expert. :)

Best regards

@nrocco
Copy link
Owner

nrocco commented Nov 4, 2022

@sandnabba thanks for reporting this. I will take a look at the code this weekend.

What meter (make and model) do you have?

@sandnabba
Copy link
Contributor Author

Perfect and thanks a lot!

It's an "S34U18" meter, which is branded by "Vattenfall", the grid owner. Seems like that meter is very common here in Sweden, also on other operators as well (With their name on it).

But in the end, I'm quite sure it's this one:
http://www.sanxingelectric.com/prodeta.php?id=5oiR55qEezEzMX10aGlzX2lzX2FfZW5jcnlwdColJSQkLCZuIWlpQD9pZD03NjQv

@nrocco
Copy link
Owner

nrocco commented Jan 3, 2023

@sandnabba The issue is that the P1 telegram that your meter (S34U18) is emitting does not contain a line matching this regex:

# Finds a line starting with 1-0:1.8.1
keys['kwh']['low']['consumed'] = self.get_float(rb'^1-0:1\.8\.1\(([0-9]+\.[0-9]+)\*kWh\)\r\n')

or

# Finds a line starting with 1-0:1.8.2
keys['kwh']['high']['consumed'] = self.get_float(rb'^1-0:1\.8\.2\(([0-9]+\.[0-9]+)\*kWh\)\r\n')

and possibly others, I have not checked them all.

That is the reason why the smeterd read-meter command does not work. It tries to multiply None with a number :).

Do you happen to have documentation (pdf) of this meter? I am curious to know what the values in the P1 packets mean.

@sandnabba
Copy link
Contributor Author

sandnabba commented Jan 4, 2023

Hi! Yeah, had the same issue with ['instantaneous'][***]['amps'] :)

I have a branch were I got the data I was looking for (mostly voltage and current on the line):
master...sandnabba:smeterd:S34U18-support

I was planning to fix this issue as well before submitting a PR with full "S34U18 support". :)

But I guess the issue is related, and as you can see, the main issue is that my meter seems to report the current (A) as a float instead of an integer.
It actually makes more sense to me, but I guess this could potentially be problematic for existing users?

Here is the API documentation for the meter:
https://www.energiforetagen.se/globalassets/energiforetagen/det-erbjuder-vi/kurser-och-konferenser/elnat/branschrekommendation-lokalt-granssnitt-v2_0-201912.pdf

Unfortunately it seems like it's only available in Swedish. The titel is "Industry recommendations for local interface", published by "Swedenergy":

Swedenergy is a non-profit industry and special interest organisation for companies that supply, distribute, sell, and store energy

So I guess this applies for pretty much everyone in Sweden at least.

I've sent them an email asking if there is an english version somewhere. But the API specification is on the last 2 pages.

And I hope that I have a fully working version ready this weekend. :)

// Emil

@nrocco
Copy link
Owner

nrocco commented Jan 4, 2023

@sandnabba nice. Looking forward to getting this solved and supported

@sandnabba sandnabba linked a pull request Jan 21, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants