update_sensor

To create, edit, or delete sensors, use the unit/update_sensor method.

Endpoint

Copied!
svc=unit/update_sensor&params={
    "itemId": <long>,
    "id": <long>,
    "callMode": <text>,
    "unlink": <uint>,
    "n": <text>,
    "t": <text>,
    "d": <text>,
    "m": <text>,
    "p": <text>,
    "f": <uint>,
    "c": <text>,
    "vt": <uint>,
    "vs": <long>,
    "tbl": [
        {
            "x": <double>,
            "a": <double>,
            "b": <double>
        }
    ]
}

Parameters

The request must contain the following parameters:

Parameter Description
itemId Unit ID.
id Sensor ID. To create a sensor, pass 0. When you create a sensor, the system ignores this value and assigns the ID itself. The ID of the new sensor is returned in the response.
callMode Action: create, update, delete.

To delete a sensor, you can also pass the following parameter:

Parameter Description
unlink Remove the links to the sensor before deleting it: 1 (yes, default), 0 (no). If you pass 1, the system removes the sensor from the unit properties that use it and sets the validating sensor ID (vs) to 0 in the sensors that use it for validation. These sensors are modified, so their last modification time (mt) changes. If you pass 0 and the sensor is used, error 2015 is returned.

To create or update a sensor, the following parameters are also required:

Parameter Description
n Name.
t Type (see Sensor types).
d Description.
m Unit of measurement.
p Parameter.
f Sensor flags (see Sensor flags).
c Configuration (see Sensor configuration parameters).
vt Validation type (see Validation types).
vs Validating sensor ID.
tbl Calculation table.

Sensor flags

Flag Description
0x01 Sensor type: instant.
0x02 Sensor type: differential.
0x03 Sensor type: differential with overflow (2 bytes).
0x04 Sensor type: switch from off to on.
0x05 Sensor type: switch from on to off.
0x20 Activate the With overflow option.

Can be used for the following sensors:

  • Mileage
  • Absolute fuel consumption
  • Absolute engine hours

If flag 0x20 isn’t set, the change delta is calculated as the absolute difference between the current and previous values. If the flag is set and the current value is lower than the previous one, the delta is equal to the current value (that is, the previous value is assumed to be 0). This flag applies to the sensor value calculation. Overflow handling for differential sensor events is set separately in overflow_limit, regardless of this flag.
0x40 Apply the lower and upper bounds (lower_bound, upper_bound) after calculation. If the flag isn’t set, then each bound is applied to raw data (in the case of FLS, there are difficulties with setting lower and upper bounds for raw data). If the flag is set, then each bound is applied to calculated (processed) data.

Sensor types

Value Sensor type
absolute fuel consumption Absolute fuel consumption sensor
accelerometer Accelerometer
alarm trigger Alarm trigger
battery level Battery level sensor
counter Counter sensor
custom Custom sensor
digital Custom digital sensor
driver Driver assignment
engine efficiency Engine efficiency sensor
engine hours Absolute engine hours
engine operation Engine ignition sensor
engine rpm Engine revolutions sensor
fuel level Fuel level sensor
fuel level impulse sensor Impulse fuel level sensor
impulse fuel consumption Impulse fuel consumption sensor
instant fuel consumption Instant fuel consumption sensor
mileage Mileage sensor
odometer Relative odometer
private mode Private mode
real-time motion sensor Real-time motion sensor
relative engine hours Relative engine hours
tag Passenger sensor
temperature Temperature sensor
temperature coefficient Temperature coefficient
trailer Trailer assignment
voltage Voltage sensor
weight Weight sensor

For further information about sensor types, see Sensor types.

Sensor configuration parameters

The following parameters are used to configure a sensor:

Copied!
"{
  \"act\": <bool>,
  \"appear_in_popup\": <bool>,
  \"battery_capacity\": <double>,
  \"ci\": <object>,
  \"calc_fuel\": <uint>,
  \"engine_efficiency\": <array>,
  \"engine_sensors\": <array>,
  \"filter\": <long>,
  \"fuel_params\": <object>,
  \"mu\": <uint>,
  \"pos\": <uint>,
  \"show_time\": <bool>,
  \"unbound_code\": <text>,
  \"validate_driver_unbound\": <bool>,
  \"do_not_show\": <bool>,
  \"timeout\": <uint>,
  \"uct\": <bool>,
  \"lower_bound\": <double>,
  \"upper_bound\": <double>,
  \"overflow_limit\": <double>,
  \"text_params\": <uint>,
  \"show_as_percentage\": <uint>
}"

The c parameter is sent as a string containing a JSON object. Enclose the object in double quotes and escape the double quotes in its keys and values ("). See the configuration examples below.

Set sensor flags in the top-level f parameter. Don’t set c.flags directly. The system stores the value of f in the flags key of the c string. That’s why, if f isn’t 0, the returned c string contains this key as a string value, for example, \"flags\":\"1\". If the c string that you pass already contains the flags key, the value of f is ignored.

If the c value isn’t valid JSON, the system ignores it and doesn’t return an error. The sensor is saved with an empty configuration, which contains only the flags key if f isn’t 0.

Parameter Description
act Sensor value calculation: 0 (based on the last unit message), 1 (based on the last unit parameters).
This option uses inverted logic:
  • Enabled state (value 0) refers to the older, historic logic version.
  • Disabled state (value 1) refers to the newer logic version (data is used for popups, which is useful when important parameters are received infrequently).
By default, the older logic version is selected.
appear_in_popup Pass true to enable the Visible option, or false to disable it.
battery_capacity Battery capacity of a battery level sensor, in kWh. Used together with show_as_percentage to display the battery level as a percentage.
ci Custom value intervals. See the configuration examples.
calc_fuel Specify a nonzero value to enable data calculation by the sensor, or 0 to disable it. The key is used by fuel level, absolute fuel consumption, impulse fuel consumption, instant fuel consumption, and battery level sensors. For fuel sensors, it defines whether the sensor is used to calculate fuel consumption and, for fuel level sensors, to detect fuel fillings and drains. For a battery level sensor, this enables battery level and charge calculation when the sensor is used with the battery_level, charge, or ev detector.

If you don’t specify this key, the system uses the flag of the corresponding sensor type in the calcTypes field of the unit fuel settings: 0x02 for fuel level and battery level sensors, 0x08 for absolute fuel consumption sensors, 0x10 for impulse fuel consumption sensors, and 0x20 for instant fuel consumption sensors.
engine_efficiency Array of IDs of the engine efficiency sensors bound to an engine ignition, absolute engine hours, or relative engine hours sensor. Corresponds to the Binding engine efficiency sensors option. The system takes this key into account only if the engine sensor is specified in engine_sensors of a fuel level sensor. If none of these engine sensors has engine efficiency sensors bound, the system uses all the engine efficiency sensors of the unit. See the configuration examples.
engine_sensors Array of IDs of the engine ignition, absolute engine hours, and relative engine hours sensors bound to a fuel level sensor. Corresponds to the Binding engine sensors option. The system uses these sensors to calculate fuel consumption mathematically for this fuel level sensor, for example, to replace invalid values. IDs of nonexistent sensors and sensors of other types are ignored. If the array is empty, the key isn’t specified, or none of the IDs is valid, the system uses all the engine sensors of the unit. See the configuration examples.
filter Redefines the filtering level for the sensor. Integer from 0 to 255. Applies a smoothing filter to the sensor value. Valid for the following sensors:
  • Accelerometer
  • Custom
  • Engine revolutions
  • Impulse fuel level
  • Temperature
  • Voltage
  • Weight
Fuel level and battery level sensors use a separate filtering setting. See fuel_params in the configuration examples.
fuel_params Settings of a fuel level, impulse fuel consumption, or battery level sensor. See the configuration examples.
mu Measurement system:
  • 0: SI
  • 1: US
  • 2: imperial
  • 3: metric with gallons
pos Sensor position in the list (count from 1).
show_time Pass true to display the sensor value with the time since which this value was received, or false to hide the time. For further information, read about the Time option on the Sensors page.
unbound_code For the driver or trailer assignment sensors, you can specify a custom separation code.
validate_driver_unbound Validate separation: 0 (no), 1 (yes).
do_not_show This parameter is used to configure the Private mode sensor. Set it to 1 to prevent the unit location data from being transmitted when the sensor is on, or 0 to allow this data to be received.
timeout Timeout, seconds.
uct Pass 0 to enable the Overflow by raw data option. Pass 1 to disable it. The option is available only for differential counter sensors with overflow.

The option changes the order in which the calculation table is applied in reports. It doesn’t affect how overflow_limit is applied to differential sensor events.
lower_bound Lower bound of valid sensor values for the calculation table.
upper_bound Upper bound of valid sensor values for the calculation table.
overflow_limit Value at which the sensor counter resets to zero. The system uses it to calculate differential sensor events for the following sensors:
  • Mileage
  • Absolute engine hours
  • Absolute fuel consumption
  • Impulse fuel consumption
  • Differential counter
If the current value is lower than the previous value and the limit is greater than 0, the delta is calculated as (overflow_limit - previous value) + current value. If the limit is 0 or isn’t set, the delta is equal to the current value. The system applies this key regardless of the 0x20 flag, which controls overflow handling in the sensor value calculation. Specify the limit in processed values, that is, in the values obtained after the calculation table is applied. The uct key doesn’t affect this behavior.

For impulse fuel consumption sensors, this key affects events only. To set the overflow value for fuel consumption calculation in reports, use fuel_params.maxImpulses, which corresponds to the Maximum impulses option. Unlike overflow_limit, maxImpulses is applied to raw values, before the calculation table.
text_params Pass 1 to enable the Text parameters option, or 0 to disable it.
show_as_percentage Pass 1 to display the battery level as a percentage of the value specified in battery_capacity, or 0 to display the value in kWh. If battery_capacity is missing or isn’t a positive number, the value is displayed in kWh. See the Convert values to % option.

Configuration examples

In the ci parameter, each key is the lower bound of an interval, and each value describes how the sensor value is displayed in this interval:

Copied!
"c":"{\"ci\":{\"0\":{\"t\":\"Off\",\"c\":16711680},\"50\":{\"t\":\"On\"}}}"
Key Description
t Text displayed instead of the sensor value.
c Optional text color, specified as an integer in decimal format.

To set an interval without a lower bound, use the "-Infinity" key.

Example of binding the engine sensors with IDs 3 and 5 to a fuel level sensor:

Copied!
"c":"{\"engine_sensors\":[3,5]}"

Example of binding the engine efficiency sensor with ID 7 to an engine ignition sensor. This binding is taken into account only for the fuel level sensors that have this engine ignition sensor in engine_sensors:

Copied!
"c":"{\"engine_efficiency\":[7]}"

Example of passing fuel_params of a fuel level sensor:

Copied!
"c":"{\"fuel_params\":{\"flags\":1728,\"ignoreStayTimeout\":20,\"minFillingVolume\":21,\"minTheftTimeout\":0,\"minTheftVolume\":15,\"filterQuality\":0,\"fillingsJoinInterval\":300,\"theftsJoinInterval\":300,\"extraFillingTimeout\":0}}"

Example of passing fuel_params of an impulse fuel consumption sensor:

Copied!
"c":"{\"fuel_params\":{\"maxImpulses\":10,\"skipZero\":0}}"

For fuel level and impulse fuel consumption sensors, the fuel_params object overrides the unit fuel settings (fuelLevelParams and fuelConsImpulse). If you don’t pass the object, the system uses the settings of the unit. If you pass the object without some of the keys, the system doesn’t take these keys from the unit settings. It uses the values from the table below.

Sensor type Key Value used if the key isn’t specified
Fuel level flags 1
Fuel level ignoreStayTimeout 20
Fuel level minFillingVolume 20
Fuel level minTheftTimeout 0
Fuel level minTheftVolume 10
Fuel level filterQuality 0
Fuel level fillingsJoinInterval 300
Fuel level theftsJoinInterval 300
Fuel level extraFillingTimeout 0
Impulse fuel consumption maxImpulses 0
Impulse fuel consumption skipZero 0

The keys have the same meaning as the fields of the unit fuel settings. For the list of flags, see unit/get_fuel_settings. To replace invalid values with those calculated mathematically, add the 0x1000 flag to flags.

Specify minFillingVolume and minTheftVolume in the measurement system set in the mu key of the sensor: liters for SI (also used if mu isn’t specified), US gallons for US and metric with gallons, or imperial gallons for imperial. If the sensor has no fuel_params object, the measurement system of the unit is used.

Example of passing fuel_params of a battery level sensor:

Copied!
"c":"{\"fuel_params\":{\"flags\":16387,\"filterQuality\":0,\"minFillingVolume\":10,\"fillingsJoinInterval\":300}}"

For a battery level sensor, the system uses the settings listed below. If you don’t pass the fuel_params object at all, the system uses the fuel settings of the unit. If you pass the object without some of the keys, the system uses the values from the table.

Key Description
flags Flags of charge detection and filtering. The values are the same as for a fuel level sensor. For the list, see unit/get_fuel_settings.

If you don’t specify this key, the system uses 1, which merges the values of sensors with the same name. To turn the merging off, specify a value without the 0x01 flag. If the value includes the 0x2000 flag, the merging stays on regardless of the other flags you pass.
filterQuality Median filtering level (0-255). Applied when median filtering is enabled in flags. Ignored when adaptive filtering is enabled. If you don’t specify this key, the system uses 0.
minFillingVolume Minimum charge volume, in kWh. If you don’t specify this key, the system uses 20 kWh. The default value in the Minimum charge volume field of the interface is 10 kWh. To apply the same threshold through the API, specify this key explicitly.
fillingsJoinInterval Timeout to separate consecutive charges, in seconds. If you don’t specify this key, the system uses 300 seconds.

The system uses these settings only when data calculation by the sensor is enabled in calc_fuel. This setting corresponds to the Calculate data by the sensor option. The calculated values are returned by the battery_level, charge, and ev detectors. For details, see the Event detector types section.

The show_as_percentage key affects display only. Always specify battery_capacity and charge detection settings in kWh.

Example of a c string for a battery level sensor that sets both charge detection and percentage display:

Copied!
"c":"{\"fuel_params\":{\"flags\":16387,\"filterQuality\":0,\"minFillingVolume\":10,\"fillingsJoinInterval\":300},\"show_as_percentage\":1,\"battery_capacity\":64.5,\"calc_fuel\":2,\"act\":1,\"mu\":0,\"appear_in_popup\":true}"

Pass show_as_percentage and battery_capacity inside the c string, next to fuel_params, and keep the m parameter of the sensor set to kWh.

The filtering type is set in flags: use 0x02 to enable filtering and add 0x4000 for adaptive median filtering. If you set the 0x2000 flag, the system uses only the filtering flags and the filterQuality value of the sensor.

For battery level sensors, the 0x2000 flag works in the opposite way to its description on the unit/get_fuel_settings page: it doesn’t apply the default fuel settings of the unit.

The system always adds the calculation-by-time mode (0x10) for battery level sensors. You can’t disable it through this method.

For battery level sensors, the system resets the ignoreStayTimeout, minTheftVolume, and extraFillingTimeout keys to 0, even if you specify other values. The minTheftTimeout and theftsJoinInterval keys have no effect because drain detection isn’t available for these sensors.

Validation types

Validation type flag Description
0x01 Logical AND
0x02 Logical OR
0x03 Math AND
0x04 Math OR
0x05 Sum up
0x06 Subtract validator from sensor
0x07 Subtract sensor from validator
0x08 Multiply
0x09 Divide sensor by validator
0x0A Divide validator by sensor
0x0B Not-null check
0x0C Replace sensor with validator in case of error

Response

If the request to create or update a sensor is completed successfully, a response in the following format is returned:

Copied!
[
  <long>,  /* Sensor ID */
  {
    "id": <long>,       /* Sensor ID */
    "n": "<text>",      /* Name */
    "t": "<text>",      /* Type */
    "d": "<text>",      /* Description */
    "m": "<text>",      /* Unit of measurement */
    "p": "<text>",      /* Parameter */
    "f": <uint>,        /* Sensor flags */
    "c": <text>,        /* Configuration, sent and returned as a JSON string */
    "vt": <int>,        /* Validation type */
    "vs": <long>,       /* Validating sensor ID */
    "tbl": [            /* Calculation table */
      {
        "x": <double>,
        "a": <double>,
        "b": <double>
      }
    ],
    "ct": <uint>,       /* Creation time (Unix timestamp) */
    "mt": <uint>        /* Last modification time (Unix timestamp) */
  }
]

In Wialon Local, the ct and mt fields are available starting from version 2304.

The response to an update request doesn’t always show the changes. If the request changes only the name (n), description (d), or unit of measurement (m), the changes are saved, but the response contains the sensor as it was before the request, including the old mt value. If any other sensor property changes, the response contains the updated sensor. To get the current sensor properties, use the core/search_item request with the flag of sensors 0x00001000 (4096). In Wialon Local, this applies starting from version 2504. In earlier versions, the response always contains the updated sensor.

If the request to delete a sensor is completed successfully, a response in the following format is returned:

Copied!
[
  <long>,   /* Sensor ID */
  null
]

If the request fails, an error code is returned.

Error codes

Error code Description
4 Wrong input parameters: a required parameter is missing or has a wrong type, callMode has an unsupported value, or a row of the calculation table (tbl) doesn’t contain exactly three keys (x, a, b) or contains invalid values.
6 The operation failed: the sensor with the specified id doesn’t exist (update and delete), the sensor couldn’t be saved (for example, n or t is empty), the links to the sensor couldn’t be removed (delete), or the event detectors of the unit couldn’t be updated after the change. In the last case, the sensor is already created, updated, or deleted.
7 The unit doesn’t exist, or the user has no Create, edit, and delete sensors access right to it.
2015 Unable to delete the sensor because it is used in the advanced properties of the unit or in the properties of another sensor, and unlink is 0.

If the sensor type (t) doesn’t exactly match one of the values listed in Sensor types, the request can return error 6 even though the sensor is already created, updated, or deleted. Don’t repeat a create request after this error, because each retry creates one more sensor. To check the result, get the unit sensors with the core/search_item request. In Wialon Local, this applies starting from version 2504. In earlier versions, the request is completed successfully in this case.

If you find a mistake in the text, please select it and press Ctrl+Enter.