Active TopicsActive Topics  Display List of Forum MembersMemberlist  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin
PowerHome General
 PowerHome Messageboard : PowerHome General
Subject Topic: X10 lighting Buttons Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
PhilC
Newbie
Newbie
Avatar

Joined: March 18 2004
Location: United Kingdom
Online Status: Offline
Posts: 39
Posted: September 20 2004 at 17:02 | IP Logged Quote PhilC

Hi,
Have been getting to grip with functions, particularly conditional if statements with varying success!
Coming from a perl/Javascript background I am struggling a little with the syntax and am trying to achieve the following with the sole intention of reducing the amount of lighting buttons I have:

I have 5 buttons (x10 A1-5). You basically select the one of these buttons which corresponds to an X10 Module and then select one of the desired actions to go with it...ie dim 25% on, off etc.
The problem is 2 of my modules are appliance modules and therefore won't switch off when receiving a DIM 100 command. Would it be possible to write a function that checks if the module is an appliance or lamp module and then performs the appropriate DIM or OFF command?

Many thanks
Phil

Back to Top View PhilC's Profile Search for other posts by PhilC
 
TonyNo
Moderator Group
Moderator Group
Avatar

Joined: December 05 2001
Location: United States
Online Status: Offline
Posts: 2889
Posted: September 20 2004 at 18:41 | IP Logged Quote TonyNo

I just looked through the docs and can't find anything on reading module types. Maybe Dave can suggest a method of reading it directly from the database?

Tony
Back to Top View TonyNo's Profile Search for other posts by TonyNo Visit TonyNo's Homepage
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: September 20 2004 at 19:00 | IP Logged Quote dhoward

Yep, I sure do...

Use the following formula below to check whether a house/unit supports dimming:

ph_sqlselectinto(1,"select x10types.standard_dim from x10unitcode,x10types where x10unitcode.typeid = x10types.typeid and x10unitcode.id = 'K' and x10unitcode.code = 7")

You can see that Ive hardcoded a check for K7 so you can change this to global or system variables if you like. When you execute this formula, either a 1 or 0 will be the [LOCAL1] system variable (a 0 means no dimming).

To make this even more useful, you could create a macro that accepts the house and unit code and returns the 1 or 0 and call it use the ph_macroparmret function. As a matter of fact, here it is for you:

insert into macroheader values ('CHECK MODULE DIM','CHECK MODULE DIM',0,0,1);
insert into macrodetail values ('CHECK MODULE DIM',1,38,'',0,'ph_sqlselectinto(1,"select x10types.standard_dim from x10unitcode,x10types where x10unitcode.typeid = x10types.typeid and x10unitcode.id = ''[LOCAL1]'' and x10unitcode.code = [LOCAL2]")',0);

This will only import into the new beta version 1.03.4.1 and will create a macro for you called "CHECK MODULE DIM". Anytime you want to check a modules type from within a formula just call this function: ph_macroparmret("CHECK MODULE DIM","housecode to check",unitcode to check,0,0,0). This function call will return either a 1 or 0.

Hope this helps and let me know if you need more help.

Dave.
Back to Top View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 
PhilC
Newbie
Newbie
Avatar

Joined: March 18 2004
Location: United Kingdom
Online Status: Offline
Posts: 39
Posted: September 21 2004 at 04:10 | IP Logged Quote PhilC

Thanks for the speedy reply Dave and Tony.
I can pretty much understand what's going on here but think I am going to have problems constructing the 'IF' syntax. Basically a perl version of what I would need would be:

if($module eq "appliance"){$command="off";}
if($module eq "Lamp"){$command="DIM 100";}

execute:$command

....so how would this translate to PH syntax?

Also Dave, is there any documentation regarding the syntax of formulas as at the moment I am relying on the phsample.db to try and work out how things are constructed?

Many thanks.
Phil

PS: I have made a prototype touch screen remote system based on an 8 inch LCD and micro pc which I am going to sell on to clients here in the UK with PH as the heart of the system. Will post up some pics when it's ready if you like.

Back to Top View PhilC's Profile Search for other posts by PhilC
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: September 21 2004 at 10:35 | IP Logged Quote dhoward

Phil,

The "IF" syntax if you using my macro above would be:

if(ph_macroparmret("CHECK MODULE DIM","{housecode}",{unitcode},0,0,0) = 1,ph_x10btn("{housecode}",{unitcode},4,100),ph_x10btn("{housecode}",{unitcode},3,0))

The above statement also assumes you have a couple of global variables named "housecode" and "unitcode" where you have stored the house/unit you are wanting to check and control.

Formulas are really pretty simple, but since you are an experienced programmer, will be a little counterintuitive. Essentially, a formula is nothing more than a sequence of operands separated by operators. The operands must all be of the same type (ie. numeric, string, etc.) and the standard operators are available (+,-,/,*,open and close parenthesis).

So, 2 + 2 is a formula, "This is" + " a test" is a formula. The power comes in the functions. Each function is treated as an operand since they all return a standard datatype. Most functions will also perform additional processing, but they all return something. These functions can be combined into a formula by adding them together. Often, the result of the formula is unimportant to you and the formula is just a means to execute a number of functions.

A good reference for all of the functions is the ph-help.chm file that can be accessed from the menu Help->Contents. Here, every PowerHome function is explained.

Dissecting the "IF" statement above, it's really just a formula. In PowerHome, there is no "IF" statement, but there is an "if" function. If we look at the help for the "if" function, we see that it takes 3 parameters. The first parameter is a formula that should evaluate to a boolean true of false. In our above example, we compare the result of the ph_macroparmret function with the number 1. If this result is true, the "if" function returns the second parameter (which in itself can be a formula). If the result is false, the "if" function returns the third parameter (again, this can be a function as well).

Note: The "if" function isnt exactly a function, if it were, both the second and third parameters would have been evaluated. In this case, the second and third parameters are only evaluated depending upon the result of the first parameter. This is the exception rather than the norm however.

I would love to see some pics when you have them ready.

Let me know if you have any other questions and I'll do my best to help.

Good luck,

Dave.
Back to Top View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 
PhilC
Newbie
Newbie
Avatar

Joined: March 18 2004
Location: United Kingdom
Online Status: Offline
Posts: 39
Posted: October 07 2004 at 16:20 | IP Logged Quote PhilC

Hi Dave,

I have finished the work on the other thread and am now turning my attention to this problem.

I am really struggling with this and cannot seem to get it to work.
If I paste in:

ph_sqlselectinto(1,"select x10types.standard_dim from x10unitcode,x10types where x10unitcode.typeid = x10types.typeid and x10unitcode.id = 'A' and x10unitcode.code = 1")

into the formula builder, I always get a return value of 0 no matter what unit code I put in. I have all my x10 devices set up properly in the x10 type table.

Any ideas?

Thanks
Phil

Back to Top View PhilC's Profile Search for other posts by PhilC
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: October 07 2004 at 23:10 | IP Logged Quote dhoward

Phil,

You've got it right. The function returns a 0 signifying that it was executed successfully. The value you want is in the [LOCAL1] system variable.

The first parameter of the ph_sqlselectinto function is the starting number of the local variable you wish to select into. This function allows you to have more than 1 column returned. If you had selected 3 columns and placed a 1 in the first parameter then the 3 columns would be in [LOCAL1] thru [LOCAL3].

So, if you want to test it out in the formula builder, use:

ph_sqlselectinto(1,"select x10types.standard_dim from x10unitcode,x10types where x10unitcode.typeid = x10types.typeid and x10unitcode.id = 'A' and x10unitcode.code = 1") + ph_getvar_n(1,1)

Assuming the ph_sqlselectinto executed normally, it will return a 0 which will be added to the ph_getvar_n(1,1) function which returns the value in [LOCAL1].

Hope this helps,

Dave.
Back to Top View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 
PhilC
Newbie
Newbie
Avatar

Joined: March 18 2004
Location: United Kingdom
Online Status: Offline
Posts: 39
Posted: October 08 2004 at 03:41 | IP Logged Quote PhilC

Hi Dave,
Got it, and I now understand better what is actually going on!

This leaves me with a final problem... the following formula:

if(ph_macroparmret("CHECK MODULE DIM","{HOUSECODE}",{UNITCODE},0,0,0) = 1,ph_x10btn("{HOUSECODE}",{UNITCODE},4,100),ph_x10btn("{HOUSECODE}",{UNITCODE},3,0))

seems to always fail...returning a '!'

Error in the syntax?

Thanks
Phil


Back to Top View PhilC's Profile Search for other posts by PhilC
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: October 18 2004 at 09:06 | IP Logged Quote dhoward

Phil,

Sorry I missed your message .

The problem with the above formula is your use of the ph_macroparmret function. This function returns a "string" value which you are comparing to a numeric. You could fix this by enclosing the ph_macroparmret function within the "integer()" function or you could make the comparison to 1 be a string with "1".

Hope this helps,

Dave.
Back to Top View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 
PhilC
Newbie
Newbie
Avatar

Joined: March 18 2004
Location: United Kingdom
Online Status: Offline
Posts: 39
Posted: October 19 2004 at 04:23 | IP Logged Quote PhilC

Thanks Dave, it now works beautifully!

For the benefit of everyone else, here is a brief summary of how to achieve this:

GLOBAL VARIABLES
Set up 2 Global variables "HOUSECODE" and "UNITCODE"

SELECTION BUTTONS
Set up your buttons where you select which lamp you want to Dim or Switch off. This is the formula:
ph_setglobal_a( "HOUSECODE", "A" ) + ph_setglobal_a( "UNITCODE", "2" )
This sets "HOUSECODE" to 'A' and "UNITCODE" to '2'

MACRO TO CHECK IF MODULE IS APPLIANCE OR LAMP
Set up this macro, making sure all your X10 units are set up correctly in the X10 setup area of PH Explorer.
Macro Name: CHECK MODULE DIM
Formula:
ph_sqlselectinto(1,"select x10types.standard_dim from x10unitcode,x10types where x10unitcode.typeid = x10types.typeid and x10unitcode.id = '{HOUSECODE}' and x10unitcode.code = {UNITCODE}")

X10 BUTTONS
Finally create 2 buttons, one for 'On' and one for 'Off'.
These buttons will now check if the module is appliance or Lamp and dim or switch accordingly.
Formula for on:
if(ph_macroparmret("CHECK MODULE DIM","{HOUSECODE}",{UNITCODE},0,0,0) = "1",ph_x10btn("{HOUSECODE}",{UNITCODE},5,100),ph_x10btn("{HOUSECODE}",{UNITCODE},2,0))

Formula for off:
if(ph_macroparmret("CHECK MODULE DIM","{HOUSECODE}",{UNITCODE},0,0,0) = "1",ph_x10btn("{HOUSECODE}",{UNITCODE},4,100),ph_x10btn("{HOUSECODE}",{UNITCODE},3,0))

That's it!.

Thanks for all your help in achieving this Dave.

Phil

Back to Top View PhilC's Profile Search for other posts by PhilC
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: October 25 2004 at 09:34 | IP Logged Quote dhoward

Phil,

Excellent!! Just let me know if you have any other questions. BTW, the code looks great.

Dave.
Back to Top View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 
TonyNo
Moderator Group
Moderator Group
Avatar

Joined: December 05 2001
Location: United States
Online Status: Offline
Posts: 2889
Posted: December 22 2004 at 22:36 | IP Logged Quote TonyNo

OK! I'm trying to use the code to check if a module is dimmable in a PSP page, but, it is obviously stepping on LOCAL1 because it is supposed to...

Is there a way 'round this to not trash LOCAL1?
Back to Top View TonyNo's Profile Search for other posts by TonyNo Visit TonyNo's Homepage
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: December 22 2004 at 23:09 | IP Logged Quote dhoward

Tony,

Im not sure which part of the code you're talking about. Are you referring to the "CHECK MODULE DIM" function up above? If so, then there is no way around trashing [LOCAL1] thru [LOCAL5] without rewriting it. The ph_macroparm and ph_macroparmret functions will overwrite [LOCAL1] thru [LOCAL5] for the passing of its parameters. Additionally, ph_macroparmret must use [LOCAL1] for the return value.

You could work around this so that the function instead receives the house and unit codes through global variables or perhaps a couple of the seldom used system variables [GLOBAL1] thru [GLOBAL20]. The same could be used for the return value.

Let me know if this what you were thinking of.

Dave.
Back to Top View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 
TonyNo
Moderator Group
Moderator Group
Avatar

Joined: December 05 2001
Location: United States
Online Status: Offline
Posts: 2889
Posted: December 23 2004 at 07:31 | IP Logged Quote TonyNo

Yes, this is about CHECK MODULE DIM. I'll just recode my stuff to not use LOCAL1.
Back to Top View TonyNo's Profile Search for other posts by TonyNo Visit TonyNo's Homepage
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum