python code suddenly not working

Python and python framework

Moderator: leecollings

lambertmular
Posts: 14
Joined: Wednesday 09 May 2018 11:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

python code suddenly not working

Post by lambertmular »

Today I noticed that my python script on domoticz was not running anymore with no obvious reason (I have not touch it for a while). After trying to understand the problem I noticed that even a very simple code is not working at all

Code: Select all

import DomoticzEvents as DE
import re

es=list(map(float,re.findall("(\d+\.?\d+);(\d+);(\d+)",DE.Devices['Temp Chambre'].s_value)[0]))
DE.Log("Python:"+str(es))

def data_meteo(name):
    return list(map(float,re.findall("(\d+\.?\d+);(\d+);(\d+)",DE.Devices[name].s_value)[0]))
    
dat=data_meteo('Temp Chambre')

DE.Log("Python:"+str(dat))
In the log here is the output that I get

Code: Select all

 2023-01-08 15:28:23.348 Python:[17.7, 75.0, 3.0]
2023-01-08 15:28:23.350 Error: Traceback (most recent call last):
2023-01-08 15:28:23.351 Error: File "Script #1", line 10, in <module>
2023-01-08 15:28:23.351 Error: File "Script #1", line 8, in data_meteo
2023-01-08 15:28:23.351 Error: NameError: name 're' is not defined 
I do not understand what happens. Maybe the version of python used by my system changes? Anyone can help me?
Last edited by lambertmular on Sunday 08 January 2023 17:37, edited 1 time in total.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: python code suddenly not working

Post by waltervl »

What is menu setup - about saying about python?
The error is about module "re" (regex expressions) wich i think is a standard installed module so not sure what is going on...
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
lambertmular
Posts: 14
Joined: Wednesday 09 May 2018 11:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: python code suddenly not working

Post by lambertmular »

What is menu setup - about saying about python?
I am sorry I do not get it, what do you mean exactly?
The error is about module "re" (regex expressions) wich i think is a standard installed module so not sure what is going on...
Yes, you are right, the error is about the module "re". But please note that I call twice the same command (line 4 and line 10). The first time I call it, in line 4, it gives the correct output:

Code: Select all

 2023-01-08 15:28:23.348 Python:[17.7, 75.0, 3.0]
The second time I call it (inside a function) it gives the error. It is very strange.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: python code suddenly not working

Post by waltervl »

What is the python version Domoticz is using? You see that in menu setup - about. Or at startup in the logging of Domoticz...
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
lambertmular
Posts: 14
Joined: Wednesday 09 May 2018 11:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: python code suddenly not working

Post by lambertmular »

What is the python version Domoticz is using? You see that in menu setup - about. Or at startup in the logging of Domoticz...
That's "Python Version: 3.7.3 (default, Oct 31 2022, 14:04:00) [GCC 8.3.0]"
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: python code suddenly not working

Post by waltervl »

Ok, that seems a very well supported version. No idea what happened on your side...
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
lambertmular
Posts: 14
Joined: Wednesday 09 May 2018 11:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: python code suddenly not working

Post by lambertmular »

To check if the problem comes from python I also try to run the same code directly with python3.7 on my rapsberry.
I ran the following code on my raspberry using python3.7

Code: Select all

import re
es=re.findall("(\d+\.?\d+);(\d+);(\d+)","18.4;75;3")
print(es)

def ret(dev):
    return re.findall("(\d+\.?\d+);(\d+);(\d+)","18.4;75;3")
    
print(ret('Temp Chambre')) 
It works fine and gives me the output

Code: Select all

[('18.4', '75', '3')]
[('18.4', '75', '3')]
On the other hand on domoticz

Code: Select all

import DomoticzEvents as DE
import re

DE.Log("Python:"+str(re.findall("(\d+\.?\d+);(\d+);(\d+)","18.4;75;3")))

def ret(dev):
    return re.findall("(\d+\.?\d+);(\d+);(\d+)","18.4;75;3")
    
ret('Temp Chambre') 
and it gives me the following output in the log file

Code: Select all

2023-01-08 21:49:41.293 Python:[('18.4', '75', '3')]
2023-01-08 21:49:41.295 Error: Traceback (most recent call last):
2023-01-08 21:49:41.295 Error: File "Script #1", line 9, in <module>
2023-01-08 21:49:41.295 Error: File "Script #1", line 7, in ret
2023-01-08 21:49:41.295 Error: NameError: name 're' is not defined  
The first command with "re" works fine in domoticz but not when it is inside the function "ret", it is very strange. I do not get why
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: python code suddenly not working

Post by willemd »

I don't understand it either, looks like a bug, but if it worked before and now no longer it is not a program error and the key question is: When did it stop working and what happened shortly before it? Can you find it in the logfile? Maybe related to an upgrade?
lambertmular
Posts: 14
Joined: Wednesday 09 May 2018 11:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: python code suddenly not working

Post by lambertmular »

When did it stop working and what happened shortly before it? Can you find it in the logfile? Maybe related to an upgrade?
Yes I have the logfile and know when it stopped working. I copy the log file below:

Code: Select all

2023-01-07 13:31:43.283  RFLink: Temp + Humidity (Temp Chambre)
2023-01-07 13:31:43.313  Python: Appareils allumés:['Bouilloire_Grille pain']
2023-01-07 13:31:43.314  Python: puissance instantanée=692.0
2023-01-07 13:31:43.314  Python: vac:(datetime.datetime(2022, 12, 17, 10, 0), datetime.datetime(2023, 1, 1, 15, 0))
2023-01-07 13:31:55.748  Status: Incoming connection from: 192.168.1.68
2023-01-07 13:32:00.279  Python: Appareils allumés:['Bouilloire_Grille pain']
2023-01-07 13:32:00.279  Python: puissance instantanée=692.0
2023-01-07 13:32:00.280  Python: vac:(datetime.datetime(2022, 12, 17, 10, 0), datetime.datetime(2023, 1, 1, 15, 0))
2023-01-07 13:32:01.907  RFLink: General/kWh (Consommation)
2023-01-07 13:32:01.939  Python: Appareils allumés:['Bouilloire_Grille pain']
2023-01-07 13:32:01.940  Python: puissance instantanée=692.0
2023-01-07 13:32:01.940  Python: vac:(datetime.datetime(2022, 12, 17, 10, 0), datetime.datetime(2023, 1, 1, 15, 0))
2023-01-07 13:32:07.402  Status: Closing application!...
2023-01-07 13:32:07.402  Status: Stopping worker...
2023-01-07 13:32:07.417  Python: Appareils allumés:['Bouilloire_Grille pain']
2023-01-07 13:32:07.417  Python: puissance instantanée=692.0
2023-01-07 13:32:07.417  Python: vac:(datetime.datetime(2022, 12, 17, 10, 0), datetime.datetime(2023, 1, 1, 15, 0))
2023-01-07 13:32:07.417  Status: RxQueue: queue worker stopped...
2023-01-07 13:32:07.918  Status: WebServer(HTTP) stopped
2023-01-07 13:32:08.421  Status: WebServer(SSL) stopped
2023-01-07 13:32:08.423  Status: TCPServer: shared server stopped
2023-01-07 13:32:08.423  Status: Stopping all hardware...
2023-01-07 13:32:08.424  Status: Darksky: Worker stopped...
2023-01-07 13:32:08.426  Status: RFLink: Worker stopped...
2023-01-07 13:32:08.427  Status: Scheduler stopped...
2023-01-07 13:32:08.428  Status: EventSystem: Stopped...
2023-01-07 13:32:08.428  Status: EventSystem: Queue thread stopped...
2023-01-07 13:32:08.475  Status: EventSystem - Python stopped...
2023-01-07 13:32:08.476  Status: NotificationSystem: thread stopped...
2023-01-07 13:32:08.476  Status: PluginSystem: Exited work loop.
2023-01-07 13:32:08.503  Status: PluginSystem: Stopped.
2023-01-07 13:32:08.503  Status: Mainworker Stopped...
2023-01-07 13:32:09.790  Status: Domoticz V2022.2 (c)2012-2022 GizMoCuz
2023-01-07 13:32:09.790  Status: Build Hash: eea9db734, Date: 2022-11-05 12:05:35
2023-01-07 13:32:09.790  Status: Startup Path: /home/pi/domoticz/
2023-01-07 13:32:09.897  Sunrise: 07:56:00 SunSet: 17:24:00
2023-01-07 13:32:09.897  Day length: 09:28:00 Sun at south: 12:40:00
2023-01-07 13:32:09.897  Civil twilight start: 07:26:00 Civil twilight end: 17:54:00
2023-01-07 13:32:09.897  Nautical twilight start: 06:52:00 Nautical twilight end: 18:28:00
2023-01-07 13:32:09.897  Astronomical twilight start: 06:19:00 Astronomical twilight end: 19:00:00
2023-01-07 13:32:10.003  Status: PluginSystem: Started, Python version '3.7.3', 1 plugin definitions loaded.
2023-01-07 13:32:10.011  Active notification Subsystems: (0/13)
2023-01-07 13:32:10.012  Status: WebServer(HTTP) startup failed on address :: with port: 8080: resolve: Host not found (authoritative) [asio.netdb:1], trying ::
2023-01-07 13:32:10.013  Status: WebServer(HTTP) startup failed on address :: with port: 8080: resolve: Host not found (authoritative) [asio.netdb:1], trying 0.0.0.0
2023-01-07 13:32:10.013  Status: WebServer(HTTP) started on address: 0.0.0.0 with port 8080
2023-01-07 13:32:10.026  Status: WebServer(SSL) startup failed on address :: with port: 443: resolve: Host not found (authoritative) [asio.netdb:1], trying ::
2023-01-07 13:32:10.029  Status: WebServer(SSL) startup failed on address :: with port: 443: resolve: Host not found (authoritative) [asio.netdb:1], trying 0.0.0.0
2023-01-07 13:32:10.032  Status: WebServer(SSL) started on address: 0.0.0.0 with port 443
2023-01-07 13:32:10.036  Starting shared server on: 0.0.0.0:6144
2023-01-07 13:32:10.036  Status: TCPServer: shared server started...
2023-01-07 13:32:10.037  Status: RxQueue: queue worker started...
2023-01-07 13:32:12.038  Status: Darksky: Started...
2023-01-07 13:32:12.038  Status: NotificationSystem: thread started...
2023-01-07 13:32:12.039  Status: EventSystem: reset all events...
2023-01-07 13:32:12.041  Status: EventSystem: reset all device statuses...
2023-01-07 13:32:12.203  Status: Python EventSystem: Initializing event module.
2023-01-07 13:32:12.204  Status: EventSystem: Started
2023-01-07 13:32:12.204  Status: EventSystem: Queue thread started...
2023-01-07 13:32:12.239  Status: RFLink: Using serial port: /dev/ttyUSB_RFX
2023-01-07 13:32:12.284  Error: Traceback (most recent call last):
2023-01-07 13:32:12.284  Error:   File "script_general_Oct_2022", line 304, in <module>
2023-01-07 13:32:12.284  Error:   File "script_general_Oct_2022", line 252, in thermostat_chauffage_salon
2023-01-07 13:32:12.285  Error: NameError: name 'date_retour' is not defined
at 13:31 it was working fine and then it stops. When it restarted at 13:32 errors started to appear. I am not sure why it stops.
I know I have been updating my raspberry pi this week but I am not sure it was at this particular moment. I tried to investigate when it was using this page https://serverfault.com/questions/20747 ... th-apt-get and I ended up using the following command

Code: Select all

ls -lt --time-style="long-iso" /var/log/apt
I obtained the output

Code: Select all

-rw-r--r-- 1 root root   175 2023-01-08 16:50 history.log
-rw-r----- 1 root adm    940 2023-01-08 16:50 term.log
-rw-r--r-- 1 root root 68960 2023-01-08 16:50 eipp.log.xz
-rw-r--r-- 1 root root   476 2023-01-05 07:16 history.log.1.gz
-rw-r----- 1 root adm   1059 2023-01-05 07:16 term.log.1.gz
-rw-r--r-- 1 root root   402 2022-12-01 18:47 history.log.2.gz

It seems to be the list of dates when I updated my raspberry pi, but I am not totally sure. Can you confirm? If it is, then the date of the restart in the logfile and the date of update do not agree.
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: python code suddenly not working

Post by willemd »

lambertmular wrote: Monday 09 January 2023 10:57
at 13:31 it was working fine and then it stops. When it restarted at 13:32 errors started to appear. I am not sure why it stops.
I know I have been updating my raspberry pi this week but I am not sure it was at this particular moment.
I can't offer you a conclusion unfortunately but it seems you made a connection (incoming connecting ....) and then forced a restart. I assume it was for a reason .....?
lambertmular
Posts: 14
Joined: Wednesday 09 May 2018 11:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: python code suddenly not working

Post by lambertmular »

I assume it was for a reason .....?
I cannot remember making a connection this day. I initially thought it was the raspberry pi update but we saw that is was not on the same day. So I don't know

The only thing I can imagine is that I inadvertently updated my version of domoticz. I was avoiding the update since several months because it gave me an error (see this post viewtopic.php?t=37962&sid=bfc423208e79f ... ef46890633). However, it is easy to click on the update button by mistake and now I notice that domoticz does not propose me to update: so I guess I am in the last version, what do you think?
It is written on the domoticz page that I am in the
Version: 2022.2
Build Hash: eea9db734
Compile Date: 2022-11-05 12:05:35

If it is the last version, the good point is that the old error ("Segmentation fault") that I had does not appear anymore. Obviously the bad point is this new crazy error. What do you think? Many thanks for trying to help me
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: python code suddenly not working

Post by willemd »

Yes, that's the same latest version that I use. I tried running your small piece of code but apparently I don't have DomoticzEvents installed since the import fails.
lambertmular
Posts: 14
Joined: Wednesday 09 May 2018 11:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: python code suddenly not working

Post by lambertmular »

Any news? any help? I really need my code, it controls everything in my house and avoid the circuit breaker to trip.
Many thanks
User avatar
heggink
Posts: 972
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: python code suddenly not working

Post by heggink »

I recall a problem I had before with python plugins where the import acted weird every now and then. Dnpwwo suggested to debug by adding a try statement before the import but cannot recall what my resolution was. I think it eas down to a package update which messed up the import. Will look it up tomorrow hoping I can still find it.
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: python code suddenly not working

Post by willemd »

lambertmular wrote: Tuesday 10 January 2023 22:47 Any news? any help? I really need my code, it controls everything in my house and avoid the circuit breaker to trip.
Many thanks
Since the cause is not clear, I would take a new SD card, re-install everything, copy the dbase and scripts and see whether it is solved ...
User avatar
Didi
Posts: 15
Joined: Thursday 02 May 2019 21:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands
Contact:

Re: python code suddenly not working

Post by Didi »

Could you solve it using this command?

Code: Select all

sudo pip3 install requests
it seems you miss this module.
Main: RPI-5 / Zigbee / Sonoff / RFLink433 / Camera's / Wifi / Custom Interface / Alarm / GPS / MQTT / MiLight
lambertmular
Posts: 14
Joined: Wednesday 09 May 2018 11:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: python code suddenly not working

Post by lambertmular »

Could you solve it using this command?

Code: Select all

sudo pip3 install requests
it seems you miss this module.
It seems that I already have it installed

Code: Select all

pi@raspberrypi:~ $ sudo pip3 install requests
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (2.21.0)
pi@raspberrypi:~ $ 
Thanks for the help
lambertmular
Posts: 14
Joined: Wednesday 09 May 2018 11:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: python code suddenly not working

Post by lambertmular »

Since the cause is not clear, I would take a new SD card, re-install everything, copy the dbase and scripts and see whether it is solved ...
I did as you suggest: I took a different card, I installed "pi-lite", I installed domoticz, I installed python on the pi and when testing the same code as in the beginning of this post:

Code: Select all

import DomoticzEvents as DE
import re

es=list(map(float,re.findall("(\d+\.?\d+);(\d+);(\d+)",DE.Devices['Temp Chambre'].s_value)[0]))
DE.Log("Python:"+str(es))

def data_meteo(name):
    return list(map(float,re.findall("(\d+\.?\d+);(\d+);(\d+)",DE.Devices[name].s_value)[0]))
    
dat=data_meteo('Temp Chambre')

DE.Log("Python:"+str(dat))
I obtain again the same error :( :( :(

Code: Select all

2023-01-21 19:33:53.349 Status: EventSystem: reset all events...
2023-01-21 19:33:53.357 Error: Traceback (most recent call last):
2023-01-21 19:33:53.357 Error: File "Script #1", line 10, in <module>
2023-01-21 19:33:53.357 Error: File "Script #1", line 8, in data_meteo
2023-01-21 19:33:53.357 Error: NameError: name 're' is not defined 
I want to cry honestly!
Am I the only one to have this??? I cannot believe it
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: python code suddenly not working

Post by willemd »

Did you raise it as an issue on Github yet?
lambertmular
Posts: 14
Joined: Wednesday 09 May 2018 11:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: python code suddenly not working

Post by lambertmular »

Did you raise it as an issue on Github yet?
No, I did not know it was the next step in the process. Thanks.
However I am not sure how to do it, any advise? I will try tomorrow
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest