Python: using variable in variable name

If you have a question or need help, this is the place to be.
Post Reply
FourFootPaul
Posts: 10
Joined: Fri Jun 15, 2012 12:05 pm

Python: using variable in variable name

Post by FourFootPaul » Wed May 10, 2017 4:27 pm

Hi All,
I'm trying to set some eg.global variables in python from MQTT events.

The events are coming in to eventGhost like:
MQTT.Stat/LampCorner/Power u"ON"

I've created a macro triggered by event "MQTT.*" and within this macro have a python script to set the global variable, but this is where I'm struggling. From the above event.string I want to set a variable: eg.global.LampCorner = ON

I can get the device name (LampCorner in this example) by:
device = eg.event.string.rsplit('/',1)[-2]
device = device.rsplit('/',1)[-1]

I can get the payload (ON in this example) by:
eg.event.payload

but I can't use the device name variable as part of the global variable name to make eg.global.LampCorner

Any assistance?

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

Re: Python: using variable in variable name

Post by kgschlosser » Thu May 11, 2017 3:42 am

variables are also known as attributes and setattr is for set attribute. and if you want to dynamically get an attribute/variable it would be getattr

Heed some warning tho. if you are going to use the getattr either specify a defaulted return value or check to make sure the attribute/variable exists first. and also make sure that the attribute/variable name does not contain any special characters other then an _ and that means no spaces also.

this is to set the attribute/variable

Code: Select all

setattr(eg.globals, device, eg.event.payload)
defaulted value if attribute/variable does not exist

Code: Select all

attribute_name = "WHATEVER_NAME"
some_variable = getattr(eg.globals, attribute_name, None)

if some_variable is not None:
    #do your code here
this will check first then grab

Code: Select all

attribute_name = "WHATEVER_NAME"

if hasattr(eg.globals, attribute_name):
    some_variable = getattr(eg.globals, attribute_name)
    #do your code here

Little suggestion on how to parse the event.

Code: Select all

device = eg.event.suffix.split('/')[1]
state = eg.event.payload

setattr(eg.globals, device, state)
But also doing this you have to make sure you do not use this kind of thing in any other event that you could end up setting the same device name to have an unexpected value. this is what I would do personally. and you can place this kind of a thing into your autostart

Code: Select all

class DefaultMQTTDevice(object):
    Power = None
    Level = None

eg.globals.DefaultMQTTDevice = DefaultMQTTDevice
and then use the code below for a macro

the really nice thing about this is if you are using eg.0.5 you can wildcard the event.
so create a macro and then create an event and in the event you would put this

MQTT.Stat/*

this event will trigger for any event starting with everything before the *.
and then in your macro you would do a script as follows..

Code: Select all

device, attr_name = eg.event.suffix.split('/')[1:]
value = eg.event.payload

if not hasattr(eg.globals, device):
    setattr(eg.globals, device, eg.globals.DefaultMQTTDevice())

device = getattr(eg.globals, device)
setattr(device, attr_name, state)
and how you would access the information would be as follows.

Code: Select all

if eg.globals.LampCorner.Power == 'On':
    #do code here

if eg.globals.LampCorner.Level == 100':
    #do code here
If you like the work I have been doing then feel free to Image

FourFootPaul
Posts: 10
Joined: Fri Jun 15, 2012 12:05 pm

Re: Python: using variable in variable name

Post by FourFootPaul » Thu May 11, 2017 8:52 am

Wow, I wasn't expecting such a thorough answer - thanks for that, it'll give me something to build on :D

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

Re: Python: using variable in variable name

Post by kgschlosser » Thu May 11, 2017 6:31 pm

well i wanted to make sure you know all of the complexities involved when doing this. and the best possible way to avoid any errors.
If you like the work I have been doing then feel free to Image

Post Reply