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.
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 »

Besides you had some typos, I think you are getting tired :shock: , I did adjust the addresses. I put all into one script for the time and it works here, it actually fires the command in RFXtrx (and I see that captured by the TellStickDuo)

1) Look at the syntax I have corrected for your definitions (no additional parenthesis needed, that is just creating a tuple and we want a string !!!)

2) The keyword 'eval' is used to execute a string content, this is where it gets fired to the RFXtrx

3) If you split this code into two scripts I'm sure it will work the same

Code: Select all

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

def OvnPaa(var1):
    const1 = 'eg.plugins.RFXtrx.send'
    #const2 = 'Ovnpaa_'
    #const3 = ')'
    #ovn = const1+const2+var1+const3
    ovn = const1+var1
    print(ovn)
    eval(ovn)

eg.globals.OvnPaa = OvnPaa  #Adding the function to eg.globals


######Calling script
eg.globals.Rom = 'Testrom'
#print (eg.globals.Rom )

if eg.globals.Rom == 'Testrom':
    eg.globals.OvnPaa (eg.globals.Ovnpaa_Testrom)
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 must admit that there's code flying in front of my eyes now... I fear I may dream in Python! Or of a python? Can be just as bad... :lol: Thanks a lot! And Yeah, you're right! I tested it with one of the actual plugs, and it fired. :) So I think I'll end on a success and call it a night, since I was up so early! "ell, I have had a few today, actually - I can send thermostate modes (day, night, no frost) and the temperatures for those different modes for all the rooms from Girder with the EG command line, and they are both put as active variables and as system registry entries so they are saved for next reboot. And I can use all those to fire events, which tomorrow will be to the script here. Not bad for a day with both coding and other stuff! :D
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 »

The sending works nicely. But I can't for the life of me get it to work with a variable instead of a string in the triggering action. I forgot that I don't have the room as a string, but as a variable. Somewhat like this:

Code: Select all

######Calling script
Rom = 'Testrom'
print (Rom )

if Rom == 'Testrom':
    eg.globals.OvnPaa (Rom)
This, when ran through the function, of course doesn't give me the line I want. The value Testrom isn't translated in to this line:

Code: Select all

eg.plugins.RFXtrx.send eg.globals(Ovnpaa__AC(u'Anslut 1 på', u'AC', u'02e377fe', u'1', u'on', u'0', None))
I have also tried to simplify the calling script a bit (since I will use the same function for a lot of amplifiers and other stuff, with functions called AmpOn and so on), where I came to this:

Code: Select all

def OvnPaa(var1):
    const1 = 'eg.plugins.RFXtrx.send eg.globals'
    const2 = '(Ovnpaa_'
    const3 = ')'
    ovn = const1+const2+var1+const3
    print(ovn)
    #eval(ovn)
eg.globals.OvnPaa = OvnPaa  #Adding the function to eg.globals
I was hoping that this would let me only use the Rom variable as the payload for the action, and I think it sort of works, since it gives me the same result as above:

Code: Select all

eg.plugins.RFXtrx.send eg.globals(Ovnpaa_Testrom)
Still without the full line, of course. Can you see where I have gone over the edge here? As soon as I have this one script running I will build on it for other stuff. Like the temperatures that aren't part of the control, but rather alarm tempeartures: Freezer above -10, fridge above 10, temp in a room with water in it below 5 and so on. So if I manage to get this stuff running today, I will start expanding next time. And I think I should be able to fix the functions myself then, since they will mostly be search/replace jobs! :mrgreen:
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 »

I do not understand your problem, are you back to your non-working example again?

1) to execute a string as code you shall use eval !!! The eval function lets a python program run python code within itself, eval() interprets a string as code. I thought I have explained that already a couple of times

2) if you do not provide the correct parameters to the function in your call, how shall the function understand what to do? I do not see where you define all the necessary params
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 »

First of all thank youfor your patience! Maybe I understand even less than I thought. Firstly about the eval, I understood that one. It's just that I tried to print the string first to see if the string sent to eval was correct, but it wasn't. The print gave me

Code: Select all

eg.plugins.RFXtrx.send eg.globals(Ovnpaa_Testrom)
and not the full string, with the AC set:

Code: Select all

eg.plugins.RFXtrx.send eg.globals Ovnpaa__AC(u'Anslut 1 på', u'AC', u'02e377fe', u'1', u'on', u'0', None)
. So with the eval activated everyting just errored out.

Well, the original is still working, it's just that I sort of thought it would do what I needed to with the value of a variable as well as a string ('Testrom'). the string works, but when it's the variable value it doesn't. The thing is I would like to define only one single parameter in the action script, and that's the name of the room. The name of the room is in the example the variable Rom that I set in the first line of the example. So in my head (which is a scary place these days...) this:

Code: Select all

######Calling script
Rom = 'Testrom'
print (Rom )

if Rom == 'Testrom':
    eg.globals.OvnPaa (Rom)
was supposed to fire the function eg.globals.OvnPaa with the only missing parameter in the Function, which was the value of the variable Rom, in this case Testrom. Is that clearer?
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 think I overexplain stuff a lot... Probably because I grew up the son of two teachers! :lol: How about a diagram? Given that the the working script has set the variables Rom (the name of the room) and a startup script has set the variables:

eg.globals.Ovnpaa_WC ( "_AC(u'Anslut 1 p\xe5', u'AC', u'012e77fe', u'1', u'on', u'0', None)"
eg.globals.Ovnpaa_LivingRoom ( "_AC(u'Anslut 1 p\xe5', u'AC', u'012e77fe', u'1', u'on', u'0', None)"
eg.globals.Ovnpaa_MasterBedroom ( "_AC(u'Anslut 1 p\xe5', u'AC', u'012e77fe', u'1', u'on', u'0', None)"
eg.globals.Ovnpaa_CornerBedroom ( "_AC(u'Anslut 1 p\xe5', u'AC', u'012e77fe', u'1', u'on', u'0', None)"

So one for each room in the house that has a power plug on the heater (of course with the correct string for each of the power plugs). Here's the diagram:

action script triggering eg.globalsOvnPaa (Rom)
->
the function eg.globalsOvnPaa does it's magic, converts it to the correct plug-in action and sends it to the plugin (presumeably with eval)
->
eg.plugins.RFXtrx.send eg.globals(Ovnpaa__AC(u'Anslut 1 på', u'AC', u'02e377fe', u'1', u'on', u'0', None)) is run.
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 »

Sorry, you have to give all parameters correctly to the RFXtrx action somehow, otherwise it will not work. And your examples above are not correct from syntax point of view.

Look at this line in your example, it is completly wrong, double check the syntax otherwise it will NEVER work!

Code: Select all

 eg.plugins.RFXtrx.send eg.globals(Ovnpaa__AC(u'Anslut 1 på', u'AC', u'02e377fe', u'1', u'on', u'0', None)) 
It is not even close to what is required and what I have described earlier, compare with the correct that should look like this

Code: Select all

eg.plugins.RFXtrx.send_AC(u'Anslut 1 p\xe5', u'AC', u'02e377fe', u'1', u'on', u'0', None)
You have to fix so that the strings gets composed in such way so that once eval is applied, it results in a compatible python code.
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 »

Sorry, that was not the string I get composed. That was just a mistake when I was going to write what string I was looking for. If I got that string when trying to get it all down to a function with one parameter set by a variable I would probably be close enuogh to the target to fix it myself. I don't get that string at all, what I get is

Code: Select all

eg.plugins.RFXtrx.send eg.globals(Ovnpaa_Testrom)
I'm guessing you answered this when I was writing my second answer to the one bedfore, maybe it's a bit clearer explained in that one?
Last edited by Mastiff on Sun Jan 18, 2015 2:38 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 »

Btw good going on the women's sprint relay! I thought Norway would have that one for sure... :oops: Stina Nilsson will be dangerous in Falun!
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 »

And what you get will obviously not work
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 »

Yeah, I understood that. I just don't understand why I'm getting it. I had the action script set the value of the parameter to the function to for instance 'WC', and then let the function put together the constants and variables like this:

Code: Select all

 OvnPaa(var1):
    const1 = 'eg.plugins.RFXtrx.send'
    const2 = 'Ovnpaa_'
    const3 = ')'
    ovn = const1+const2+var1+const3
    print(ovn)
    eval(ovn)
In my head the resulting variable "ovn" should contain the value of OvnPaa_WC in the resulting string. What it does though is to have the name of the variable OvnPaa_WC there instead. That's what I don't understand. I'm guessing that I a conversion line there that somehow puts the value into the concatenating string instead of the name
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 »

What it does though is to have the name of the variable OvnPaa_WC there instead
Yes, of course it will since you put it in the name as a string instead of fetching the value first and then make that a string!!!!!

Look at this step by step:

const2 = 'Ovnpaa_'
var1 = 'WC'

Now merge them
const2+var1 = 'Ovnpaa_WC'

To get the value you have to access the variable with that name, right, not use the name of it. We have learned how to run a string as python code, remember?

myCmnd = eval( const2+var1 )

Finally, we have to make that a string again and melt all together in one final string that we can use with eval to trigger the action.


Now look & test this script, it might be what you are looking for

Code: Select all

eg.globals.Ovnpaa_WC = "_AC(u'Anslut 1 p\xe5', u'AC', u'012e77fe', u'1', u'on', u'0', None)"

def OvnPaa(var1):
    const1 = 'eg.plugins.RFXtrx.send'
    const2 = 'eg.globals.Ovnpaa_'
    myCmnd = eval( const2+var1 ) 
    ovn = const1+str(myCmnd)
    print(ovn)
    eval(ovn)
    
OvnPaa('WC')    
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 »

Please, sir, don't be so !!!! mad! I haven't tried actual Python coding before the last week! :oops: Finally, I see where I went wrong, and I actually think I understand the difference! And the script? Works perfectly! Thank you very much! :D :D :D :D I'll try not to nag more today, I will be busy putting the final bits and pieces together to a control system. Oh, and having a few "mellan├Âl" bought in Str├Âmstad to celebrate the second I have it running! :mrgreen:
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 »

Good, no worries, I'm happy it worked out and also happy you can still afford buying stuff in Sweden, it's hard when the oil price is dropping :D
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 »

Yeah, I know. Luckily we have a lot of money in the bank, called The petroleum fund. So I hope I will get a pension in 15-20 years. My children will probably have to work harder... :wink:
Post Reply