Skip to main content

State

State is an unstructured form of global data which is uniquely identifiable.

State Types

State
{
organizationId: string,
id: string,
value: string,
updatedUtc: datetime,
changedUtc: datetime,
createdUtc: datetime
}
tip

State values are strings, but can be converted to other data types using other built-in functions such as parseInt() or parseFloat().

setState

Read-only Mode

This function is not available in read-only mode.

Sets a new State value.

setState(stateId, value)

Parameters

NameTypeDescription
stateIdstringThe identifier of the State to retrieve.
valuestringA new or updated State value.

Returns The newly created or updated State.

Example

var state = setState('office-light', 'off');
info

This function will either create or update an existing State.

deleteState

Read-only Mode

This function is not available in read-only mode.

Deletes a State with a given identifier.

deleteState(stateId)

Parameters

NameTypeDescription
stateIdstringThe identifier of the State to delete.

Example

deleteState('office-temp');
danger

This performs a hard delete and will not be recoverable afterwards. Delete with caution.

deleteStates

Read-only Mode

This function is not available in read-only mode.

Deletes multiple States with a given collection of identifiers.

deleteStates([stateIds])

Parameters

NameTypeDescription
stateIdsstring[]The identifiers of the State to delete.

Example

deleteStates(['office-temp', 'office-humidity']);
danger

This performs a hard delete and will not be recoverable afterwards. Delete with caution.

stateExists

Checks whether a State with an identifier exists.

stateExists(stateId)

Parameters

NameTypeDescription
stateIdstringThe identifier of the State to check.

Returns A boolean on whether the State exists.

Example

var exists = stateExists('office-device');

if (exists) {
// The State for a device exists
}

getState

Gets a State by its identifier.

getState(stateId)

Parameters

NameTypeDescription
stateIdstringThe identifier of the State to retrieve.

Returns The State with the specified stateId identifier.

Example

var state = getState('office-light');

if (state.value == 'on') {
// The office light is currently on
}

getStates

Gets a collection of States by their identifiers.

getStates([stateIds])

Parameters

NameTypeDescription
stateIdsstring[]The identifiers of the States to retrieve.

Returns A map of States with the specified stateIds identifiers.

Example

var states = getStates(['office-light1', 'office-light2']);

if (states['office-light1'].value == 'on'
|| states['office-light2'].value == 'on') {
// Both office lights are on
}