schaudhary77 Posted December 28, 2021 Share Posted December 28, 2021 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. Quote Link to comment Share on other sites More sharing options...
msgreenf Posted December 29, 2021 Share Posted December 29, 2021 When the room turns off the tv already turns off. This won't do anything Quote Link to comment Share on other sites More sharing options...
schaudhary77 Posted December 29, 2021 Author Share Posted December 29, 2021 Just now, msgreenf said: When the room turns off the tv already turns off. This won't do anything My TV driver has an issue where it sometimes does not turn off. This workaround ensures that it always turns off when I hit Room Off. Quote Link to comment Share on other sites More sharing options...
c4toys Posted December 29, 2021 Share Posted December 29, 2021 the macro needs to land at the room level.. you need to duplicate macro for every room.. you are thinking reverse, room in macro. needs to be macro in room Quote Link to comment Share on other sites More sharing options...
schaudhary77 Posted December 29, 2021 Author Share Posted December 29, 2021 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? Quote Link to comment Share on other sites More sharing options...
msgreenf Posted December 29, 2021 Share Posted December 29, 2021 Fix the bad driver. Less work Quote Link to comment Share on other sites More sharing options...
martymohr Posted December 29, 2021 Share Posted December 29, 2021 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. Quote Link to comment Share on other sites More sharing options...
schaudhary77 Posted December 29, 2021 Author Share Posted December 29, 2021 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. Quote Link to comment Share on other sites More sharing options...
TFlury Posted December 29, 2021 Share Posted December 29, 2021 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. Quote Link to comment Share on other sites More sharing options...
martymohr Posted December 29, 2021 Share Posted December 29, 2021 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. Quote Link to comment Share on other sites More sharing options...
schaudhary77 Posted December 29, 2021 Author Share Posted December 29, 2021 @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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.