Help with numbers in payload

If you have a question or need help, this is the place to be.
Post Reply
therealbiglou
Experienced User
Posts: 141
Joined: Sat May 19, 2012 4:33 am

Help with numbers in payload

Post by therealbiglou » Fri Jun 05, 2020 2:01 am

I'm trying to extract the number from a payload and use it in an if statement:

Code: Select all

watts = eg.event.payload
if watts > 1:
    eg.TriggerEvent("Great Room TV Power Usage On")
However, no matter what the value in the payload--even less than 1--it triggers the event. If I print the payload, I see something like:

['83.69']

Am I doing something wrong? Should this payload be converted to a number somehow to compare in the script?

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

Re: Help with numbers in payload

Post by kgschlosser » Fri Jun 05, 2020 6:05 am

as a matter of fact you are..

the payload is a list. you can tell this because of the []'s.
Think of a list of grocery items. when you make a list you put one item on each line. well the same thing goes with these lists. the only difference is on a grocery list you start off with line 1. in a python list you start off with line 0.

so how we tell python what item we want is by adding [line number] to the end of the variable name.

Code: Select all

eg.event.payload[0]
Now there is also one other thing. do you see the single quotes surrounding the number in the list?? well that means it is not actually a number it is a string of text. Now technically even if it was a "number" it's really not a number it is a float a float has a decimal point in it. Python as given us an easy way to convert the string into an actual float.

Code: Select all

watts = eg.event.payload[0]
watts = float(watts)
If you like the work I have been doing then feel free to Image

therealbiglou
Experienced User
Posts: 141
Joined: Sat May 19, 2012 4:33 am

Re: Help with numbers in payload

Post by therealbiglou » Sun Jun 07, 2020 9:00 pm

Brilliant, that worked perfectly! Thanks!

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

Re: Help with numbers in payload

Post by kgschlosser » Sun Jun 07, 2020 9:54 pm

no worries m8. That's why I am here. If you are wanting to further your python skills I am more then happy to help. So ask as many questions as you like.
If you like the work I have been doing then feel free to Image

Post Reply