Python script outputs

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
JoeInBend
Posts: 10
Joined: Mon Mar 10, 2008 2:59 pm

Python script outputs

Post by JoeInBend » Wed May 06, 2009 10:03 pm

Hi all,
I posed a question about writing a specific plugin, havent gotten any takers on it and that's okay, but how about if I ask a more direct question about Python scripting:

I have some example code for some hardware I am interfacing with. I am able to dump it into a Python Script Event, and it reads my sensor data, and dumps it out to the EG log (lefthand pane of the app), but it is dumping as informational, so it's not really usable in terms of triggering events from that data. This data is coming from "Print" statements in the code. Can I use something other than "print", to have it then generate an event string in EG? Here's an example of what I am working with:

In the foreground is part of the Python script, and you can see in the background log, that it is outputting the data
Image

But, rather than just "print" it, how can i get that data to be actual events?

stottle
Plugin Developer
Posts: 636
Joined: Sun Apr 26, 2009 10:59 pm

Re: Python script outputs

Post by stottle » Wed May 06, 2009 11:48 pm

I'm still learning EG, so I could be wrong. From a plugin, you create events with the TriggerEvent(string) call. I'm not sure if there is a way to create an event outside of a plugin (?). Sorry I can't be more help, but I thought knowing the command would help you find the answer.

Good luck,
Brett

User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Python script outputs

Post by jinxdone » Thu May 07, 2009 6:33 am

Yep, eg.TriggerEvent(event, [payload]) is the function for triggering an event. The payload is optional.

Some further reading..
http://www.eventghost.org/docs/eg/index.html#functions
http://docs.python.org/reference/

JoeInBend
Posts: 10
Joined: Mon Mar 10, 2008 2:59 pm

Re: Python script outputs

Post by JoeInBend » Thu May 07, 2009 3:01 pm

Most likely the problem with me understanding your response is my lack of dev experience :) But, let me pose my question a little more concisely to see if I get something back that I understand... a picture is worth a thousand words, so here we go again:

In the shot below, marked in red, the print statement is giving me the output shown on the left, which is correct. The problem I am having, is the type of output that EG is seeing it as. With the "i bubble" next to it, it's just informational, and you cannot use these lines to trigger events/actions/macros. Below that is an example of the type of output I would like to see my script output to EG, with the "lightening bolt" marked in blue. Is this just a matter of preempting the line in the script with something other than "print" in order for it to deliver the output I need as a usable line in EG? If it were to give me the exact same thing as I am getting now, except display it as "(lightening bolt) Sensor 0:290" then I'd be set!!

Image

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Python script outputs

Post by krambriw » Thu May 07, 2009 4:00 pm

instead of using "print...", try

eg.TriggerEvent("Sensor %i: %i" % (e.index, e.value))

Best regards, Walter

JoeInBend
Posts: 10
Joined: Mon Mar 10, 2008 2:59 pm

Re: Python script outputs

Post by JoeInBend » Thu May 07, 2009 4:55 pm

YES!! That did the trick!! The sensor values are now showing up as Events instead of Informational!! Thank you Walter!!

Image

JoeInBend
Posts: 10
Joined: Mon Mar 10, 2008 2:59 pm

Re: Python script outputs

Post by JoeInBend » Thu May 07, 2009 5:19 pm

Alrighty well I'm almost to where I need to be, but I've ran into another roadblock. The "raw" outputs of the sensor values I am reading must be ran through a calculation before they produce meaningful data. in my example:

eg.TriggerEvent("Sensor %i: %i" % (e.index, e.value))

%i outputs the raw data from the sensor, in this case "285". This needs to run through an equation so it will produce a number indicating degrees in Celsius. calculation is:

[(sensor value/1000) x 222.22] - 61.111

So, I tried plugging this into the TriggerEvent, but all it does in the log is output the actual text of the equation, it doesnt calculate it:

eg.TriggerEvent("Sensor %i: [(%i/1000)*222.22]-61.111" % (e.index, e.value))

I tried various renditions of [ and ( to see if I could get it to fire off, but each time it just outputs the full string instead of the calculation.

Ideas?

--- EDIT: ---
Nevermind, figured it out :) I did not have my brackets correct, nor my spacing correct. It's dumping correct values now! Thanks again for everyone's help!

User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Python script outputs

Post by jinxdone » Thu May 07, 2009 7:43 pm

You could also use variables instead of trying to do all the calculation in one long line, that usually makes it more readable. Also it's usually a good idea to put in as much comments as possible to describe why you wrote a piece of code. When you come to look at your code a year from now you'll thank yourself. :wink:

You can also take use of the payload of the events if that seems appropriate. For example:

Code: Select all

eg.TriggerEvent("Sensor %i" % (e.index), e.value)
The payload value will be accessible in the eg.event.payload variable when you trigger something with that event.

-jinxdone

Post Reply