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.

How do I create a variable from another variable?

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Re: How do I create a variable from another variable?

Post by Mastiff »

And that's when the problems came. It seems I can do simple variables this way, but for some reason I can't make it to do the temperature variable. I have these two tables created in a startupscript:

Code: Select all

try:
    dummy
except NameError:
    dummy = 0
    eg.globals.Ovnmodus = {
        'WC':None,
        'Kontor':None,
        'Hovedsoverom':None,
        'Gjesterom':None,
        'Testrom':None
    }

try:
    dummy
except NameError:
    dummy = 0
    eg.globals.Temp1 = {
        'WC':None,
        'Kontor':None,
        'Hovedsoverom':None,
        'Gjesterom':None,
        'Testrom':None
    }
As you can see I have the Testrom in both of them. Then I set the variables tor this test script. First the modus, then the temp. These are going to be set upfront from the value in the registry.
And finally the room, which later will come from the number of the sensor, and I try to manipulate this:

Code: Select all

eg.globals.Ovnmodus['Testrom'] = 1
eg.globals.Temp1['Testrom'] = 22
Rom = "Testrom"

temp = eg.globals.Temp1[Rom]

OvnmodusRom = eg.globals.Ovnmodus[Rom]
print (OvnmodusRom)

print (temp)
I have no problems with the modus, but the temp just won't work. I get this error message, and it seems like it won't even set the value into the table:

Code: Select all

   Traceback (most recent call last):
     Python script "52", line 2, in <module>
       eg.globals.Temp1['Testrom'] = 22
   AttributeError: 'Bunch' object has no attribute 'Temp1'
So for some reason I can get it to handle the variable Ovnmodus, which seems to be translated correctly, but the temperature won't work at all. And I can't understand why, because to my eyes I have done exactly the same with that as I have done with the Ovnmodus, only with a different number. Or is it too late in the evening to do this, and I have overlooked something extremely simple?

Edit: Can it be that there has to be a difference earlier in the table script because I'm using more than one list here? Because after a bit of experimenting I tried to print the contents of eg.globals.Ovnmodus and eg.globals.Temp1, and the first gave me the list of the rooms, the second gave me the Bunch error. So it seems the table just isn't created.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: How do I create a variable from another variable?

Post by krambriw »

You are close but it's simpler than that

Code: Select all

try:
    dummy
except NameError:
    dummy = 0
    eg.globals.Ovnmodus = {
        'WC':None,
        'Kontor':None,
        'Hovedsoverom':None,
        'Gjesterom':None,
        'Testrom':None
    }
    eg.globals.Temp1 = {
        'WC':None,
        'Kontor':None,
        'Hovedsoverom':None,
        'Gjesterom':None,
        'Testrom':None
    }
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Re: How do I create a variable from another variable?

Post by Mastiff »

Aha, I see! They only need to be defined for the first dictionary! :D Got it, thanks!
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: How do I create a variable from another variable?

Post by krambriw »

Code: Select all

try:
    dummy
except NameError:
    dummy = 0
That is a little trick I once was learned from the great EvenGhost master himself, Bitmonster. So it could maybe be worth to recap for other coders how this works.

When the script runs for the first time, it tries to reference the variable named 'dummy' (or any other name you like). The attempt fails because the variable is obviously not yet defined and initiated. A 'NameError' exception occurs.
At this point, the 'exception' statements will be executed, the variable 'dummy' is initiated & defined and you can add more definitions like we did with your eg.globals.

In following rounds when the script is running, 'dummy' is found, no exception occurs and your already defined variables are left untouched.
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Re: How do I create a variable from another variable?

Post by Mastiff »

I see! Well, it works. :) Btw I'm up at this hour on a Saturday because of an 11 year old mastiff who really had to go out to take a leak. What's your excuse? :wink:
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: How do I create a variable from another variable?

Post by krambriw »

Just waking up early, first cup of coffe, best part of the weekend
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Re: How do I create a variable from another variable?

Post by Mastiff »

OK, one of those horrible A persons? If I can sleep until around 9 or 10, I'm good! Oh and as for coffee... You have to be grownup to drink that stuff. I'm only 48, so I may drink it in another 50 years! My coffee is a regular Coke. The rest of the day I drink Zero, all cans and all bought at Svinesund (near Str├Âmstad, on the Norwegian border)!

But since you're such an early bird and your brain has gotten a jump start, maybe you could tell me the best way of doing this? :mrgreen:

The part of the script that turns on and of the oven is at the moment just firing an event, like this:

Code: Select all

res = eg.plugins.EventGhost.TriggerEvent(u'OvnPaa_'+(Rom), 0.0) #Turn on heater if it is too cold
It works, but then I'ld have to use regular actions for the heaters. I would like to avoid that. Here's what the Python version looks like:

Code: Select all

OvnPaa_WC = eg.plugins.RFXtrx.send_AC(u'Anslut 1 p\xe5', u'AC', u'012e77fe', u'1', u'on', u'0', None) 
Here I have defined that the variable OvnPaa_WC (which I will get from the dictionary) , but I can't run a variable, can I? Would this be a typical target for a function, so that I need funtions for each heater in each room?
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: How do I create a variable from another variable?

Post by krambriw »

Yes, that will not work since the RFXtrx action is not giving you any return value back on the call. So you have to build the logic yourself with some additional code like this

Code: Select all

if turnOn:
    eg.plugins.RFXtrx.send_AC(u'Anslut 1 p\xe5', u'AC', u'012e77fe', u'1', u'on', u'0', None)
    OvnPaa_WC = True
    #OvnPaa_WC = 'on' #Or like this

if turnOff:
    eg.plugins.RFXtrx.send_AC(u'Anslut 1 p\xe5', u'AC', u'012e77fe', u'1', u'off', u'0', None)
    OvnPaa_WC = False
    #OvnPaa_WC = 'Off'


#Later if you want to check the status and turn off or on you could do it like this

if OwnPaa_WC:
    #it is on, turn the thing off

#or this way

if OwnPaa_WC == 'on':
    #it is on, turn the thing off
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Re: How do I create a variable from another variable?

Post by Mastiff »

Thanks! And Sorry, another "hj├ñrnbl├Âdning", as my friends in Stockholm says. To my defence it was early for me... I have no problem firing that from the script. Then thing I want to do (to keep it simple to make changes, and to avoid a five page script with all power plugs in if statements) is to have all the power plug actions defined in a startup script (as a function that calls the defined powerr plugs , I suppose) and then be able to fire them easily from any other script. Now what I've learnt about functions in Python so far means that it would probably be something in this area (I should really have a big, red L on my computer these days, just like 17 year olds learning to drive). First I define the power plugs in a separate startup script:

Code: Select all

#Testrom
Ovnpaa_Testrom = (u'Anslut 1 p\xe5', u'AC', u'01e77fe', u'1', u'on', u'0', None)
Ovnav_Testrom = (u'Anslut 1 p\xe5', u'AC', u'01277fe', u'1', u'off', u'0', None) 
 
Then I create the functions:

Code: Select all

def OvnPaa(var1):
    const1 = "Ovnpaa_"
    print(const1+var1)
    res = eg.plugins.RFXtrx.send_AC(var1+const1)
    
def OvnAv(var1):
    const1 = "Ovnav_"
    print(const1+var1)
    res = eg.plugins.RFXtrx.send_AC(var1+const1)    
Does it look correct so far?

Finally I have it in the script, wich will be like this:

Code: Select all

Rom = "Testrom"
print (Rom)

if Rom == "Testrom":
    OvnPaa(Rom)
And the function's print returns "Ovnpaa_Testrom". But for some reason that isn't used in the plug-in call, because i get:

Code: Select all

  
   Traceback (most recent call last):
     Python script "539", line 17, in <module>
       OvnPaa(Rom)
     Python script "539", line 4, in OvnPaa
       res = eg.plugins.RFXtrx.send_AC(var1+const1)
   TypeError: __call__() takes exactly 8 arguments (2 given)
Any idea what I have done wrong there?
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: How do I create a variable from another variable?

Post by krambriw »

Have you thought about what this really means?
TypeError: __call__() takes exactly 8 arguments (2 given)
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: How do I create a variable from another variable?

Post by krambriw »

For defining your stuff I would just show you how to use a separate py-file where you can set all your variables at start. So lets define a new file, we call it myDefinitions.py and we put it in the EG root folder. Let's make a simple content to start

Code: Select all

#Testrom
Ovnpaa_Testrom = (u'Anslut 1 p\xe5', u'AC', u'012e77fe', u'1', u'on', u'0', None)
Ovnav_Testrom = (u'Anslut 1 av', u'AC', u'012e77fe', u'1', u'off', u'0', None) 
In our script we now just import the file and then we can access all the pre-defined variables

Code: Select all

import myDefinitions

def OvnPaa(var):
    eval("eg.plugins.RFXtrx.send_AC"+var)

OvnPaa(str(myDefinitions.Ovnpaa_Testrom))
Drawbacks: if you need to change the imported file, you need to restart EG for the changes to take effect.

Right know I have lost a bit where you are heading, I do not see your red line, so this might not be very helpful, I am not sure
Last edited by krambriw on Sat Jan 17, 2015 4:45 pm, edited 1 time in total.
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Re: How do I create a variable from another variable?

Post by Mastiff »

Thanks, I'll wait! And yes, I have thought about that. I understand that the variable isn't used in the call to the plugin, I just can't figure out why.
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Re: How do I create a variable from another variable?

Post by Mastiff »

Ah, here I was waiting for a mail from the forum about an answer, didn't occur to me that you would use that post as a "placeholder" and edit your answer in that! :mrgreen: Thanks! Well, where I'm going with this single function (I almost have a working script for running all ovens of only one script with triggers for each room, this is the only thing missing, I think) is to simplify what I have to write to turn on and off an oven (and it will have a "sister" for audio/video power plugs as well). I appreciate the help, but I'm afraid I'm not big on putting anything excecpt the stuff that is installed as standard in files. Simple reason: I keep offsite backup of the one directory where I have all my Girder, EventGhost and other automation setup and I'd prefer not to spread stuff around. :)

So what I would like to achieve is a way to simplify the call a lot, so that using res = Ovnpaa(Rom) in a sccript turns on the oven in the room that's defined as the active room in the script at the moment, or later res = Forsterkerpaa(Rom) does the same with the amp. The variable Rom being defined. I have found a few errors in my attemt, but I don't think I am close enough yet. The problem is that this is the code for triggering an RFX send:

Code: Select all

eg.plugins.RFXtrx.send_AC(u'Anslut 1 p\xe5', u'AC', u'012e77fe', u'1', u'on', u'0', None)
And I also have a couple of ARC. So the perfect solution would be to have this part in a variable defined at startup:

Code: Select all

OvnPaa_Testrom = (_AC(u'Anslut 1 p\xe5', u'AC', u'012e77fe', u'1', u'on', u'0', None))
while the function took the room from the variable in the triggering script (for instance 'Testrom'), added the constant OvnPaa_ to make it into OvnPaa_Testrom and then contained something like this:

Code: Select all

eg.plugins.RFXtrx.send(OvnPaa_Testrom)
So this, when triggered actually sends the full string. Is that even possible with all the stuff in the plugin command?
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Re: How do I create a variable from another variable?

Post by Mastiff »

I may be on to something, don't use any time on this before I have tried an adjustment to my script. :)
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Re: How do I create a variable from another variable?

Post by Mastiff »

No, I wasn't! I've been coding myself in circles... Sometimes this is soooo frustrating! Except for when I finally solve it, of course...the few times I actually solves something myself, that is! Here's what I have so far, the script that defines the function and the variable:

Code: Select all

#Testrom
eg.globals.Ovnpaa_Testrom = ("_AC(u'Anslut 1 p\xe5', u'AC', u'012efe', u'1', u'on', u'0', None)")
eg.globals.Ovnav_Testrom = (u'Anslut 1 p\xe5', u'AC', u'01277fe', u'1', u'off', u'0', None) 

def OvnPaa(var1):
    const1 = 'eg.plugins.RFXtrx.send_AC('
    const2 = 'Ovnpaa_'
    const3 = ')'
    ovn = const1+const2+var1+const3
    print(ovn)
    res = ovn
    
eg.globals.OvnPaa = OvnPaa
And then the call script:

Code: Select all

eg.globals.Rom = 'Testrom'
print (eg.globals.Rom )

if eg.globals.Rom == 'Testrom':
    res = eg.globals.OvnPaa (eg.globals.Rom)
The if statement in the call script is just to have something that fires the function. I get a result that's close, I think:

Code: Select all

   eg.plugins.RFXtrx.send_AC(Ovnpaa_Testrom)
But of course nothing is fired. My guess is that I need something that translates the variable value into another variable name? Or am I just totally of track?
Post Reply