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

Activate macro when battery status in payload reaches 1?

Post by Henrik4223 »

Hi all

I have very little knowledge of pythone scripting and need help in making a script that can "read" the battery status of the below event and activate a macro that can send me a an e-mail/autoremote message when the battery status reaches 1? :?:
Attachments
Unavngivet.jpg
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 »

don't know how this will come out.

but this will trigger an event when the battery is at 1% so you can marry it to a macro.

it should work. i did some some cutting up of the orignal event string so you should have an event that is totally different than the original so you can't get them confused.

or i can just change the event prefix to something else and trigger that if you like but the suffix would be identical up to you

the latter of the 2 can be 1 line of code put into a python command instead of a script


first example

Code: Select all

battery = int(eg.event.payload.split(': ')[1][:1])
if battery == 1:
	event = eg.event.suffix.split(': ')
	devType = event[1][4:event[1][4:].find(' ')]
	devID = event[2][:event[2].find('a()')]
	devStat = event[3]
	eg.TriggerEvent(prefix=SensorBatteryWarning,suffix=devType+'.'+devStat+'-ID:'+devID)
second

Code: Select all

if int(eg.event.payload.split(': ')[1][:1]) == 1: eg.TriggerEvent(prefix=SensorBatteryWarning,suffix=eg.event.suffix)

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 »

Thank you very much for helping - I tried your script and command, and the event triggers the command and nothing happends - I guess this is because the battery is in the state "9" - but what do I have to change to test it ie. set the event to 9 insted of 1?
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 »

where you see the == 1

change the 1 to a 9


if you change the == to <=

that would be less than or equal to it will fire the event every time the battery status pops up

if it's at == it will only for the event if the level equals if its above or below nadda
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 »

Tried to test it with a python command and == 9 but get the below error message?
Attachments
Nyt bitmapbillede.jpg
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 »

oopsie


LOL


I for got some quotes. happens all the time.

Code: Select all

    battery = int(eg.event.payload.split(': ')[1][:1])
    if battery == 1:
       event = eg.event.suffix.split(': ')
       devType = event[1][4:event[1][4:].find(' ')]
       devID = event[2][:event[2].find('a()')]
       devStat = event[3]
       eg.TriggerEvent(prefix='SensorBatteryWarning',suffix=devType+'.'+devStat+'-ID:'+devID)

Code: Select all

if int(eg.event.payload.split(': ')[1][:1]) == 1: eg.TriggerEvent(prefix='SensorBatteryWarning',suffix=eg.event.suffix)
sorry dumb mistake, but it happens

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 »

BEAUTIFULL - THANK YOU VERY MUCH...So nice not to have to worry about battery change :-)

How do I modify the command to use it for the below event?:

07:55:27 RFXtrx.Type: Viking 02035,02038 id: 42752 ' temperature: +21.2 deg C humidity: 41 %RH status: comfort signal: 7 battery: 9'
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 »

ok i am going to write up just a general plugin because now that you have posted another device with a battery i noticed the order of the payload data differs.

so the other one has a possibility of breaking..
so the one i am going to key up for ya is going to be a lot more than a one liner, but it shouldn't break because of order of the payload.

going to take me a little bit to do it up


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 »

Fantastic - thanks 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 »

ok here goes.

you are going to add an event to a macro you put this in.

then edit the action. erase everything after the first period and put a *

it should look like this

RFXtrx.*

then add this code to the python code action

Code: Select all

# edit the 2 variables below to adjust the trigger points for the events
WARNING = 9
NOTICE = 15


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 <= WARNING else 'SensorBatteryNotice' \
                if battery <= NOTICE and battery > WARNING else False
    if prefix: eg.TriggerEvent(
                            prefix=prefix,
                            suffix=eg.event.suffix,
                            payload="current battery level is at "+deviceData['battery']+"%"
                            )
what this is going to do is any event that is coming from the plugin that is generating the prefix of RFXtrx will launch this script

this script will parse all payload data and then convert that data into a dictionary with key value pairs.

then it will check that dictionary to see if there is a key 'battery' and if there is it will then look at the value of battery and see if its within specified range

i have the ranges at the top of the script for you to easily edit it.



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 »

I have some challenges - Have I done it right?
Attachments
Error when running the script
Error when running the script
The macro I have created with the script
The macro I have created with the script
Unavngivet0.jpg (8.62 KiB) Viewed 6279 times
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 »

looks right

i need to see the whole event that is causing the error
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 »

Looks like this:

07:20:17 RFXtrx.Type: Viking 02811 id: 43008 ' temperature: +20.6 deg C signal: 7 battery: 9'
Henrik4223
Posts: 47
Joined: Fri Mar 04, 2016 10:18 am

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

Post by Henrik4223 »

Okay - I am to blame - the Battery alert works perfectly on my PIR sensor (look below) - but on the temperature sensor it does not (see below).

Any ideas - it seems that it has the same "battery" name in the event.
Attachments
Battery alert on temperaturesensor not working properly
Battery alert on temperaturesensor not working properly
Battery alert on PIR sensor working perfectly
Battery alert on PIR sensor working perfectly
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 »

ok add this line at the top.

eg.event.payload += ' '

Code: Select all

# edit the 2 variables below to adjust the trigger points for the events
WARNING = 9
NOTICE = 15

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 <= WARNING else 'SensorBatteryNotice' \
                if battery <= NOTICE and battery > WARNING else False
    if prefix: eg.TriggerEvent(
                            prefix=prefix,
                            suffix=eg.event.suffix,
                            payload="current battery level is at "+deviceData['battery']+"%"
                            )

copy and paste it there is a space between the single quotes.
i wasn't paying attention because my script uses the space between the items in the payload as a marker for when the value for an item has ended.

and guess what??

there is no space at the end of the last item.

so what that does is just adds the space to the end of the payload data

funny thing is i had it keyed in on my test over here i must have copied and pasted it missing that line.

K
If you like the work I have been doing then feel free to Image
Post Reply