Python - Relay Control | MUSE
In this video, you’ll learn how to control relay ports on an AMX MUSE controller using Python. We’ll walk through toggling relay states with button events and using simple Boolean logic to manage hardware behavior. You’ll also see how to monitor relay state changes with watch events and generate log feedback for debugging. By the end, you’ll have a solid foundation for integrating real-world hardware control into your automation workflows.
What You’ll Learn:
• Controlling MUSE relay ports with Python
• Toggling relay states using button events
• Applying Boolean logic to manage hardware behavior
• Monitoring relay changes with watch events
• Logging feedback for debugging and visibility
• Integrating physical control elements into automation systems
Hello, and welcome to this video on controlling MUSE relay ports using Python code. For this example, we are going to start with some basic code already written. You can see here that we have a couple of devices already defined. We have dvIdevice for our MUSE controller’s idevice and dvTP for a touch panel. In addition, we have a very basic button event created for button number 1 on port 1.
We want to write a simple piece of code that turns on the relay when the button is pressed, and when the button is released, turn off that relay. The first thing we need to look at is how the relay is controlled. To do this, let’s look at the descriptor file for idevice. Right-click on idevice in the Devices list and select Descriptor, now select Text for the file type at the top of the window.
Now, search the text document for the word relay. You can see here that there are 4 relay ports on this controller, and each relay port has a single Parameter called state. It’s a Boolean parameter that is read/write indicated by the rw. This means to control the state of the relay we need to change the value to the state parameter. In addition, if we wanted to trigger something based on that state, we could use a watch event to monitor the state parameter.
Let’s return to our script and start writing some code. The first thing we are going to do is create a device for our relay port. To do this, let’s assign dvRelay to be equal to dvIdevice.relay[0].
*** Enter Code ***
dvRelay = dvIdevice.relay[0]
*****************
In our button event, let’s create the two conditionals necessary to be triggered when the button is pressed and released. For this we will create an if statement that says if(event.value == True): this will be the push trigger, and below it we will put the else: statement for the release trigger.
*** Enter Code ***
If(event.value == True):
Else:
******************
To control our relay ports, we need to set the ‘state’ attribute parameter to true for on and false for off. Below our if statement, type in dvRelay.state = True, and below the else statement type in dvRelay.state = False.
*** Enter Code ***
dvRelay.state = True
dvRelay.state = False
******************
Save your code and upload it to the MUSE controller and be sure and attach to[RJ1.1][MW1.2] the code when it is running. When you press and release the touch panel button, you should see the LED for relay 1 blink on and off as well as hear the clicking sound of the relay being actuated. Since our code can’t see an LED or hear the clicking, how can we know if the relay port is on or off?
To monitor the state of the relay port, we can create a watch for the state parameter. Remember, watches require that a callback function be passed to the watch. First let’s create the watch. The watch will be dvRelay.state.watch, and we will pass it a function named relayState.
*** Enter Code ***
dvRelay.state.watch(relayState)
*****************
The first thing we’ll do is create our call back function and look at the dictionary printout to see what information we receive when the relay turns on and off. Let’s create an info log by typing in context.log.info(f”Relay dictionary: {event.__dict__}”)
*** Enter Code ***
Def relayState(event):
Context.log.info(f”Relay dictionary: {event.__dict__}”)
Load code and show printout of dictionary.
*****************
The dictionary provides us the id of the parameter that triggers the event, “state”, and more importantly the current “value” of that state. We can use value to create a conditional to print out whether the relay is on or off.[RJ2.1][MW2.2]
We can actually use the same conditional we created in our button event for value.
*** Enter Code ***
If(event.value == True):
Else:
****************
Lastly, let’s create a couple of info logs informing us of the state of the relay. For each conditional type in context.log.info(“Relay 1 is ON/OFF”).
*** Enter Code ***
Context.log.info(“Relay 1 is ON”)
Context.log.info(“Relay 1 is OFF”)
*****************
Save and upload your code to the controller. Now when you press the touch panel button, you should see the messages that your relay is on or off.