Calculation examples
The Initiate attribute node in IoT Logic supports a wide range of calculations through Navixy IoT Logic Expression Language. This guide provides practical examples of common calculations to help you extract maximum value from your IoT data.
Important considerations
When creating calculations, keep these points in mind:
Attribute names validity: Make sure you use correct attribute names in calculations. You can look up existing attribute names using Data Stream Analyzer, or insert them with gShort syntax using autocomplete.
Data validity: Ensure your expressions handle potential null values or invalid readings gracefully
Performance impact: Complex calculations with many nested functions may impact processing speed for high-frequency data
Mathematical constraints: Functions like logarithm and square root require positive input values
Historical references: When using indexed values (e.g.,
value('param', 1, 'valid')), ensure you have sufficient historical data and use full expression syntax
Basic calculations
Unit conversions
Converting measurements between different units is one of the most common operations in IoT data processing.
Speed conversion (km/h to mph)
can_speed/1.609Practical application: This conversion standardizes vehicle speed data for regions using imperial measurements. By performing this calculation in IoT Logic rather than in downstream applications, you ensure consistency across all systems consuming the data.
Temperature conversion (Celsius to Fahrenheit)
temperature * 1.8 + 32Practical application: This conversion makes temperature readings comprehensible for users more familiar with Fahrenheit measurements. It's especially valuable for multinational organizations that operate across regions with different measurement standards.
Calculating differences
Comparing current readings with previous values helps identify changes and trends in your data.
Temperature change detection
temperature - value('temperature', 1, 'valid')Practical application: This calculation helps detect rapid temperature fluctuations that might indicate equipment issues or environmental changes. By creating a dedicated attribute for this difference, you can set up alerts for sudden changes without complex downstream processing.
Time-based calculations
Time calculations help you understand device behavior over time and measure operational patterns.
Finding time elapsed between last valid readings
srvTime('avl_25', 0, 'valid') - srvTime('avl_25', 1, 'valid')Practical application: This calculation measures the interval between consecutive data transmissions, which can help identify communication issues or validate that devices are reporting at expected frequencies. This example showcases the calculation between the last two valid readings, but can be adjusted to handle any two historical values.
Converting timestamp to human-readable format
dtFormat(genTime('can_fuel_1', 0, 'all'))Practical application: Converting Unix timestamps to ISO 8601 format makes the data more readable in logs and reports. This format is widely supported by analysis tools and databases, simplifying integration with other systems.
Advanced mathematical functions
IoT Logic supports sophisticated mathematical operations through its built-in math functions.
Rounding values
Round temperature to nearest integer
math:round(value('temperature_2', 0, 'valid'))Practical application: Rounding is valuable when exact precision isn't needed or when you want to reduce noise in sensor readings. For instance, rounding environmental sensor data might be sufficient for general climate monitoring while reducing storage requirements. This function is also useful when creating categories or bands of values (e.g., grouping temperature readings into 5-degree increments).
Logarithmic calculations
Natural logarithm of a value
math:log(value('temperature_2', 0, 'valid'))Practical application: Logarithmic transformations are particularly useful for:
Compressing data that spans multiple orders of magnitude into a more manageable range
Converting exponential relationships into linear ones for easier analysis
Working with certain sensor types that have logarithmic response characteristics
Sound level calculations, where decibels are logarithmic units
pH measurements in environmental monitoring
Square root operations
Calculate square root of a reading
math:sqrt(value('temperature_2', 0, 'valid'))Practical application: Square root functions are valuable for:
Converting between power and amplitude in electrical measurements
Calculating standard deviations in statistical analysis of sensor data
Determining root mean square (RMS) values for electrical parameters
Distance calculations in multi-dimensional space (e.g., trilateration for positioning)
Normalizing certain types of sensor data
Combined operations
You can create even more complex calculations by combining multiple functions and operations.
Geometric mean with rounding
math:round(math:sqrt(value('temperature_1', 0, 'valid') * value('temperature_2', 0, 'valid')))Practical application: This calculation computes the geometric mean of readings from two temperature sensors and rounds the result to provide a cleaner value. The geometric mean is often more appropriate than an arithmetic average when dealing with rates, ratios, or measurements where multiplication is the natural way to combine values.
Standardized value calculation
(value('sensor_reading', 0, 'valid') - value('sensor_min', 0, 'valid')) / (value('sensor_max', 0, 'valid') - value('sensor_min', 0, 'valid')) * 100Practical application: This normalizes a sensor reading to a percentage scale from 0-100%, allowing for standardized comparisons across different sensors with varied ranges. This is particularly useful for creating uniform dashboards or triggering alerts based on relative values rather than absolute measurements.
Last updated
Was this helpful?