Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Activate macro when battery status in payload reaches 1?

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Henrik4223
Posts: 47
Joined: Fri Mar 04, 2016 10:18 am

Re: Activate macro when battery status in payload reaches 1?

Post by Henrik4223 »

That fixed it PERFECTLY - THANX a lot!!!!! :mrgreen:
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Activate macro when battery status in payload reaches 1?

Post by kgschlosser »

now if you look at what i have done is i have made it so that it captures all of the payload data and reformats it.

not just the battery.

so if you want to add to that script you can very easily

example:

say you have this event

RFXtrx.Type: Viking 02035,02038 id: 42752 ' temperature: +21.2 deg C humidity: 41 %RH status: comfort signal: 7 battery: 9'

and you want to now add an event for the humidity

Code: Select all

if 'humidity' in deviceData:
    humidity = int(deviceData['humidity'])
    prefix = 'HumidityAlert' if humidity > 50 else False
    if prefix: eg.TriggerEvent(
                            prefix=prefix,
                            suffix=eg.event.suffix,
                            payload="current Humidity at "+deviceData['humidity ']+"%"
                            )
you would just add on something like that to the bottom of the script

the if statement makes sure that the keyword or variable 'humidity' is in the holder for the payload data and if it's there then to proceed
the second statement makes a variable to hold the humidity data and because it's a number but has double quotes around it and we want to compair it with the < > symbols we have to turn it into a number from a string. that's why it is surrounded by an int() to turn it into a integer


next one is just another if statement but kinda backwards and tells the prefix variable what to put inside of it's self

so if the humidity is higher than 50 then put event prefix in it, if not put False

next line check to see if prefix variable has the prefix in it, or if it's False. and having a string of text or a number other than 0 and the True statement in it is True and if False or None or has a blank string or is a 0 it would be considered False

and since it is true to firre the event.

pretty much any variable is that way.

now for the temperature it has numbers and letters in it so it's a string and if we want to use it with a < > we have to strip off everything except for the number a + or - sign is considered a number

so say we have this here
temperature = "+21.2 deg C"

we would want to either remove the back parts and trash them or remove the front parts and keep them

i know it sounds the same but isn't

temperature = temperature[:5]
this statement keeps the front data
temperature = temperature[:-6
and this statement removes the back data

the end result is the same just 2 different ways of doing it.
i would personally keep the front data, because we know there is a + there so we are 100% sure to see the - so we don't have to account for a change in spacing for that, but what we don't know is if it hits a solid number will it have a .0 at the end or not...

so we can do something new for that one
temperature = temperature[:temperature.find(' ')-1]

this will put in that field the location of the first space in the string of text and since we want to go to one character before that space. we subtract 1 from it.

now we have a concrete way of knowing where the actual numeric portion of the data ends but we can always check too

now what i would do is

Code: Select all

try:
    temperature = float(temperature[:temperature.find(' ')-1]) 
except:
    temperature = False

this will slice the data and try and convert it to a float. if there is anything in the data that does not qualify it to be a float it will trigger an exception and that exception is caught (without you getting that ugly red error on the screen) and sets the variable to False instead

so lets see if you can make that script do something more with that information.

now remember watch the payload data for all the device to see what you may want to have performed if thing are at different levels
If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Activate macro when battery status in payload reaches 1?

Post by kgschlosser »

Code: Select all


NOTICE = 15
WARN = 10

eg.event.payload += ' '
data = eg.event.payload.split(':')

deviceData =  dict([[data[i][data[i].rfind(" "):].strip(), data[i+1][:data[i+1].rfind(" ")].strip()] for i in range(len(data)-1)])
if 'battery' in deviceData:
    battery = int(deviceData['battery'])
    prefix = 'SensorBatteryWarning' if battery <= WARN else 'SensorBatteryNotice' \
                if battery <= NOTICE and battery > WARN  else False
    if prefix: eg.TriggerEvent(
                            prefix=prefix,
                            suffix=eg.event.suffix,
                            payload="current battery level is at "+deviceData['battery']+"%"
                            )
i forgot to remove the print statements i had in it for testing.

so here it is with them removed
If you like the work I have been doing then feel free to Image
Henrik4223
Posts: 47
Joined: Fri Mar 04, 2016 10:18 am

Re: Activate macro when battery status in payload reaches 1?

Post by Henrik4223 »

Thank you very much for this detalied description ÔÇô as I have no knowledge of python it has been most usefull :mrgreen:

I am considering reading some more on python scripting (for beginners) ÔÇô do you have any good ideas on where I could find help for this? :?:
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Activate macro when battery status in payload reaches 1?

Post by kgschlosser »

Well you can ask here for some help

I started programming about a year ago with c++

And 6-8 months ago with Python

And what I did was read other code and if there is something I wanted to know about I would google it

A lot of the eventghost specific functions there is no documentation on but you can read the code for the eg function the files for the functions are named very similar to the function

I just took other plugins and started cutting up the code and a lot of reading to understand

Kinda stinks I found something I love to do so much at 40

My coding skills and ok not great I am not what is called a "pythonic" coder

The best part is when you make something and it takes like 100 lines and then in a month or 2 go back and look at it and you say to yourself "what the hell was I thinking" and you redo it and it's 1/2 the size I have done this couple of times now with my whole EG tree and all the plugins I made

If you don't know how to do something don't be afraid to ask I'll help ya with what I know
And Google is your friend (only if you learn how to use the booean)

K
If you like the work I have been doing then feel free to Image
Henrik4223
Posts: 47
Joined: Fri Mar 04, 2016 10:18 am

Re: Activate macro when battery status in payload reaches 1?

Post by Henrik4223 »

Well I am "only" 37 so I better get startet - will start up by changing some existing scripts that I already have in my settings and I will probably need help soon :mrgreen:
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Activate macro when battery status in payload reaches 1?

Post by krambriw »

Yes, asking in this forum is always welcome ;)

Here is a link with some good information for beginners

https://www.python.org/about/gettingstarted/
Henrik4223
Posts: 47
Joined: Fri Mar 04, 2016 10:18 am

Re: Activate macro when battery status in payload reaches 1?

Post by Henrik4223 »

Thanks :P
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Activate macro when battery status in payload reaches 1?

Post by kgschlosser »

i like python better than c++ for the simple thing of the function names are user friendly.

and you can change a variable type easily. and there isn't all the extra bs of declaring your variables

example:

in c++ a command to convert an array of characters to an integer is atoi or atol

and once you do that the variable that was holding the array sits there taking up memory until you are done with the function

in python it's simple

var = '12345'
var = int(var)
and that's all she wrote

you don't have to remember all the cooky short function names line strncpy or strncat
to copy or concatenate a string ot strcpy or strcat to do the whole line

or to compair a string to another string

if (!strncmp(event,"USBPOWER", 8)) {
do some code
}

that's c++

and python

if event[:8] == 'USBPOWER':
do some code

it is a lot of trial and error learning something new i go through it all the time. i keep on plugging away at it. but you get to see the joy of something you made
i just started messing around with drawing the config panels i do have to say it's highly aggravating but when it finally works ya feel pretty good about it.
If you like the work I have been doing then feel free to Image
Post Reply