A scripted signal is a signal that is computed on the fly as defined by a script, unlike signals described in a signal database.
For example, let's imagine that you want to subscribe to the value representing the percentage of battery remaining in your vehicle, however, your signal database only defines signals for remaining battery capacity and total battery capacity.
With a simple Lua script, it is possible to define a scripted signal that returns the battery percentage. You can find an example of such a script in our samples repository: Vehicle.Powertrain.TractionBattery.StateOfCharge.Displayed.lua.
Defining a scripted signal
There are three things that need to be put in place:
- an interface with a type "scripted" in the broker configuration file
- a scripted signals database file (defined in JSON) that is referenced by the scripted interface
- a directory with Lua scripts that define the computations and is referenced in the scripted signals database file
Let's say you wish to subscribe to a signal named
Vehicle.Speed, but your signal database names it as
One option is to create a Lua script that will simply pass the same value of
ID257DIspeed.DI_vehicleSpeed under a new name.
A more convenient way is to use a feature called "simple mapping". When the only computation you wish to perform is to rename a signal, you can define the mapping between old and new names in the scripted signals database JSON file. Take a look at an example for inspiration.
Changing frequency of signals
A specific kind of computation you can perform with scripted signals is to change the frequency of emitted signals.
Let's say you wish to change the frequency to 10 updates per second. Take a look at an example script for inspiration on how to achieve that.