[solved] python script triggered by any/random events

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:

[solved] python script triggered by any/random events

Post by kofec »

My version:

Code: Select all

2019-03-04 19:41:59.050 Status: Domoticz V4.10531 (c)2012-2019 GizMoCuz
2019-03-04 19:41:59.051 Status: Build Hash: bf19d8bb, Date: 2019-02-27 09:04:05
2019-03-04 19:41:59.052 Status: Startup Path: /usr/share/domoticz/
2019-03-04 19:41:59.153 Status: EventSystem: reset all events...
2019-03-04 19:41:59.385 Status: PluginSystem: Started, Python version '3.7.2'.
I have created a simple script to test "time" and "event" python scripts.

Code: Select all

root@Xiaomi3G:/etc/domoticz/scripts/python# cat script_device_Przelacznik.py
import DomoticzEvents as DEVS
DEVS.Log("Python: Changed: " + DEVS.changed_device.Describe())

DEVS.Log("DEVS.changed_device_name: " + str(DEVS.changed_device_name))
DEVS.Log("DEVS.changed_device.name: " + "ID: " + str(DEVS.changed_device.id) + " Name: " + str(DEVS.changed_device.name))

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

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

DEVS.Log("DIR(dir(DEVS.changed_device)): " + str(dir(DEVS.changed_device)))
DEVS.Log("DIR(DEVS): " + str(dir(DEVS)))
for device in DEVS.Devices:
   if device:
       DEVS.Log("device: " + str(device) + " Id: " + str(DEVS.Devices[device].id) + " n_value_string: " + str(
           DEVS.Devices[device].n_value_string) + " s_value: " + str(
           DEVS.Devices[device].s_value) + " Last updated: " +
                str(DEVS.Devices[device].last_update_string) + " All typs:" + str(
           DEVS.Devices[device].sub_type) + " " + str(DEVS.Devices[device].switch_type) + " " + str(
           DEVS.Devices[device].type))

root@Xiaomi3G:/etc/domoticz/scripts/python#
I have added one dummy switch to test it. I noticed that this scrept was executed many time by any other devices like CPU usage or Memory usage by also by other devices.

Regarding the issue and question 1:
https://github.com/domoticz/domoticz/issues/2097
The variable changed_device and changed_device_name are not present in "time" script only in action right ?
@teeminus

Code: Select all

2019-03-04 19:39:14.850  Python: Changed: ID: 5 Name: HDD /overlay, Type: 243, subType: 6, switchType: 0, s_value: 19.42, n_value: 0, n_value_string: 19.42, last_update_string: 2019-03-04 19:39:14
domoticz: DEVS.changed_device_name: HDD /overlay
2019-03-04 19:39:14.851  DEVS.changed_device_name: HDD /overlay
domoticz: DEVS.changed_device.name: ID: 5 Name: HDD /overlay
2019-03-04 19:39:14.852  DEVS.changed_device.name: ID: 5 Name: HDD /overlay
domoticz: Python: Number of user_variables: 0
2019-03-04 19:39:14.853  Python: Number of user_variables: 0
domoticz: DIR(dir(DEVS.changed_device)): ['Describe', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'id', 'last_update_string', 'n_value', 'n_value_string', 'name', 's_value', 'sub_type', 'switch_type', 'type']
2019-03-04 19:39:14.854  DIR(dir(DEVS.changed_device)): ['Describe', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'id', 'last_update_string', 'n_value', 'n_value_string', 'name', 's_value', 'sub_type', 'switch_type', 'type']
domoticz: DIR(DEVS): ['Command', 'Devices', 'Log', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'changed_device', 'changed_device_name', 'is_daytime', 'is_nighttime', 'minutes_since_midnight', 'sunrise_in_minutes', 'sunset_in_minutes', 'user_variables']
2019-03-04 19:39:14.855  DIR(DEVS): ['Command', 'Devices', 'Log', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'changed_device', 'changed_device_name', 'is_daytime', 'is_nighttime', 'minutes_since_midnight', 'sunrise_in_minutes', 'sunset_in_minutes', 'user_variables']
domoticz: device: CPU_Usage Id: 1 n_value_string: 0.56 s_value: 0.56 Last updated: 2019-03-04 19:39:14 All typs:6 0 243
2019-03-04 19:39:14.857  device: CPU_Usage Id: 1 n_value_string: 0.56 s_value: 0.56 Last updated: 2019-03-04 19:39:14 All typs:6 0 243
domoticz: device: Memory Usage Id: 2 n_value_string: 19.47 s_value: 19.47 Last updated: 2019-03-04 19:38:44 All typs:6 0 243
2019-03-04 19:39:14.858  device: Memory Usage Id: 2 n_value_string: 19.47 s_value: 19.47 Last updated: 2019-03-04 19:38:44 All typs:6 0 243
domoticz: device: Process Usage Id: 3 n_value_string: 13.9300 s_value: 13.9300 Last updated: 2019-03-04 19:38:44 All typs:31 0 243
2019-03-04 19:39:14.859  device: Process Usage Id: 3 n_value_string: 13.9300 s_value: 13.9300 Last updated: 2019-03-04 19:38:44 All typs:31 0 243
domoticz: device: HDD /overlay Id: 5 n_value_string: 19.42 s_value: 19.42 Last updated: 2019-03-04 19:39:14 All typs:6 0 243
2019-03-04 19:39:14.860  device: HDD /overlay Id: 5 n_value_string: 19.42 s_value: 19.42 Last updated: 2019-03-04 19:39:14 All typs:6 0 243
domoticz: device: Podlipie_Pinger Id: 6 n_value_string: On s_value: 0 Last updated: 2019-03-03 09:55:08 All typs:0 0 17
2019-03-04 19:39:14.861  device: Podlipie_Pinger Id: 6 n_value_string: On s_value: 0 Last updated: 2019-03-03 09:55:08 All typs:0 0 17
domoticz: device: Ludynia Pinger Id: 7 n_value_string: On s_value: 0 Last updated: 2019-03-03 22:15:20 All typs:0 0 17
2019-03-04 19:39:14.862  device: Ludynia Pinger Id: 7 n_value_string: On s_value: 0 Last updated: 2019-03-03 22:15:20 All typs:0 0 17
domoticz: device: Glowackiego Pinger Id: 8 n_value_string: Off s_value: 0 Last updated: 2019-03-04 06:02:12 All typs:0 0 17
2019-03-04 19:39:14.863  device: Glowackiego Pinger Id: 8 n_value_string: Off s_value: 0 Last updated: 2019-03-04 06:02:12 All typs:0 0 17
domoticz: device: PiotrAP_Pinger Id: 9 n_value_string: On s_value: 0 Last updated: 2019-03-04 16:32:39 All typs:0 0 17
2019-03-04 19:39:14.864  device: PiotrAP_Pinger Id: 9 n_value_string: On s_value: 0 Last updated: 2019-03-04 16:32:39 All typs:0 0 17
domoticz: device: nBox Pinger Id: 10 n_value_string: On s_value: 0 Last updated: 2019-02-27 14:36:23 All typs:0 0 17
2019-03-04 19:39:14.865  device: nBox Pinger Id: 10 n_value_string: On s_value: 0 Last updated: 2019-02-27 14:36:23 All typs:0 0 17
domoticz: device: Glowackiego WG1 Pinger Id: 11 n_value_string: Off s_value: 0 Last updated: 2019-03-04 05:59:53 All typs:0 0 17
2019-03-04 19:39:14.866  device: Glowackiego WG1 Pinger Id: 11 n_value_string: Off s_value: 0 Last updated: 2019-03-04 05:59:53 All typs:0 0 17
domoticz: device: Przelacznik Id: 12 n_value_string: On s_value: 0 Last updated: 2019-03-04 19:14:08 All typs:73 0 244
2019-03-04 19:39:14.867  device: Przelacznik Id: 12 n_value_string: On s_value: 0 Last updated: 2019-03-04 19:14:08 All typs:73 0 244
domoticz: Python: Changed: ID: 1 Name: CPU_Usage, Type: 243, subType: 6, switchType: 0, s_value: 0.49, n_value: 0, n_value_string: 0.49, last_update_string: 2019-03-04 19:39:45
2019-03-04 19:39:45.417  Python: Changed: ID: 1 Name: CPU_Usage, Type: 243, subType: 6, switchType: 0, s_value: 0.49, n_value: 0, n_value_string: 0.49, last_update_string: 2019-03-04 19:39:45
domoticz: DEVS.changed_device_name: CPU_Usage
2019-03-04 19:39:45.418  DEVS.changed_device_name: CPU_Usage
domoticz: DEVS.changed_device.name: ID: 1 Name: CPU_Usage
2019-03-04 19:39:45.419  DEVS.changed_device.name: ID: 1 Name: CPU_Usage
domoticz: Python: Number of user_variables: 0
2019-03-04 19:39:45.419  Python: Number of user_variables: 0
domoticz: DIR(dir(DEVS.changed_device)): ['Describe', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'id', 'last_update_string', 'n_value', 'n_value_string', 'name', 's_value', 'sub_type', 'switch_type', 'type']
2019-03-04 19:39:45.421  DIR(dir(DEVS.changed_device)): ['Describe', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'id', 'last_update_string', 'n_value', 'n_value_string', 'name', 's_value', 'sub_type', 'switch_type', 'type']
domoticz: DIR(DEVS): ['Command', 'Devices', 'Log', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'changed_device', 'changed_device_name', 'is_daytime', 'is_nighttime', 'minutes_since_midnight', 'sunrise_in_minutes', 'sunset_in_minutes', 'user_variables']
2019-03-04 19:39:45.422  DIR(DEVS): ['Command', 'Devices', 'Log', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'changed_device', 'changed_device_name', 'is_daytime', 'is_nighttime', 'minutes_since_midnight', 'sunrise_in_minutes', 'sunset_in_minutes', 'user_variables']
domoticz: device: CPU_Usage Id: 1 n_value_string: 0.49 s_value: 0.49 Last updated: 2019-03-04 19:39:45 All typs:6 0 243
2019-03-04 19:39:45.423  device: CPU_Usage Id: 1 n_value_string: 0.49 s_value: 0.49 Last updated: 2019-03-04 19:39:45 All typs:6 0 243
domoticz: device: Memory Usage Id: 2 n_value_string: 19.47 s_value: 19.47 Last updated: 2019-03-04 19:38:44 All typs:6 0 243
2019-03-04 19:39:45.425  device: Memory Usage Id: 2 n_value_string: 19.47 s_value: 19.47 Last updated: 2019-03-04 19:38:44 All typs:6 0 243
domoticz: device: Process Usage Id: 3 n_value_string: 13.9300 s_value: 13.9300 Last updated: 2019-03-04 19:38:44 All typs:31 0 243
2019-03-04 19:39:45.426  device: Process Usage Id: 3 n_value_string: 13.9300 s_value: 13.9300 Last updated: 2019-03-04 19:38:44 All typs:31 0 243
domoticz: device: HDD /overlay Id: 5 n_value_string: 19.42 s_value: 19.42 Last updated: 2019-03-04 19:39:14 All typs:6 0 243
2019-03-04 19:39:45.427  device: HDD /overlay Id: 5 n_value_string: 19.42 s_value: 19.42 Last updated: 2019-03-04 19:39:14 All typs:6 0 243
domoticz: device: Podlipie_Pinger Id: 6 n_value_string: On s_value: 0 Last updated: 2019-03-03 09:55:08 All typs:0 0 17
2019-03-04 19:39:45.428  device: Podlipie_Pinger Id: 6 n_value_string: On s_value: 0 Last updated: 2019-03-03 09:55:08 All typs:0 0 17
domoticz: device: Ludynia Pinger Id: 7 n_value_string: On s_value: 0 Last updated: 2019-03-03 22:15:20 All typs:0 0 17
2019-03-04 19:39:45.429  device: Ludynia Pinger Id: 7 n_value_string: On s_value: 0 Last updated: 2019-03-03 22:15:20 All typs:0 0 17
domoticz: device: Glowackiego Pinger Id: 8 n_value_string: Off s_value: 0 Last updated: 2019-03-04 06:02:12 All typs:0 0 17
2019-03-04 19:39:45.430  device: Glowackiego Pinger Id: 8 n_value_string: Off s_value: 0 Last updated: 2019-03-04 06:02:12 All typs:0 0 17
domoticz: device: PiotrAP_Pinger Id: 9 n_value_string: On s_value: 0 Last updated: 2019-03-04 16:32:39 All typs:0 0 17
2019-03-04 19:39:45.431  device: PiotrAP_Pinger Id: 9 n_value_string: On s_value: 0 Last updated: 2019-03-04 16:32:39 All typs:0 0 17
domoticz: device: nBox Pinger Id: 10 n_value_string: On s_value: 0 Last updated: 2019-02-27 14:36:23 All typs:0 0 17
2019-03-04 19:39:45.432  device: nBox Pinger Id: 10 n_value_string: On s_value: 0 Last updated: 2019-02-27 14:36:23 All typs:0 0 17
domoticz: device: Glowackiego WG1 Pinger Id: 11 n_value_string: Off s_value: 0 Last updated: 2019-03-04 05:59:53 All typs:0 0 17
2019-03-04 19:39:45.433  device: Glowackiego WG1 Pinger Id: 11 n_value_string: Off s_value: 0 Last updated: 2019-03-04 05:59:53 All typs:0 0 17
domoticz: device: Przelacznik Id: 12 n_value_string: On s_value: 0 Last updated: 2019-03-04 19:14:08 All typs:73 0 244
2019-03-04 19:39:45.435  device: Przelacznik Id: 12 n_value_string: On s_value: 0 Last updated: 2019-03-04 19:14:08 All typs:73 0 244
domoticz: Python: Changed: ID: 2 Name: Memory Usage, Type: 243, subType: 6, switchType: 0, s_value: 19.47, n_value: 0, n_value_string: 19.47, last_update_string: 2019-03-04 19:40:05
2019-03-04 19:40:05.824  Python: Changed: ID: 2 Name: Memory Usage, Type: 243, subType: 6, switchType: 0, s_value: 19.47, n_value: 0, n_value_string: 19.47, last_update_string: 2019-03-04 19:40:05
domoticz: DEVS.changed_device_name: Memory Usage
2019-03-04 19:40:05.826  DEVS.changed_device_name: Memory Usage
domoticz: DEVS.changed_device.name: ID: 2 Name: Memory Usage
2019-03-04 19:40:05.827  DEVS.changed_device.name: ID: 2 Name: Memory Usage
domoticz: Python: Number of user_variables: 0
2019-03-04 19:40:05.828  Python: Number of user_variables: 0
domoticz: DIR(dir(DEVS.changed_device)): ['Describe', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'id', 'last_update_string', 'n_value', 'n_value_string', 'name', 's_value', 'sub_type', 'switch_type', 'type']
2019-03-04 19:40:05.829  DIR(dir(DEVS.changed_device)): ['Describe', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'id', 'last_update_string', 'n_value', 'n_value_string', 'name', 's_value', 'sub_type', 'switch_type', 'type']
domoticz: DIR(DEVS): ['Command', 'Devices', 'Log', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'changed_device', 'changed_device_name', 'is_daytime', 'is_nighttime', 'minutes_since_midnight', 'sunrise_in_minutes', 'sunset_in_minutes', 'user_variables']
2019-03-04 19:40:05.831  DIR(DEVS): ['Command', 'Devices', 'Log', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'changed_device', 'changed_device_name', 'is_daytime', 'is_nighttime', 'minutes_since_midnight', 'sunrise_in_minutes', 'sunset_in_minutes', 'user_variables']
domoticz: device: CPU_Usage Id: 1 n_value_string: 0.49 s_value: 0.49 Last updated: 2019-03-04 19:39:45 All typs:6 0 243
2019-03-04 19:40:05.832  device: CPU_Usage Id: 1 n_value_string: 0.49 s_value: 0.49 Last updated: 2019-03-04 19:39:45 All typs:6 0 243
domoticz: device: Memory Usage Id: 2 n_value_string: 19.47 s_value: 19.47 Last updated: 2019-03-04 19:40:05 All typs:6 0 243
2019-03-04 19:40:05.834  device: Memory Usage Id: 2 n_value_string: 19.47 s_value: 19.47 Last updated: 2019-03-04 19:40:05 All typs:6 0 243
domoticz: device: Process Usage Id: 3 n_value_string: 13.9300 s_value: 13.9300 Last updated: 2019-03-04 19:38:44 All typs:31 0 243
2019-03-04 19:40:05.835  device: Process Usage Id: 3 n_value_string: 13.9300 s_value: 13.9300 Last updated: 2019-03-04 19:38:44 All typs:31 0 243
domoticz: device: HDD /overlay Id: 5 n_value_string: 19.42 s_value: 19.42 Last updated: 2019-03-04 19:39:14 All typs:6 0 243
2019-03-04 19:40:05.837  device: HDD /overlay Id: 5 n_value_string: 19.42 s_value: 19.42 Last updated: 2019-03-04 19:39:14 All typs:6 0 243
domoticz: device: Podlipie_Pinger Id: 6 n_value_string: On s_value: 0 Last updated: 2019-03-03 09:55:08 All typs:0 0 17
2019-03-04 19:40:05.838  device: Podlipie_Pinger Id: 6 n_value_string: On s_value: 0 Last updated: 2019-03-03 09:55:08 All typs:0 0 17
domoticz: device: Ludynia Pinger Id: 7 n_value_string: On s_value: 0 Last updated: 2019-03-03 22:15:20 All typs:0 0 17
2019-03-04 19:40:05.839  device: Ludynia Pinger Id: 7 n_value_string: On s_value: 0 Last updated: 2019-03-03 22:15:20 All typs:0 0 17
domoticz: device: Glowackiego Pinger Id: 8 n_value_string: Off s_value: 0 Last updated: 2019-03-04 06:02:12 All typs:0 0 17
2019-03-04 19:40:05.841  device: Glowackiego Pinger Id: 8 n_value_string: Off s_value: 0 Last updated: 2019-03-04 06:02:12 All typs:0 0 17
domoticz: device: PiotrAP_Pinger Id: 9 n_value_string: On s_value: 0 Last updated: 2019-03-04 16:32:39 All typs:0 0 17
2019-03-04 19:40:05.842  device: PiotrAP_Pinger Id: 9 n_value_string: On s_value: 0 Last updated: 2019-03-04 16:32:39 All typs:0 0 17
domoticz: device: nBox Pinger Id: 10 n_value_string: On s_value: 0 Last updated: 2019-02-27 14:36:23 All typs:0 0 17
2019-03-04 19:40:05.843  device: nBox Pinger Id: 10 n_value_string: On s_value: 0 Last updated: 2019-02-27 14:36:23 All typs:0 0 17
domoticz: device: Glowackiego WG1 Pinger Id: 11 n_value_string: Off s_value: 0 Last updated: 2019-03-04 05:59:53 All typs:0 0 17
2019-03-04 19:40:05.845  device: Glowackiego WG1 Pinger Id: 11 n_value_string: Off s_value: 0 Last updated: 2019-03-04 05:59:53 All typs:0 0 17
domoticz: device: Przelacznik Id: 12 n_value_string: On s_value: 0 Last updated: 2019-03-04 19:14:08 All typs:73 0 244
2019-03-04 19:40:05.846  device: Przelacznik Id: 12 n_value_string: On s_value: 0 Last updated: 2019-03-04 19:14:08 All typs:73 0 244
domoticz: Python: Changed: ID: 3 Name: Process Usage, Type: 243, subType: 31, switchType: 0, s_value: 13.9600, n_value: 0, n_value_string: 13.9600, last_update_string: 2019-03-04 19:40:05
Last edited by kofec on Thursday 07 March 2019 16:06, edited 1 time in total.
moroen
Posts: 408
Joined: Sunday 15 January 2017 11:06
Target OS: Linux
Domoticz version: beta
Location: Norway
Contact:

Re: python script triggered by any/random events

Post by moroen »

This is the intended behaviour, in order to enable creating scripts that triggers from multiple devices. The basic idea is to check the changed_device variable and exit the script if it's not one of the devices intended to trigger this particular script.

Code: Select all

triggerDev = DE.changed_device

if triggerDev.name == "...":
If you really need a script pr. device, it's possible to make a script that checks the changed_device object and calls the correct script.

Regards,
M
kofec
Posts: 53
Joined: Friday 16 September 2016 14:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: python script triggered by any/random events

Post by kofec »

Thanks - so I close the subject
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest