hello, I decided to write and share with you a script for downloading data from the SAJ inverter and presenting the data in PRTG. The original inverter tracking software is very limited and as I have PRTG instance at home on my ESX, I decided to use it for these purposes. Technically this is a advanced server which generates xml. It generates 35 channels. All data came from csv which is loaded from device interface. All you need to change ip to your.
function Get-Data {
param(
[string]$DeviceIP
)
[int]$LocalDevicePort = "80"
$url = "http://"+ $deviceIP +"/status/status.php"
$test = test-netconnection -ComputerName $deviceIP -Port $LocalDevicePort
if ($test) {
$method = "GET"
$WebResponse = Invoke-WebRequest -Uri $url -Method $method
$Content = $WebResponse.Content
Return $content
} else {
Write-Output "host not responding"
}#end if
}
function Get-Table {
param(
[string]$DataString
)
$statusfieldnames = @('Statistics', 'Total_Generated', 'Total_Running Time', 'Today_Generated', 'Today_Running_Time', 'PV1_Voltage', 'PV1_Current', 'PV2_Voltage', 'PV2_Current', 'PV3_Voltage', 'PV3_Current', 'PV1_StrCurr1', 'PV1_StrCurr2', 'PV1_StrCurr3', 'PV1_StrCurr4', 'PV2_StrCurr1', 'PV2_StrCurr2', 'PV2_StrCurr3', 'PV2_StrCurr4', 'PV3_StrCurr1', 'PV3_StrCurr2', 'PV3_StrCurr3', 'PV3_StrCurr4', 'Grid-connected_Power', 'Grid-connected_Frequency', 'Line1_Voltage', 'Line1_Current', 'Line2_Voltage', 'Line2_Current', 'Line3_Voltage', 'Line3_Current', 'Bus_Voltage', 'Device_Temperature', 'CO2emission_Reduction', 'Other_Status')
$unitsfieldnames = @( 'na', 'kWh', 'h', 'kWh', 'h', 'V', 'A', 'V', 'A', 'V', 'A', 'V', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'W', 'Hz', 'V', 'A', 'V', 'A', 'V', 'A', 'V', 'C', 'Kg', 'na')
$divisorfields = @( '1', '100', '10', '100', '10', '10', '100', '10', '100', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '100', '10', '100', '10', '100', '10', '100', '10', '10', '10', '1')
$ValueArray = $DataString.split(',')
if (($statusfieldnames.count) -eq ($ValueArray.count)) {$max = $statusfieldnames.count}
$result = for ($i = 0; $i -lt $max; $i++) {
[PSCustomObject]@{
channel = $statusfieldnames[$i]
value = ([string]([double]($ValueArray[$i]) / [int]($divisorfields[$i]) )).replace(',','.')
unit = $unitsfieldnames[$i]
}
}
Return $result
}
function Get-XML {
param(
$Table
)
$prtg = '<?xml version="1.0" encoding="Windows-1252" ?>
<prtg>'
#$prtg = '<prtg>'
foreach ($row in $Table) {
$channel = $row.channel
$value = $row.value
$unit = $row.unit
if ($value.Contains('.')) {$float = 1} else {$float = 0}
if ($channel) {
$prtg +="
<result>
<channel>$channel</channel>
<value>$value</value>
<unit>Custom</unit>
<customUnit>$unit</customUnit>
<float>$float</float>
</result>"
}
}
$prtg +="</prtg>"
return $prtg
}
$IP = "[PUT YOUR IP HERE]"
$data = Get-Data -DeviceIP $IP
$table = Get-Table -DataString $data
[xml]$xml = New-Object System.Xml.XmlDocument
[xml]$xml = Get-XML -Table $table
$xml.InnerXml
Hello,
Thank you very much for sharing your script with the community. We greatly appreciate your initiative which contributes to make PRTG better.
Kind regards.
Oct, 2020 - Permalink