mO SharemO Share

How to generate a sensor_config.xml from a CSV file

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.

  1. Open Convertcsv.com - Template Import
  2. Import the according template JSON in Load Form

  3. Paste the list of sensors into the input field

    There is no feedback that the form was loaded successfully after uploading the template but it works.

  4. Click Convert CSV Using Template and afterwards Download Result

Simple Sensor Config

This generates a config without severities or sensor classes.

  1. Load the Template File 
      File Modified

    File convertcsv-com_sensor_config_simple 07-2018.json Changed schmea_version and added parsing for headings

    Jul 30, 2018 by Sebastian Koch
  2. Insert the symbol list with the format as pre loaded in Input

    Input
    ErsterSensor
    zweiterSensor
    DritterSensor
  3. Click 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.

  1. Load the Template File 
      File Modified

    File convertcsv-com_action_rules_from_alarms.json

    Mar 29, 2018 by Sebastian Koch
  2. 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 _
  3. 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.xml

Variables

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;0

In 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.xml

Result

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 /wiki/spaces/SOD/pages/24379410 that has the device section as well as a sensor gateway in it.