Default python script does not work

Python and python framework

Moderator: leecollings

Post Reply
soyxan
Posts: 1
Joined: Thursday 24 January 2019 10:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Default python script does not work

Post by soyxan »

Code: Select all

Version: 4.10179
Build Hash: f0985571
Compile Date: 2018-11-07 15:30:43
dzVents Version: 2.4.7
Python Version: 3.6.5 (default, Aug 22 2018, 14:21:14) [GCC 6.4.0]
I have been usen Blocky in domoticz for some time but I want to develop more complex scripts so I decided to use python as I have experience with it.

From the webpage I have added a new python script and even the default script that is automatically created does not work.

This is the script:

Code: Select all

"""
Domoticz passes information to python scripts through global variables and the
domoticz python module

The global variables in the script are:
 * changed_device: the current device that changed (object of Device)
 * changed_device_name: name of current device (same as changed_device.name)
 * is_daytime: boolean, true when it is is daytime
 * is_nighttime: same for the night
 * sunrise_in_minutes: integer
 * sunset_in_minutes: integer
 * user_variables: dictionary from string to value

A Device has a number of attributes and methods
The attributes are:
 * id
 * name
 * type
 * sub_type
 * switch_type
 * n_value
 * n_value_string
 * s_value
 * last_update_string
 * last_update: datetime object

The methods are:
 * def last_update_was_ago(self, **kwargs):
    Arguments can be: days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]
 * def is_on(self):
    returns True when device is on
 * def is_off(self):
    returns True when device is ooff

 * def on(self, after=None, reflect=False):
 	  turns device on, after is optional and are the number of seconds after which
    to turn the device on.
    If reflect is True, a next call to is_on will return True, while normally
    domoticz will first have to go through possible script before turning it on
 	def off(self, after=None, reflect=False):
 		simular to on()

uservariables and uservariables_lastupdate are arrays for all user variables:
 uservariables['yourvariablename'] = 'Test Value'
 uservariables_lastupdate['yourvariablename'] = '2015-12-27 11:19:22'

other useful details are contained in the following global variables:
 * is_daytime
 * is_nighttime
 * sunrise_in_minutes
 * sunset_in_minutes
 * (TODO) security

TODO: wiki page

Compare to Lua, instead of filling a commandArray, you change the status of a
device by calling device.on() or device.off()

TODO: setting variables

Calling Python's print function will not print to the domoticz console, see below
"""
import domoticz
import random

print ("This will only show up in the shell where you start domoticz");
domoticz.log("Hi Domoticz!")

domoticz.log("The device that got changed is: ", changed_device_name)
# changed_device.name is the same string

for name, device in domoticz.devices.items():
  domoticz.log("device", name, "is", "on" if device.is_on() else "off")
  if device.is_off():
      domoticz.log("could turn it on in 3 seconds")
      #device.on(after=3)
for name, value in user_variables.items():
  domoticz.log("var", name, "has value", value)
And this is the message I get in the log:

Code: Select all

2019-01-24 10:29:44.279 Error: EventSystem: Failed to execute python event script "script_device_python"
2019-01-24 10:29:44.279 Error: EventSystem: Traceback (most recent call last):
2019-01-24 10:29:44.279 Error: EventSystem: File "<string>", line 67, in <module>
2019-01-24 10:29:44.279 Error: EventSystem: File "/config/scripts/python/domoticz.py", line 42, in log
2019-01-24 10:29:44.280 Error: EventSystem: domoticz_.log(0, " ".join([str(k) for k in args]))
2019-01-24 10:29:44.280 Error: EventSystem: AttributeError: module 'DomoticzEvents' has no attribute 'log'
I have even tried to write this really simple script but I get an error in the log saying that " changed_device_name" is not being defined.

Code: Select all

import domoticz
domoticz.log("The device that got changed is: ", changed_device_name)
schj
Posts: 6
Joined: Saturday 29 December 2018 20:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Default python script does not work

Post by schj »

Same problem here.

I got it running by changing the domoticz.py module:

def log(*args):
domoticz_.Log(" ".join([str(k) for k in args]))

def error(*args):
domoticz_.Log(" ".join([str(k) for k in args]))


these used to have a 0 and 1 as first argument respectively.
Samael
Posts: 27
Joined: Saturday 12 January 2019 11:48
Target OS: Windows
Domoticz version: 4.9701
Contact:

Re: Default python script does not work

Post by Samael »

I modified default script as

Code: Select all

import DomoticzEvents as DE
import domoticz
import random

print ("This will only show up in the shell where you start domoticz");
domoticz.log("Hi Domoticz!")

domoticz.log("The device that got changed is: ", DE.changed_device_name)
# changed_device.name is the same string

for name, device in domoticz.devices.items():
  domoticz.log("device", name, "is", "on" if device.is_on() else "off")
  if device.is_off():
      domoticz.log("could turn it on in 3 seconds")
      #device.on(after=3)
for name, value in DE.user_variables.items():
  domoticz.log("var", name, "has value", value)
and modified \Domoticz\scripts\python\domoticz.py

Code: Select all

def log(*args):
	domoticz_.Log(" ".join([str(k) for k in args]))

def error(*args):
	domoticz_.Log("E>".join([str(k) for k in args]))
now it works as expected
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest