# How to control an Output on a MOXA ioLogik using a CoCoS Script

<p class="callout warning">Please use the [custom device type "Moxa ioLogik"](https://knowledgebase.cocos.software/books/cocos/page/how-to-control-an-output-on-a-moxa-iologik-using-the-cocos-moxa-connector) instead.</p>

## Introduction

This use case describes how to configure a (CoCoS) script for controlling outputs on a MOXA ioLogik device.

## Step by step implementation

To configure this use case, follow these steps:

1. Create a (Ping) device to represent our MOXA.
2. Create a script for handling our MOXA outputs.
3. Create relay tag(s) under our device to represent our output(s).
4. Testing.

### 1. Create device

To keep handling of the MOXA relays/outputs on a single place we will create a device for our MOXA by following the following steps:

<table border="1" id="bkmrk-step-1%3A-navigate-to-" style="border-collapse: collapse; width: 100%; height: 502.921px;"><colgroup><col style="width: 50%;"></col><col style="width: 50%;"></col></colgroup><tbody><tr style="height: 239.989px;"><td style="height: 239.989px;">**Step 1:** Navigate to "System / Devices", and click on the new device button.</td><td style="height: 239.989px;">[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/HyxGYMUujdNygBGl-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/HyxGYMUujdNygBGl-image.png)

</td></tr><tr style="height: 216.614px;"><td style="height: 216.614px;">**Step 2:** Type a name for the device (like: "Location - MOXA ioLogik"), choose the **"Ping tool"** device type, and click

on the add button.

</td><td style="height: 216.614px;">[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/rpkgOfyyvFRrl7U4-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/rpkgOfyyvFRrl7U4-image.png)

</td></tr><tr style="height: 46.3182px;"><td style="height: 46.3182px;">**Step 3:** Fill in the "IP address" field, set the status to "On", and click on the save button.

</td><td style="height: 46.3182px;">[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/iO8zeCSPrRZ78Ldn-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/iO8zeCSPrRZ78Ldn-image.png)

</td></tr></tbody></table>

### 2. Create script for output control

For controlling relays/outputs on our MOXA ioLogik we have to create a script.

<table border="1" id="bkmrk-step-1%3A-navigate-to--1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 50%;"></col><col style="width: 50%;"></col></colgroup><tbody><tr><td>**Step 1:** Navigate to "Programming / Scripts" (or "System / Scripts" in versions before 5.1.0), and click on the new script button. </td><td>[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/MY6iVueBkmgDlYHR-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/MY6iVueBkmgDlYHR-image.png)

</td></tr><tr><td>**Step 2:** Paste the following script into the "Script" field.</td><td>(See the script bellow)

</td></tr></tbody></table>

<p class="callout info">The following script can be used for one device, with one or more relay tags *(for creating these tags see: [3. Create relay/output tag(s)](#bkmrk-3.-create-relay%2Foutp))*.</p>

```php
// ---------------------------------------------------------------------
// BASE CONFIGURATION
//
$host = 'x.x.x.x';                      // Host / IPAddress of the device to preform
$deviceName = 'MoxaiologikE1214';       // Name of the MOXA device without any spaces
$slot = 0;                              //

// ---------------------------------------------------------------------
// INIT VARIABLES.
//

// Get the value or use $source to get the tag value from the source tag
if(isset($value) && !empty($value)){
  $state = $value;
}
else {
  $state = $cocos->tagGet($source);	
}

// Get the output/relay to use based on the source tag name
//
$relayTagPrefix = 'device_'.$deviceName.'.relay_';
if(!str_starts_with($source, $relayTagPrefix)) {
	$cocos->logError('Invalid $source argument for script. Source \''.$source.'\' does not match required tag name prefix \''.$relayTagPrefix.'\'.');
	return;
}
$relay = str_replace($relayTagPrefix, '', $source);

// Prepare our request URL.
// For more info on http request to moxa check: https://www.moxa.com/Moxa/media/PDIM/S100000327/moxa-iologik-e1200-series-manual-v15.2.pdf
$url = 'http://' . $host . '/api/slot/' . $slot . '/io/relay/' . $relay . '/relayStatus';

// ---------------------------------------------------------------------
// CALL THE OUTPUT.
//
// Prepare our request data
$data = json_encode(array(
	'slot' => $slot,
	'io' => array(
		'relay' => array(
			$relay => array('relayStatus' => $state)
		)
	)
), JSON_FORCE_OBJECT);

// Preform the PUT request for setting the relay
$response = $cocos->urlPut($url,  $data, 'json', array(
	'Accept: vdn.dac.v1',
	'Content-Type: application/json',
	'Content-Length: ' . strlen($data)
));
// Log output for debugging the script
$cocos->logDebug('PUT ' . $url, "REQ:\nPUT " . $url . "\n> body: " . $data .  "\n\nRES:\n" . $response);
```

<table border="1" id="bkmrk-step-3%3A-before-savin" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 50%;"></col><col style="width: 50%;"></col></colgroup><tbody><tr><td>**Step 3:** Before saving the script change the following variables:

- Set the `$host` variable to the IP address of the MOXA as set in the created device, like:

```php
$host = '10.x.x.x';
```

- Set $deviceName to the name of the device created at [1. Create device](#bkmrk-3.-belplan-aanmaken) without any spaces, like:

```php
$deviceName = 'deviceMoxaioLogik';
```

<p class="callout info">This device name without spaces can be seen under the full device name in the devices list:[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/x9n6izcRYsCCAuZv-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/x9n6izcRYsCCAuZv-image.png)</p>

  
  
</td><td>  
</td></tr><tr><td>**Step 4:** Give the script a fitting name, and click on the "Save" or "Save and close" button.

</td><td>[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/Hbesz2NlGjLC6YG7-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/Hbesz2NlGjLC6YG7-image.png)

</td></tr></tbody></table>

### 3. Create relay/output tag(s)

For our relays/outputs we will now create a (few) tag(s).

The names of these tags must start with `relay_` and after that the relay number, since the script we used will use the tag name to identify which relay needs to be switched.

<table border="1" id="bkmrk-step1%3A-open-the-devi" style="border-collapse: collapse; width: 100%; height: 1036.27px;"><colgroup><col style="width: 50.0433%;"></col><col style="width: 50.0433%;"></col></colgroup><tbody><tr style="height: 221.364px;"><td style="height: 221.364px;">**Step1:** Open the device created at [1. Create device](#bkmrk-3.-belplan-aanmaken), and navigate to the "Taglist" tab.</td><td style="height: 221.364px;">[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/w58DUyHh59NIsdeq-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/w58DUyHh59NIsdeq-image.png)

</td></tr><tr style="height: 246.205px;"><td style="height: 246.205px;">**Step 2:** Click on the new tag button, and name your tag "relay\_&lt;number&gt;".

<p class="callout info">Replace "&lt;number&gt;" with the relay you want to switch. Use 0 when you are unsure, since this relay is used the most in current implementations.</p>

For the tag type, choose "Boolean".

After that click on the "Save" button.

</td><td style="height: 246.205px;">[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/4wnRt66mU6sIzTua-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/4wnRt66mU6sIzTua-image.png)

</td></tr><tr style="height: 240.841px;"><td style="height: 240.841px;">**Step 3:** Navigate to the "Triggers" tab.

</td><td style="height: 240.841px;">[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/ngFE42puxYI8tsp1-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/ngFE42puxYI8tsp1-image.png)

</td></tr><tr style="height: 214.364px;"><td style="height: 214.364px;">**Step 4:** Create a trigger that will call the script we created every time the tag has changed (as configured in the image), and click on the "Save" or "Save and close" button.

This will switch the relay to the current tag value.

</td><td style="height: 214.364px;">[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/tcONpPthRwBCPyEf-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/tcONpPthRwBCPyEf-image.png)

[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/51EZAXyip3cU47j1-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/51EZAXyip3cU47j1-image.png)

[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/DmB9Fh7KSYlMlFuy-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/DmB9Fh7KSYlMlFuy-image.png)

</td></tr><tr style="height: 113.5px;"><td style="height: 113.5px;">**(Optional) Step 5:** To make the relay switch back to 0 (off) x seconds after being switched to 1 (on), also add the following trigger (as configured in the image), and click on the "Save" or "Save and close" button.  
  
Useful for relays that open doors, barriers, etc.

</td><td style="height: 113.5px;">[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/Xv2zQXiflY7Gthre-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/Xv2zQXiflY7Gthre-image.png)

[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/UNc25E3HQ72fJ3cu-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/UNc25E3HQ72fJ3cu-image.png)

[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/hbzEZeyHAnwq7JRh-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/hbzEZeyHAnwq7JRh-image.png)

</td></tr><tr><td>**Step 6:** Repeat step 3 to 5 for each relay you want to configure.

</td><td></td></tr></tbody></table>

### 4. Testing

<table border="1" id="bkmrk-step-1%3A-click-one-%28o" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 50%;"></col><col style="width: 50%;"></col></colgroup><tbody><tr><td>**Step 1:** Click one (or more) of the configured relay tags.</td><td>[![test_relay.gif](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/AOl7EYRIDMyWHM1O-test-relay.gif)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/AOl7EYRIDMyWHM1O-test-relay.gif)

</td></tr><tr><td>Step 2: Open the created script, check the "Queue and history" tab to see of the script was executed, and check the "Logbook" tab to see if any issues occurred when executing the script.</td><td>[![image.png](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/scaled-1680-/kNwzM213k97DfxiJ-image.png)](https://knowledgebase.cocos.software/uploads/images/gallery/2025-07/kNwzM213k97DfxiJ-image.png)

</td></tr></tbody></table>