# MosUtils

The MosUtils object provides some useful methods and fields for working with expressions.

{% hint style="warning" %}
Please note that calling setXXXProperty() function of the *MosUtils* object will immediately put the script module into demo mode, if it is not licensed
{% endhint %}

### Methods

| **Name**                                                | **Description**                                                                                                                                                                                                                                             |
| ------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| getTagValueState(path)                                  | Returns a ValueState object for the specified tag. The path to the tag is specified as a string, e.g. "*Area1.Tank1.Level*". Note that using a data trigger to obtain a tag value is more productive than calling this method.                              |
| writeTagValueSync(path, value)                          | Writes a value to the specified tag and returns a ValueState object as a result of the method. The method is blocking, i.e. the execution of the expression code will be paused until the record result is returned. **Be careful when using this method!** |
| getExpressionValueState(path)                           | Returns a ValueState object for the specified expression. The path to the expression is specified as a string, e.g. *"Elec.Power"*.                                                                                                                         |
| getDeviceProperty(groupName, path, propertyName)        | Returns the value of a property of the device specified in the method arguments, where *groupName* is the name of the group, *path* is the path to the device or subdevice, *propertyName* is the name of the property.                                     |
| getStoreProperty(name, propertyName)                    | Returns the value of a property of the store specified in the method arguments, where *name* is the name of the store and *propertyName* is the name of the property.                                                                                       |
| getTagProperty(path, propertyName)                      | Returns the value of the property specified in *propertyName* for the tag specified in *path*.                                                                                                                                                              |
| setDeviceProperty(groupName, path, propertyName, value) | Sets *value* of the property specified in *propertyName* for a device where *groupName* is the group in which the device is located and *path* is the device name or path to the subdevice. Also see the *commitChanges()* method.                          |
| setStoreProperty(name, propertyName, value)             | Sets *value* of the property specified in *propertyName* for the store named *name*. Also see the *commitChanges()* method.                                                                                                                                 |
| setTagProperty(path, propertyName, value)               | Sets *value* of the property specified in *propertyName* for the tag specified by the argument *path*. Also see the *commitChanges()* method.                                                                                                               |
| commitChanges()                                         | Commit changes in any properties set by the *setXXXProperty* functions. **This method must necessarily be called after calling any function of the&#x20;*****setXXXProperty*****&#x20;type.**                                                               |
| sleep(millis)                                           | Suspends script execution for the number of specified milliseconds.                                                                                                                                                                                         |
| log(eventType, message)                                 | Outputs a message to the server log, allows tracing script execution, where eventType is numeric from 1 to 3 (1 - INFO, 2 - WARNING, 3 - ERROR) and message is the message text.                                                                            |

### Fields <a href="#h.o1rfpn6or86b" id="h.o1rfpn6or86b"></a>

| **Name**  | **Description**                                                                                                                                     |
| --------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| doNothing | If you return this property as a result of an expression or throw this property as an exception, the state of the expression will remain unchanged. |

### Examples

The following example demonstrates obtaining the *CommunicationEstablished* property for a Modbus device.

```javascript
return MosUtils.getDeviceProperty('', 'MyPLC', 'CommunicationEstablished');
```

or (if the device is a subdevice and is located in a group):

```javascript
return MosUtils.getDeviceProperty('MyGroup', 'MyPLC.SubDevice', 'CommunicationEstablished');
```

In the following example, the expression updates its state only if the difference from the previous value is more than 20. In this example, the expression has a data trigger named *radius*.

```javascript
const DELTA = 20;

// Getting the current value of the expression
var prevValue = context.expression.currentState.value;

// Getting the new value
var newValue = context.data['radius'].state.value

if (Math.abs(newValue - prevValue) > DELTA)
  return newValue;

return MosUtils.doNothing;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.monokot.io/scripts/api/mosutils.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
