Python script from events console

Python and python framework

Moderator: leecollings

Post Reply
lucasub
Posts: 15
Joined: Monday 28 March 2016 12:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Python script from events console

Post by lucasub »

Hello, I am looking to write script from events console inside Domoticz but default example script not working.

I have look also for infomrations on Domoticz wiki but thare are not, there are only python plugin informations.

This is the question: how can switch On of Off a device with ID 250 and name TEST1

Where I can find description of domoticz and DomoticzEvents module?

This is default python plugin that not work:

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)


Thank you very much
Luca
lucasub
Posts: 15
Joined: Monday 28 March 2016 12:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python script from events console

Post by lucasub »

Hello, nobody know how command On / Off Switch with python Events directly in Domoticz interface?
Thank you

Luca
pipiche
Posts: 2016
Joined: Monday 02 April 2018 20:33
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: France
Contact:

Re: Python script from events console

Post by pipiche »

lucasub wrote: Thursday 01 October 2020 18:21 Hello, nobody know how command On / Off Switch with python Events directly in Domoticz interface?
Thank you

Luca
import DomoticzEvents as Dz

Dz.Command( 'Your device Name', "On") # To switch on
Dz.Command( 'Your device Name', "Off") # To switch off
lucasub
Posts: 15
Joined: Monday 28 March 2016 12:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python script from events console

Post by lucasub »

Thank you very much. Work well.
lucasub
Posts: 15
Joined: Monday 28 March 2016 12:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python script from events console

Post by lucasub »

Another question:
I set Light/Switch how dimmer.
To change state from "On" and "Off" the command is:
Dz.Command( 'Your device Name', "On") # To switch on
Dz.Command( 'Your device Name', "Off") # To switch off

But, how can change by Python Script on domoticz events all properties how Level, Image, and all other that you see on
http://domoticz_ip:port/json.htm?type=devices&plan=0

Thank you
Luca
lucasub
Posts: 15
Joined: Monday 28 March 2016 12:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python script from events console

Post by lucasub »

Hello, I cannot resolve the problem:
how I can change properties of device by python script ?

Example in my virtual device "dimmer" i need to set "MaxDimLevel" paramenter, how can set this by Python script?

Code: Select all

80:	
  AddjMulti	1
  AddjMulti2	1
  AddjValue	0
  AddjValue2	0
  BatteryLevel	255
  CustomImage	11
  Data	"Set Level: 90 %" 
  Description	""
  DimmerType	"abs"
  Favorite	1
  HardwareID	4
  HardwareName	"Device Generici"
  HardwareType	"Dummy (Does nothing, use for virtual switches only)"
  HardwareTypeVal	15
  HaveDimmer	true
  HaveGroupCmd	true
  HaveTimeout	false
  ID	"00014149"
  Image	"Water"
  IsSubDevice	false
  LastUpdate	"2020-10-24 08:38:40"
  Level	90
  LevelInt	90
  MaxDimLevel	100
  Name	"S1 SET UMIDITA"
  Notifications	"false"
  PlanID	"11"
  PlanIDs	
  0	11
  Protected	false
  ShowNotifications	true
  SignalLevel	"-"
  Status	"Set Level: 90 %"
  StrParam1	""
  StrParam2	""
  SubType	"Switch"
  SwitchType	"Dimmer"
  SwitchTypeVal	7
  Timers	"false"
  Type	"Light/Switch"
  TypeImg	"dimmer"
  Unit	1
  Used	1
  UsedByCamera	false
  XOffset	"610"
  YOffset	"969"
  idx	"249"
Thank you very much
Luca
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest