Python Forum
error in python script (for energy meter) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: error in python script (for energy meter) (/thread-6611.html)



error in python script (for energy meter) - br0ken - Nov-30-2017


Hello,

i'm having issues with a script which I found on https://github.com/datenschuft/SMA-EM

It's a script to read out and write away to some file the values of the energy meter which it has been created for.

I tried contacting the creator, but seems he has no clue either, or his instructions were not very complete.

Anyhow, I hope anyone can point me in the right direction regarding the following error:

Error:
/home/SMA-EM $ sudo python3 sma-daemon.py Traceback (most recent call last): File "/usr/local/lib/python3.5/configparser.py", line 1136, in _unify_values sectiondict = self._sections[section] KeyError: 'SMA-EM' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "sma-daemon.py", line 30, in <module> smaemserials=parser.get('SMA-EM', 'serials') File "/usr/local/lib/python3.5/configparser.py", line 779, in get d = self._unify_values(section, vars) File "/usr/local/lib/python3.5/configparser.py", line 1139, in _unify_values raise NoSectionError(section) configparser.NoSectionError: No section: 'SMA-EM'
I get this error after running this script:



#!/usr/bin/env python3
# coding=utf-8
"""
 ....
"""
import sys, time
from daemon3x import daemon3x
from configparser import SafeConfigParser
import smaem

#read configuration
parser = SafeConfigParser()
parser.read('/etc/smaemd/config')

smaemserials=parser.get('SMA-EM', 'serials')
serials=smaemserials.split(' ')
smavalues=parser.get('SMA-EM', 'values')
values=smavalues.split(' ')
pidfile=parser.get('DAEMON', 'pidfile')

class MyDaemon(daemon3x):
def run(self):
emparts = {}
while True:
emparts=smaem.readem()
for serial in serials:
#print(serial)
#print(emparts['serial'])
if serial==format(emparts['serial']):
#print("match")
for value in values:
file = open("/run/shm/em-"+format(serial)+"-"+format(value), "w")
file.write('%.4f' % emparts[value])
file.close()

if __name__ == "__main__":
daemon = MyDaemon(pidfile)
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
else:
print ("Unknown command")
sys.exit(2)
sys.exit(0)
else:
print ("usage: %s start|stop|restart" % sys.argv[0])
print (pidfile)
sys.exit(2)
and it refers to this config file as well (which I needed to create myself given the instructions)

configfile Wrote:[SMA-EM]

*serials of sma-ems the daemon should take notice
*seperated by space
serials=1900254500
*measurement values
values=pregard psurplus qsurplus ssurplus

[DAEMON]
pidfile=/run/smaemd.pid

I tried running python v3 and the base version (2.7...) with sudo ofcourse.

The issue i posted on this github, which gave me no info :

https://github.com/datenschuft/SMA-EM/issues/9


Thanks for anyone who wants to help out here.


RE: error in python script (for energy meter) - buran - Nov-30-2017

can you upload the actual config file somewhere, e.g. on GitHub repository/Gist? it is either problem with the file or it is on wrong location


RE: error in python script (for energy meter) - br0ken - Nov-30-2017

(Nov-30-2017, 02:54 PM)buran Wrote: can you upload the actual config file somewhere, e.g. on GitHub repository/Gist?

hmm, thanks for your input

nothing more as what I posted in the openingpost in fact, as based on the instructions the creator gave me, but here you go:

https://gist.github.com/broke23/64a40a89b7a7ebd59011d1140b4bea2c


RE: error in python script (for energy meter) - buran - Nov-30-2017

is the file name indeed config.py? it is expecting just config


RE: error in python script (for energy meter) - br0ken - Nov-30-2017

(Nov-30-2017, 03:30 PM)buran Wrote: is the file name indeed config.py? it is expecting just config

ps: config is located in etc/smaemd/

After renaming it I get the following error:
sudo python sma-daemon.py
Error:
Traceback (most recent call last): File "sma-daemon.py", line 23, in <module> from configparser import SafeConfigParser ImportError: No module named configparser
sudo python3 sma-daemon.py
Error:
Traceback (most recent call last): File "/usr/local/lib/python3.5/configparser.py", line 1136, in _unify_values sectiondict = self._sections[section] KeyError: '[SMA-EM]' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "sma-daemon.py", line 30, in <module> smaemserials=parser.get('[SMA-EM]', 'serials') File "/usr/local/lib/python3.5/configparser.py", line 779, in get d = self._unify_values(section, vars) File "/usr/local/lib/python3.5/configparser.py", line 1139, in _unify_values raise NoSectionError(section) configparser.NoSectionError: No section: '[SMA-EM]'

[inline]sudo systemctl daemon-reload
sudo systemctl enable smaemd.service
sudo systemctl start smaemd.service[/inline]

didn't change anything by the way


RE: error in python script (for energy meter) - buran - Nov-30-2017

Just renaming the file could not affect if configparser is available if it worked before. Also it is not possible to have both errors from your last post simultaneously


RE: error in python script (for energy meter) - buran - Nov-30-2017

AAA, I see - the first error comes from python2. There it is ConfigParser. You should run it with python3


RE: error in python script (for energy meter) - br0ken - Nov-30-2017

(Nov-30-2017, 04:47 PM)buran Wrote: AAA, I see - the first error comes from python2. There it is ConfigParser. You should run it with python3

But I did try both versions. First is indeed v2. Next is v3

Ps. I have never seen it work. Trying to make it work but run into these errors


RE: error in python script (for energy meter) - br0ken - Dec-01-2017

apparently the config file was not in UTF-8 ? Confused

i ran following code to fix it and afterwards it worked:

iconv -f UTF-8 your_file -o /dev/null

sudo python3 sma-daemon.py
usage: sma-daemon.py start|stop|restart
/run/smaemd.pid
so now I tried with

 sudo python3 sma-daemon.py start
and it seems to be working now! Heart

can I ask how come that simple copy-paste ruins the utf-8 ? I guess it's default on my raspberry pi, though copy pasting took wrong coding then ?


RE: error in python script (for energy meter) - br0ken - Dec-01-2017

This thread can be closed if you want.