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?
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.
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.
Python: using variable in variable name
-
FourFootPaul
- Posts: 10
- Joined: Fri Jun 15, 2012 12:05 pm
- kgschlosser
- Site Admin
- Posts: 5190
- Joined: Fri Jun 05, 2015 5:43 am
- Location: Rocky Mountains, Colorado USA
Re: Python: using variable in variable name
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
defaulted value if attribute/variable does not exist
this will check first then grab
Little suggestion on how to parse the event.
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
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..
and how you would access the information would be as follows.
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)
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
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)
Code: Select all
class DefaultMQTTDevice(object):
Power = None
Level = None
eg.globals.DefaultMQTTDevice = DefaultMQTTDevice
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)
Code: Select all
if eg.globals.LampCorner.Power == 'On':
#do code here
if eg.globals.LampCorner.Level == 100':
#do code here
-
FourFootPaul
- Posts: 10
- Joined: Fri Jun 15, 2012 12:05 pm
Re: Python: using variable in variable name
Wow, I wasn't expecting such a thorough answer - thanks for that, it'll give me something to build on 
- kgschlosser
- Site Admin
- Posts: 5190
- Joined: Fri Jun 05, 2015 5:43 am
- Location: Rocky Mountains, Colorado USA
Re: Python: using variable in variable name
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.
