Sensors - senseobservationsystems/commonsense-imp GitHub Wiki

Home > Function Documentation > Sensors

Sensors are what matter most in CommonSense. You will need sensors for (nearly) everything you do in CommonSense.

####GetSensorArray(p_device_id, p_session_id)

List all sensors belonging to device p_device_id.

Parameter Description
p_device_id the device id to retrieve sensors from
p_session_id a session id used to identify yourself

Returns
An array containing the sensor objects or the response status code in case of failure.

Example

local sensor_array = GetSensorArray(654321, "1234567890abc0123.98765432");

server.log("device 654321 contains the following sensors:");
for(local i = 0; i < sensor_array.len();i++){
    server.log(sensor_array[i].name + " with id " + sensor_array[i].id);
}

####GetSensorId (p_name, p_display_name, p_device_type, p_data_type, p_data_structure, p_sensor_array)

Looks through p_sensor_array for a sensor with the provided parameters.

Parameter Description
p_name a name used to identify the sensor
p_display_name name displayed in the dashboard (optional)
p_device_type description of the device
p_data_type type of the data e.g. int, string, json
p_data_structure string containing the json data structure, required when using json
p_sensor_array the sensor array to look through

Returns
The id belonging to a sensor with the provided parameters or null if it does not exist.

Example

local sensor_array = GetSensorArray(654321, "1234567890abc0123.98765432");
local sensor_id = GetSensorId("gl5528", "ldr", "photoresistor", "json", "\"{\\\"lux\\\":\\\"Integer\\\"}\"", sensor_array);

if(sensor_id != null){
    server.log("A sensor with these attributes already exists! its sensor id is: " + sensor_id);
}else{
    server.log("No such sensor exists, create one!");
}

####CreateSensor (p_name, p_display_name, p_device_type, p_data_type, p_data_structure, p_uuid, p_session_id)

Creates a sensor using the provided parameters.

Parameter Description
p_name a name used to identify the sensor
p_display_name name displayed in the dashboard (optional)
p_device_type description of the device
p_data_type type of the data e.g. int, string, json
p_data_structure string containing the json data structure, required when using json
p_uuid unique id of the imp to add this sensor to
p_session_id a session id used to identify yourself

Returns
The id of the created sensor or the return status code in case of failure.

Dependencies
This function requires the following other library functions:

  • GetDeviceArray
  • GetDeviceId
  • GetSensorArray
  • GetSensorId
  • AddSensorToDevice

Examples

local photoresistor_id = CreateSensor("gl5528", "ldr", "photoresistor", "json", "\"{\\\"lux\\\":\\\"Integer\\\"}\"", uuid, session_id);

server.log("photoresistor_id: " + photoresistor_id);
⚠️ **GitHub.com Fallback** ⚠️