Skip to content

Virtual sensor concept

Frederik Walk edited this page Mar 23, 2016 · 6 revisions

Virtual sensors are sensors which have no physically existing counterpart directly producing values. They apply a function to measurements of one or more other sensors (virtual or not). Since sensors have to belong to a device, devices can also be virtual.

Representation in the User Database

Virtual sensors are, like normal sensors, represented in the sensors table. However, they not only have the is_virtual flag, but have a counterpart in the virtual_sensors table. At this place, the formula to calculate its measurements is stored. This entry also has a relationship to the sensors table, defining which symbols in the formula represent which sensor in the database.

Measurements are not inserted into the measurement_raw table, but are rather calculated by a regular maintenance job executing a function based on the stored formula, created by the back-end, working on the already aggregated values.

This way virtual sensors can be handled like normal sensors when reading measurements from the database.

Function generation

Functions are user definable and are mainly a single line of code of a subset of Go.

The subset contains:

  • Unary expressions + and -
  • Binary expressions +,-,*,/,%
  • Integer and Float constants
  • Function calls to predefined functions
  • String literals as placeholders for sensors
  • Parentheses

This expression is then parsed using the go/parser library. This way it can be checked if the function is syntactically correct and contains less than a specified amount of sensors or operators. Based on the parsed AST, a SQL function is generated which can then be written to the database and executed by a maintenance job.

A example parser can be found in the fw-vsensor branch.

Clone this wiki locally