Jump to content
C4 Forums | Control4

Help with macro


schaudhary77

Recommended Posts

I have programming, as shown in the attached screenshot. I want to create a macro so I don't have to duplicate the code for each room. 

When room turns off -> execute macro.

How do I import the value of the room into the macro. For example, when the bedroom turns off, how do I code the macro such that it knows the commands need to be executed on the bedroom TV.

 

Screenshot-1.jpg

Link to comment
Share on other sites


If that is indeed the case, that does not seem like good coding. Instead of duplicating the code in each room, I'm duplicating the macro (with the same code).

I'm thinking, if I create a variable.

When bedroom off -> set value of this variable to bedroom tv

When living room off -> set value of this variable to living room tv

In the macro,

If variable = bedroom tv, execute commands a, b,c on bedroom tv

If variable = living room tv, execute commands a, b, c on living room tv 

If this works, it is still a lot of hard coding and repetition.

 

Ideally it would be, when room off, extract the value of which room and then provide this value to the macro.

In the macro -> get the <room> -> execute commands a,b,c on the the <room> tv

Possible?

Link to comment
Share on other sites

Yes, fixing the bad driver is the easiest. But you don't need (nor should you use) a macro. In programming, highlight the room and select the Power Off event. Then program as much as necessary, or as creatively as you wish. As an example, we use this in our master bath to turn off the lights when we power off the room. If it is daylight then turn off immediately, otherwise go off slowly.

Link to comment
Share on other sites

Thanks all for the feedback. Fixing the driver is not an option.

@martymohr I do have it programmed the way you are suggesting and it works fine. The reason I want to use a macro is to reduce repetitive code (same programming for each room). I create the code (macro) once and use it as needed wherever. This is a simple example with just a couple of lines of programming. There may be other programming that may be more complex and may need to be added to multiple rooms. Taking your example, say you created a sequence for your bathroom and you want the same sequence to run in all your bathrooms. 

 

I am equating these macros to functions or procedures in coding. Maybe I'm wrong and that is not how macros in Control4 work at all.

 

Link to comment
Share on other sites

Fixing the driver would be preferable, and room level programming is ussualy the best way to go about things. But if you really want to use a macro here is what I would do. Go into agents and select Variables create one lets call it "RoomTV" set it as a device variable.

When living room turns off

     Variables->RoomTV = Living Room TV

     Execute Macro "TV Fix"

When Master turns off

     Variables->RoomTV = Master TV

     Execute Macro "TV Fix"

 

Program Macro

When TV Fix is Executed

    If Variables->RoomTV EQUAL to Living Room TV

         *Execute commands for living room TV

    If Variables->RoomTV EQUAL to Master TV

          *Execute commands for Master TV

 

You would still need to do programing on each room and then add that room into the macro. 

I didn't test this at all I just confirmed in OS 3.2.4 you could set a variable to a device.

Link to comment
Share on other sites

13 hours ago, schaudhary77 said:

Thanks all for the feedback. Fixing the driver is not an option.

@martymohr I do have it programmed the way you are suggesting and it works fine. The reason I want to use a macro is to reduce repetitive code (same programming for each room). I create the code (macro) once and use it as needed wherever. This is a simple example with just a couple of lines of programming. There may be other programming that may be more complex and may need to be added to multiple rooms. Taking your example, say you created a sequence for your bathroom and you want the same sequence to run in all your bathrooms. 

 

I am equating these macros to functions or procedures in coding. Maybe I'm wrong and that is not how macros in Control4 work at all.

 

Yes, using a macro to reduce coding is preferable, as well as placing all code into one location. You are not saving any coding in this situation because the macro needs to know which room called it and then execute accordingly. But you do have all similar (but not identical) code in one location, which is sometimes beneficial. IOW, there is no one right way to code what you’re trying to accomplish.


And yes, equating macros to a function is correct. However, unfortunately, you cannot easily pass variables into the macro as you would with a function, and you don’t get a return value without doing additional programming.

Link to comment
Share on other sites

@TFlury Thank you for the detailed example. That's what I ended up doing. I can confirm it works. 

 

@martymohr Thanks for the clarification. Yes, it is slightly better than before, in that all the similar code is in one location (ease of maintenance). Would have been ideal if I could just say execute the following set of commands on <variable> device or room but seems like that's not how it works in C4. New to C4 programming so playing around with it. This helps is clarifying some basic concepts.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.