Cannot add method in python script

Python and python framework

Moderator: leecollings

Post Reply
kofec
Posts: 53
Joined: Friday 16 September 2016 14:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Cannot add method in python script

Post by kofec »

For python script (time based) I would like to add method and this method didn't see DEVICE object e.g.:

Code: Select all

import DomoticzEvents as DE

DE.Log("Python: Changed: " + DE.changed_device.Describe())

def DisplaySomething():
    DE.Log("Python: Test")

if DE.changed_device_name == "Test":
    if DE.Devices["Test_Target"].n_value_string == "On":
        DE.Command("Test_Target", "Off")

    if DE.Devices["Test_Target"].n_value_string == "Off":
        DE.Command("Test_Target", "On")

DE.Log("Python: Number of user_variables: " + str(len(DE.user_variables)))

# All user_variables are treated as strings, convert as necessary
for key, value in DE.user_variables.items():
    DE.Log("Python: User-variable '{0}' has value: {1}".format(key, value))

DisplaySomething()

# Description of Device_object should go here...
Error: name 'DE' is not define

If this error specific to my environment or generic ? It was working fine in previous versions
Last edited by kofec on Monday 19 September 2022 11:26, edited 2 times 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: Cannot add method in python script

Post by waltervl »

If I use the time based demo script as shown in the last paragraph of the wiki page https://www.domoticz.com/wiki/Python_Events
I get a notification every minute (as expected). I am on 2202.1 I also tried this on latest beta and there it is also running.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
kofec
Posts: 53
Joined: Friday 16 September 2016 14:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Cannot add method in python script

Post by kofec »

Hi,
Thanks for quick replay. I made a misteke in the example. Can you add any method using "DE" object in the code e.g:

def DisplaySomething():
DE.Log("Python: Test")

And call this method in the code
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Cannot add method in python script

Post by waltervl »

Indeed, just tested in latest beta 14498:

2022-09-19 20:27:00.432 Error: Traceback (most recent call last):
2022-09-19 20:27:00.432 Error: File "python1", line 11, in <module>
2022-09-19 20:27:00.432 Error: File "python1", line 9, in DisplaySomething
2022-09-19 20:27:00.432 Error: NameError: name 'DE' is not defined

In current stable 2022.1 it is no issue, the same script runs without problems.

There were a lot of changes in beta on Python framework due to incompatibility with newer Linux OS (eg bullseye).
I would advise you to make a github issue for this on the domoticz repository....
Ah, I see somebody already did something: https://github.com/domoticz/domoticz/issues/5339
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest