Python Forum
error in python script (for energy meter)
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error in python script (for energy meter)
#1

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.
Reply
#2
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
Reply
#3
(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/64a40a89...140b4bea2c
Reply
#4
is the file name indeed config.py? it is expecting just config
Reply
#5
(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
Reply
#6
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
Reply
#7
AAA, I see - the first error comes from python2. There it is ConfigParser. You should run it with python3
Reply
#8
(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
Reply
#9
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 ?
Reply
#10
This thread can be closed if you want.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sound Meter dcanogi 3 5,074 Oct-17-2025, 06:12 AM
Last Post: OtiliaGen
  Modify an Energy Model to account for year dependent interest rate rather than only t giovanniandrean 0 1,375 Oct-10-2023, 07:00 AM
Last Post: giovanniandrean
  Alicat Flow Meter data acquisition marenr 0 1,394 Sep-29-2023, 10:59 PM
Last Post: marenr
Sad "PriceSystem" Python Script error to Shopify API Alphetto 0 1,235 Jul-04-2023, 10:03 AM
Last Post: Alphetto
  No module named '_cffi_backend' error with executable not with python script stephanh 2 9,667 Nov-25-2021, 06:52 AM
Last Post: stephanh
  How to kill a bash script running as root from a python script? jc_lafleur 4 9,947 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 3,632 May-28-2020, 05:27 PM
Last Post: micseydel
  Python script Server list - if condition error razor04 12 9,133 Jan-27-2020, 09:05 PM
Last Post: razor04
  PYTHON Script ERROR Help Need alex9745 3 3,947 Sep-17-2019, 08:37 AM
Last Post: alex9745
  Using VBA to Call a Python script causes error in pyodbc connector pcarra 1 4,572 Jun-11-2019, 04:14 PM
Last Post: pcarra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020