Page 1 of 2

Can anyone lend a hand with payloads please?

Posted: Mon May 01, 2017 4:53 pm
by loveleejohn
I'm still new, so please go easy on me. lol. I have really made some progress with using the python action in eventghost! I am having some trouble grasping the concept of how to work with payloads in eventghost and I am wondering if anyone on here might be able to help. I've read several forums with people working with xbmc payloads and payloads when televisions connect but those have kind of confused me further. Here's an example of an event that appears in my log that I would like to work with.

Micasaverde.MainRoom.Phase1.First Motion Sensor.Sensor.Temperature.69 {'category': '4', 'comment': '', 'subcategory': '3', 'parent': '1', 'altid': '39', 'lasttrip': '1493644969', 'state': '-1', 'armedtripped': '0', 'room': '13', 'temperature': '69', 'batterylevel': '100', 'armed': '1', 'id': '491', 'tripped': '0', 'name': 'First Motion Sensor'}

This event shows the current temperature and the tripped or untripped state of the sensor. How do I 1. Trigger an event whenever the temperature is less than 80 but higher than 60 degrees? 2. Use the speech plugin to announce the current temperature when a macro is run. 3. write the value of the sensor's status (ie. tripped or untripped) to a variable when the event appears in the log.

Here's what I know for each tasks.

1. I know how to trigger the event by writing the following code in my python script.

Code: Select all

eg.TriggerEvent("my-magical-event")
I even taught myself how to write if statements so I could make this happen if a condition exists!

Code: Select all

if ("#payload = certain value"):
    eg.TriggerEvent("my-magical-event")
What I do not know how to do is how to capture that payload element. I also don't understand what I'm working with in the first place in regards to the payload. Is it a list, a dictionary, a class? Yeah... I know, pretty bad huh... :oops:

2. I know how to get the speech plugin to announce the information but again I don't understand the nuts and bolts of how to capture the actual content from the temperature part of the event's payload. I've tried many of the variants for capturing payload content but my mount is now filled with gritty yucky quicksand... :(

3. Admittedly I could use the event from the log file to just write a python command that says eg.globals.temperaturesensorstatus="tripped" but I would prefer to understand how to do it by using the actual payload of the event.

Thanks in advance for anyone who might be willing to help me sort these stepping stones out.

Re: Can anyone lend a hand with payloads please?

Posted: Mon May 01, 2017 9:36 pm
by jonib
loveleejohn wrote:What I do not know how to do is how to capture that payload element. I also don't understand what I'm working with in the first place in regards to the payload. Is it a list, a dictionary, a class? Yeah... I know, pretty bad huh... :oops:
You access the event payload in a Python script (if it's right below the event) with "eg.event.payload['temperature']" change 'temperature' for other data.
The curly braces {} indicate a dict in Python, square brackets [] indicate a list.

jonib

Re: Can anyone lend a hand with payloads please?

Posted: Mon May 01, 2017 10:54 pm
by loveleejohn
Hi jonib! Thanks for the pointer. I'll give it a whirl and see how it works and post back in a bit.

Re: Can anyone lend a hand with payloads please?

Posted: Tue May 02, 2017 2:01 am
by kgschlosser
ok payloads can be any python object data type.


if the payload has square brackets [] it is a list.. and think of it as a grocery list. with one item on each line.. the lines start at line number 0
so if you have ['apple', 'pear', 'peach'] and you want a pear. it would be eg.event.payload[1] now the items in the list can be any python object as well.

if you have a payload that has parenthesis () it is what is called a tuple. now a tuple if just like a list except it is not mutable. and what that means is that if you store you list in eg.globals.some_list then in one macro you assign that list to a variable the_list = eg.globals.some_list if you make a change in the_list it will also change in eg.globals.some_list if you use a tuple it will not change.

if you have a payload surrounded by curly braces {} this is a dictionary. and think of it just like that.. to find a definition in a dictionary you need something to look for a word for instance. that is called a key. and the definition is called a value. the key and value are separated by a colon : so to get the value you would do eg.event.payload['the_key']
dictionaries are mutable just like a list is

if the payload is surrounded by double quotes "" it is a string if it is surrounded by single quotes with a u in front u'' it is a unicode object (same thing as a string basically)

there is more to it then that but as a starting point this will get you where you need to go.

Re: Can anyone lend a hand with payloads please?

Posted: Tue May 02, 2017 2:06 am
by kgschlosser
Now if you are trying to make the speech plugin speak the forecast from the weather plugin. then you would do the following

eg.plugins.Speech.TextToSpeech(u'Microsoft Anna - English (United States)', 0, str(eg.event.payload), u'', 100, u'Speakers (5- High Definition Audio Device)')

Re: Can anyone lend a hand with payloads please?

Posted: Tue May 02, 2017 2:23 pm
by loveleejohn
Thanks for the detailed breakdown on those differences kg! The greatest challenge in working through these challenges is in communicating with the correct titles and that is very difficult when you don't know these so your explanation has helped understand what I'm working with. Now it seems that the weather plugin's payload is a dictionary if I'm understanding correctly. When I used the eventghost "Dump result to log" it produces several lines of data with everything from Location to Latitude to Temperature. Since it looked like a dictionary I tried printing the temperature to the log by creating a python script with contents print eg.event.payload['temperature'] but I'm getting results that say 'NoneType' object is unsubscriptable.

Re: Can anyone lend a hand with payloads please?

Posted: Tue May 02, 2017 2:24 pm
by loveleejohn
I also tried the speech command and the voice simply said "None"

Re: Can anyone lend a hand with payloads please?

Posted: Tue May 02, 2017 2:56 pm
by jonib
loveleejohn wrote:Since it looked like a dictionary I tried printing the temperature to the log by creating a python script with contents print eg.event.payload['temperature'] but I'm getting results that say 'NoneType' object is unsubscriptable.
eg.event.payload has data only when an event is activating a macro, I'm assuming you activated a Python script by itself and that won't work.

jonib

Re: Can anyone lend a hand with payloads please?

Posted: Tue May 02, 2017 3:23 pm
by loveleejohn
Thanks for the reply Jonib! Sorry I wasn't clear. I have three items in the macro, the first item is an action that pulls in the current conditions from kg's plugin, the second is an action that dumps the results to the log, and the third item is the python script I described above.

Re: Can anyone lend a hand with payloads please?

Posted: Tue May 02, 2017 3:33 pm
by jonib
loveleejohn wrote:Thanks for the reply Jonib! Sorry I wasn't clear. I have three items in the macro, the first item is an action that pulls in the current conditions from kg's plugin, the second is an action that dumps the results to the log, and the third item is the python script I described above.
To access data (result code) from another action you need to use "eg.result" not eg.event.payload as it's only available after an event.

jonib

Re: Can anyone lend a hand with payloads please?

Posted: Tue May 02, 2017 4:22 pm
by loveleejohn
Um... Jonib... Well let's just say I'm gonna go hide under a rock now to escape the embarassment! :oops: Lol. Thanks for catching my mistake there. Sadly the log is showing that the data captured by the plugin "does not have key temperature" so I'm guessing that I'm still doing something wrong and feeling bad after both you and kg have helped me so much in trying to understand this. I will just nibble away until I figure it out at some point. :)

Re: Can anyone lend a hand with payloads please?

Posted: Tue May 02, 2017 4:41 pm
by jonib
loveleejohn wrote:Um... Jonib... Well let's just say I'm gonna go hide under a rock now to escape the embarassment! :oops: Lol. Thanks for catching my mistake there.
Easy mistake to make. :wink:
Sadly the log is showing that the data captured by the plugin "does not have key temperature" so I'm guessing that I'm still doing something wrong and feeling bad after both you and kg have helped me so much in trying to understand this. I will just nibble away until I figure it out at some point. :)
What does the output from the plugin look like?

jonib

Re: Can anyone lend a hand with payloads please?

Posted: Tue May 02, 2017 5:26 pm
by loveleejohn
It's the message about not having "key temperature" which leaves me wondering where to find the keys. Oh wait, do I have to go to the plugin's file in my program files folder?

Re: Can anyone lend a hand with payloads please?

Posted: Tue May 02, 2017 6:02 pm
by jonib
loveleejohn wrote:It's the message about not having "key temperature" which leaves me wondering where to find the keys.
I meant the output of the action you are trying to use, not the error from your script.

jonib

Re: Can anyone lend a hand with payloads please?

Posted: Tue May 02, 2017 6:42 pm
by loveleejohn
Got ya. Here's what the plugin outputs to the log when I use the "dump result to log" action.
WU plugin output
WU plugin output