How to generate a sensor_config.xml from a CSV file
- 1 Using convertcsv.com web service
- 2 Locally with a bash script
- 2.1 Variables
- 2.2 Example
- 2.2.1 Source CSV File
- 2.2.2 Script
- 2.2.3 Result
- 2.2.3.1 Sensor Config
- 3 Final Steps
Using convertcsv.com web service
This webservice allows sohisticated conversions. I prepared some templates for it to do common conversions. Follow the steps below and then choose the right template.json and format the input for the different use cades as outlined below.
Import the according template JSON in Load Form
Paste the list of sensors into the input field
Click Convert CSV Using Template and afterwards Download Result
Simple Sensor Config
This generates a config without severities or sensor classes.
Load the Template File
Insert the symbol list with the format as pre loaded in Input
Input
ErsterSensor zweiterSensor DritterSensorClick Convert CSV Using Template and afterwards Download Result
Action Rule Config
This generates the action rule block from a list of sensor names and comments. The comments will be transformed and used as rule name.
Load the Template File
Insert the symbol list with the format as pre loaded in Input
Input
ErsterSensor;This sensor does smth bla zweiterSensor;Whatever i type her ebecomes the new sensor name DritterSensor;evene special chars like (/&(/& will be replaced with _Click Convert CSV Using Template and
Locally with a bash script
The following snippet creates a part of a sensor_config.xml from an CSV file and saved it into a file named generated_sensor_config.xml. It is not creating a full configuration but the sensor related parts necessary.
Adjust the variables to match your CSV
Change the variables sensor_name,register (MODBUS register) and device_name to match the column number of the CSV. For example if you set sensor_name=$5, it will use column number five to extract the sensor name
#!/bin/sh
#
# This is just a bare snippet, you have to change below parameters to work for other files.
#
# It creates the <sensor></sensor> part of a sensor config XML from a CSV list
#
printf "\n\n\nCopy below this line.\n---------------------------------------------------\n" && \
awk -F\; '{ device_name="gw_Wago_Modbus_TCP"; sensor_name=$1; register=$7; \
print \
"<sensor sensor_id=\"" sensor_name "\">\
\t<sensor_class>unknown</sensor_class>\
\t<sensor_gateway sensor_gateway_id=\"" device_name "\">\
\t\t<demux>\
\t\t\t<keys>\
\t\t\t\t<key>"register"</key>\
\t\t\t</keys>\n\t\t</demux>\
\t</sensor_gateway>\
</sensor>"};' wagomodbus2.csv > sensor_config_Sebastian.xmlVariables
The script uses three variables that have to be set to match your CSV.
device_name
The vendor or model of the actual device that provides the sensors, e.g. a Advantech_ADAM or a B_and_R_BC0087. This is a custom string that you can define individually.
Pro Tip: A best practice is toi prefix the device name with gw (Gateway). The device name is taken to fill the <sensor_gateway> tag, prefixing it helps in larger configs to differ between the different object types like devices, sensors and sensor gateways.
sensor_name=<column>
The column number that holds the string to be used as the sensor name for each register, typically the description or human friendly name of the register.
register=<column>
The column number that holds the numeric register. This will be used within the demux section of the sensor config.
<demux>
<keys>
<key> REGISTER </key>
</keys>
</demux>Example
Source CSV File
Root.750_494.rMinimumRmsCurrentL1;MD42;1000;0;System.Single;255;412372;2;0
Root.750_494.rAverageRmsCurrentL1;MD43;1000;0;System.Single;255;412374;2;0
Root.750_494.rPeakCurrentL1;MD44;1000;0;System.Single;255;412376;2;0In this example, we'll find the sensor name in column one, the Modbus register in column seven. The registers come from a Wago Modbus device, so we set this as our device name.
Script
#!/bin/sh
#
# This is just a bare snippet, you have to change below parameters to work for other files.
#
# It creates the <sensor></sensor> part of a sensor config XML from a CSV list
#
printf "\n\n\nCopy below this line.\n---------------------------------------------------\n" && \
awk -F\; '{ device_name="gw_Wago_Modbus_TCP"; sensor_name=$1; register=$7; \
print \
"<sensor sensor_id=\"" sensor_name "\">\
\t<sensor_class>unknown</sensor_class>\
\t<sensor_gateway sensor_gateway_id=\"" device_name "\">\
\t\t<demux>\
\t\t\t<keys>\
\t\t\t\t<key>"register"</key>\
\t\t\t</keys>\n\t\t</demux>\
\t</sensor_gateway>\
</sensor>"};' wagomodbus2.csv > sensor_config_Sebastian.xmlResult
Sensor Config
<sensor sensor_id="Root.750_494.rMinimumRmsCurrentL1">
<sensor_class>unknown</sensor_class>
<sensor_gateway sensor_gateway_id="gw_Wago_Modbus_TCP">
<demux>
<keys>
<key>412372</key>
</keys>
</demux>
</sensor_gateway>
</sensor>
<sensor sensor_id="Root.750_494.rAverageRmsCurrentL1">
<sensor_class>unknown</sensor_class>
<sensor_gateway sensor_gateway_id="gw_Wago_Modbus_TCP">
<demux>
<keys>
<key>412374</key>
</keys>
</demux>
</sensor_gateway>
</sensor>
<sensor sensor_id="Root.750_494.rPeakCurrentL1">
<sensor_class>unknown</sensor_class>
<sensor_gateway sensor_gateway_id="gw_Wago_Modbus_TCP">
<demux>
<keys>
<key>412376</key>
</keys>
</demux>
</sensor_gateway>
</sensor>Final Steps
The script only creates the sensor components for the final config. It will need to be embedded into a full Sensor Config XML that has the device section as well as a sensor gateway in it.