[SOLVED] NUT-Client Items

Hello guys,

I have a problem:
I want to run this command “upsc ups@IP-NutServer” and split the Output to different Items.

This is for example the Output:

Init SSL without certificate database
battery.charge: 100
battery.charge.low: 10
battery.charge.warning: 50
battery.date: not set
battery.mfr.date: 2019/04/25
battery.runtime: 2302
battery.runtime.low: 120
battery.type: PbAc
battery.voltage: 13.6
battery.voltage.nominal: 12.0
device.mfr: APC
device.model: Back-UPS ES 700G
device.serial: XXXXXXXXXX
device.type: ups
driver.name: usbhid-ups
driver.parameter.pollfreq: 30
driver.parameter.pollinterval: 5
driver.parameter.port: auto
driver.version: XXXXXXXXX
driver.version.data: APC HID 0.95
driver.version.internal: 0.38
input.sensitivity: medium
input.transfer.high: 266
input.transfer.low: 180
input.voltage: 238.0
input.voltage.nominal: 230
ups.beeper.status: enabled
ups.delay.shutdown: 20
ups.firmware: 871.O4 .I
ups.firmware.aux: O4
ups.load: 0
ups.mfr: APC
ups.mfr.date: 2019/04/25
ups.model: Back-UPS ES 700G
ups.productid: 0000
ups.serial: XXXXXXX
ups.status: OL
ups.timer.reboot: 0
ups.timer.shutdown: -1
ups.vendorid: 00XX0

Now i want to transfer every single value like “Back-UPS ES 700G” or “medium” to different OpenHab Items

This is my Items-File:

    Number        USV_APC_BattProz                                              "Akku: [%.1f %%]"                                             <battery>                   (influx)
    Number        USV_APC_BattLowProz                                           "Akku Low bei: [%.1f %%]"                                     <battery>
    Number        USV_APC_BattWarnProz                                          "Akku Warn bei: [%.1f %%]"                                    <battery>
    String        USV_APC_BattManFacDate                                        "Akku Herstellungsdat: [%s]"                                  <calendar>
    Number        USV_APC_BattRuntime                                           "Akku Laufzeit: [%.1f s]"                                     <clock>                     (influx)
    Number        USV_APC_BattRuntimeLow                                        "Akku Runt Low: [%.1f s]"                                     <clock>
    String        USV_APC_BattType                                              "Akkutyp: [%s]"                                               <battery>
    Number        USV_APC_BattVolt                                              "Akkuspng: [%.1f V]"                                          <battery>                   (influx)
    Number        USV_APC_BattVoltNorm                                          "Akkuspng Normal: [%.1f V]"                                   <battery>
    String        USV_APC_DevMfr                                                "Hersteller: [%s]"                                            <battery>
    String        USV_APC_DevModel                                              "Model: [%s]"                                                 <battery>
    String        USV_APC_DevSerNr                                              "Seriennummer: [%s]"                                          <battery>
    String        USV_APC_DevTyp                                                "Typ: [%s]"                                                   <battery>
    String        USV_APC_DriverName                                            "Treibername: [%s]"                                           <battery>
    Number        USV_APC_DriverPollFr                                          "Pollfreq: [%s]"                                              <clock>
    Number        USV_APC_DriverPollInt                                         "Pollinter: [%s]"                                             <clock>
    String        USV_APC_DriverPort                                            "Port: [%s]"                                                  <battery>
    String        USV_APC_DriverVers                                            "Treibervers: [%s]"                                           <battery>
    String        USV_APC_DriverData                                            "Treiberdata: [%s]"                                           <battery>
    String        USV_APC_DriverVersInt                                         "Treibervers intern: [%s]"                                    <battery>
    String        USV_APC_InpSens                                               "Eingangssensib: [%s]"                                        <battery>
    Number        USV_APC_InpTransH                                             "Eingangsspng High: [%.1f V]"                                 <battery>
    Number        USV_APC_InpTransL                                             "Eingangsspng Low: [%.1f V]"                                  <battery>
    Number        USV_APC_InpVolt                                               "Eingangsspng: [%.1f V]"                                      <battery>
    Number        USV_APC_InpVoltNorm                                           "Eingangsspng Normal: [%.1f V]"                               <battery>
    String        USV_APC_UpsBeep                                               "Piepser: [%s"                                                <battery>
    Number        USV_APC_UpsShutdDelay                                         "Herunterfahrverzö: [%.1f s]"                                 <battery>
    String        USV_APC_UpsFirmware                                           "Firmware: [%s]"                                              <battery>
    String        USV_APC_UpsFirmwareAux                                        "Firmware Aux: [%s]"                                          <battery>
    Number        USV_APC_UpsLoad                                               "Last: [%.1f W]"                                              <light>
    String        USV_APC_UpsMfr                                                "Ups Hersteller: [%s]"                                        <battery>
    String        USV_APC_UpsManFacDate                                         "Ups Herstellungsdat: [%s]"                                   <battery>
    String        USV_APC_UpsModel                                              "Ups Model: [%s]"                                             <battery>
    String        USV_APC_UpsProductId                                          "Ups Product Id: [%s]"                                        <battery>
    String        USV_APC_UpsSerNr                                              "Ups Seriennummer: [%s]"                                      <battery>
    String        USV_APC_UpsStatus                                             "Ups Status: [%s]"                                            <battery>
    Number        USV_APC_UpsTimerReboot                                        "Ups Timer Reboot: [%.1f s]"                                  <battery>
    Number        USV_APC_UpsTimerShutdown                                      "Ups Timer Shutdown: [%.1f s]"                                <battery>
    String        USV_APC_UpsVendorId                                           "Ups Vendor Id: [%s]"                                         <battery>

And my Rules -File:

rule "Alle 20 Sekunden UPS Status abfragen und auswerten"
  when
    Time cron "0/20 * * ? * * *"
  then
    var String CommandOutput = executeCommandLine("upsc ups@IP-NutServer", 120*1000)
    if (CommandOutput.startsWith("Init SSL")) {
        var String[] results = CommandOutput.split("\\r?\\n")
        var Arg0 = results.get(0).split(" ").get(1)
        var Arg1 = results.get(1).split(" ").get(2)
        var Arg2 = results.get(2).split(" ").get(1)
        var Arg3 = results.get(3).split(" ").get(1)
        var Arg4 = results.get(4).split(" ").get(1)
        var Arg5 = results.get(5).split(" ").get(1)
        var Arg6 = results.get(6).split(" ").get(1)
        var Arg7 = results.get(7).split(" ").get(1)
        var Arg8 = results.get(8).split(" ").get(1)
        var Arg9 = results.get(9).split(" ").get(1)
        var Arg10 = results.get(10).split(" ").get(1)
        var Arg11 = results.get(11).split(" ").get(1)
        var Arg12 = results.get(12).split(" ").get(1)
        var Arg13 = results.get(13).split(" ").get(1)
        var Arg14 = results.get(14).split(" ").get(1)
        var Arg15 = results.get(15).split(" ").get(1)
        var Arg16 = results.get(16).split(" ").get(1)
        var Arg17 = results.get(17).split(" ").get(1)
        var Arg18 = results.get(18).split(" ").get(1)
        var Arg19 = results.get(19).split(" ").get(1)
        var Arg20 = results.get(20).split(" ").get(1)
        var Arg21 = results.get(21).split(" ").get(1)
        var Arg22 = results.get(22).split(" ").get(1)
        var Arg23 = results.get(23).split(" ").get(1)
        var Arg24 = results.get(24).split(" ").get(1)
        var Arg25 = results.get(25).split(" ").get(1)
        var Arg26 = results.get(26).split(" ").get(1)
        var Arg27 = results.get(27).split(" ").get(1)
        var Arg28 = results.get(28).split(" ").get(1)
        var Arg29 = results.get(29).split(" ").get(1)
        var Arg30 = results.get(30).split(" ").get(1)
        var Arg31 = results.get(31).split(" ").get(1)
        var Arg32 = results.get(32).split(" ").get(1)
        var Arg33 = results.get(33).split(" ").get(1)
        var Arg34 = results.get(34).split(" ").get(1)
        var Arg35 = results.get(35).split(" ").get(1)
        var Arg36 = results.get(36).split(" ").get(1)
        var Arg37 = results.get(37).split(" ").get(1)
        var Arg38 = results.get(38).split(" ").get(1)
        var Arg39 = results.get(39).split(" ").get(1)

        USV_APC_BattProz.sendCommand(Arg1)
        USV_APC_BattLowProz.sendCommand(Arg2)
        USV_APC_BattWarnProz.sendCommand(Arg3)
        USV_APC_BattManFacDate.sendCommand(Arg4)
        USV_APC_BattRuntime.sendCommand(Arg5)
        USV_APC_BattRuntimeLow.sendCommand(Arg6)
        USV_APC_BattType.sendCommand(Arg7)
        USV_APC_BattVolt.sendCommand(Arg8)
        USV_APC_BattVoltNorm.sendCommand(Arg9)
        USV_APC_DevMfr.sendCommand(Arg10)
        USV_APC_DevModel.sendCommand(Arg11)
        USV_APC_DevSerNr.sendCommand(Arg12)
        USV_APC_DevTyp.sendCommand(Arg13)
        USV_APC_DriverName.sendCommand(Arg14)
        USV_APC_DriverPollFr.sendCommand(Arg15)
        USV_APC_DriverPollInt.sendCommand(Arg16)
        USV_APC_DriverPort.sendCommand(Arg17)
        USV_APC_DriverVers.sendCommand(Arg18)
        USV_APC_DriverData.sendCommand(Arg19)
        USV_APC_DriverVersInt.sendCommand(Arg20)
        USV_APC_InpSens.sendCommand(Arg21)
        USV_APC_InpTransH.sendCommand(Arg22)
        USV_APC_InpTransL.sendCommand(Arg23)
        USV_APC_InpVolt.sendCommand(Arg24)
        USV_APC_InpVoltNorm.sendCommand(Arg25)
        USV_APC_UpsBeep.sendCommand(Arg26)
        USV_APC_UpsShutdDelay.sendCommand(Arg27)
        USV_APC_UpsFirmware.sendCommand(Arg28)
        USV_APC_UpsFirmwareAux.sendCommand(Arg29)
        USV_APC_UpsLoad.sendCommand(Arg30)
        USV_APC_UpsMfr.sendCommand(Arg31)
        USV_APC_UpsManFacDate.sendCommand(Arg32)
        USV_APC_UpsModel.sendCommand(Arg33)
        USV_APC_UpsProductId.sendCommand(Arg34)
        USV_APC_UpsSerNr.sendCommand(Arg35)
        USV_APC_UpsStatus.sendCommand(Arg36)
        USV_APC_UpsTimerReboot.sendCommand(Arg37)
        USV_APC_UpsTimerShutdown.sendCommand(Arg38)
        USV_APC_UpsVendorId.sendCommand(Arg39)
    }
end

But it’s not working.
Can someone help me with this Problem? Thanks!

Does your rule file load successfully? Look into your openhab.log
Are there any runtime errors? See openhab.log

Does the rule fire? Add a logInfo to find out.
Does your data fetch return anything? Add a logInfo to find out.

2019-08-24 13:28:00.130 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Alle 20 Sekunden UPS Status abfragen und auswerten': 2

This is in the Log

The Rule loaded successfully

I’m almost certain that means the results string is not what you expect, and there is no element 2 for get(2)

I have corrected the error where Arg1 is.

Now i get errors that the format doesn’t match

Are you aware that there is a NUT binding? Russel posted a pretty thorough tutorial to get it up and working with openHAB. Beginner's Guide to Network UPS Tools (NUT) on a Raspberry Pi

One thing I don’t know is whether the NUT output is guaranteed to always be in the same order. Just in case I would change the approach a little and use a Map. Then you can use a nice for loop to process each line as well.

rule "Alle 20 Sekunden UPS Status abfragen und auswerten"
when
    Time cron 0/20 * * ? * * *"
then
    // This Rule executes every 20 seconds. You must not let the Rule take more than 20 seconds to complete
    // The original timeout of two minutes creates a really likely risk that you run out of Quartz threads
    // and all your cron triggered Rules and Astro binding will stop functioning
    val commandOutput = executeCommandLine("upsc ups@IP-NutServer", 15*1000) 

    if(!commandOutput.startsWith("Init SSL")) return; // fail fast

    val values = newHashMap
    commandOutput.split("\\r?\\n").forEach[ line |
        val parts = line.split(": ")
        values.put(parts.get(0), parts.get(1))
    ]

    // since you are just changing OH state and not causing something to happen, use postUpdate
    USV_APC_BattProz.postUpdate(values.get("battery.charge")) 
    ...
end

Please don’t describe the error. Post the actual error.

1 Like

I’ve forgott an item so that they where all shifted.
The binding is not working for me.

Thats now my solution:

Follow this guide to setup the NUT-Client: Click

Then configure this Openhab-Files:

Items-File:

    Number        USV_APC_BattProz                                              "Akku: [%.1f %%]"                                             <battery>                   (influx)
    Number        USV_APC_BattLowProz                                           "Akku Low bei: [%.1f %%]"                                     <battery>
    Number        USV_APC_BattWarnProz                                          "Akku Warn bei: [%.1f %%]"                                    <battery>
    String        USV_APC_BattDate                                              "Akku Datum: [%s]"                                            <calendar>
    String        USV_APC_BattManFacDate                                        "Akku Herstellungsdat: [%s]"                                  <calendar>
    Number        USV_APC_BattRuntime                                           "Akku Laufzeit: [%.1f s]"                                     <clock>                     (influx)
    Number        USV_APC_BattRuntimeLow                                        "Akku Runt Low: [%.1f s]"                                     <clock>
    String        USV_APC_BattType                                              "Akkutyp: [%s]"                                               <battery>
    Number        USV_APC_BattVolt                                              "Akkuspng: [%.1f V]"                                          <battery>                   (influx)
    Number        USV_APC_BattVoltNorm                                          "Akkuspng Normal: [%.1f V]"                                   <battery>
    String        USV_APC_DevMfr                                                "Hersteller: [%s]"                                            <battery>
    String        USV_APC_DevModel                                              "Model: [%s]"                                                 <battery>
    String        USV_APC_DevSerNr                                              "Seriennummer: [%s]"                                          <battery>
    String        USV_APC_DevTyp                                                "Typ: [%s]"                                                   <battery>
    String        USV_APC_DriverName                                            "Treibername: [%s]"                                           <battery>
    Number        USV_APC_DriverPollFr                                          "Pollfreq: [%s]"                                              <clock>
    Number        USV_APC_DriverPollInt                                         "Pollinter: [%s]"                                             <clock>
    String        USV_APC_DriverPort                                            "Port: [%s]"                                                  <battery>
    String        USV_APC_DriverVers                                            "Treibervers: [%s]"                                           <battery>
    String        USV_APC_DriverData                                            "Treiberdata: [%s]"                                           <battery>
    String        USV_APC_DriverVersInt                                         "Treibervers intern: [%s]"                                    <battery>
    String        USV_APC_InpSens                                               "Eingangssensib: [%s]"                                        <battery>
    Number        USV_APC_InpTransH                                             "Eingangsspng High: [%.1f V]"                                 <battery>
    Number        USV_APC_InpTransL                                             "Eingangsspng Low: [%.1f V]"                                  <battery>
    Number        USV_APC_InpVolt                                               "Eingangsspng: [%.1f V]"                                      <battery>
    Number        USV_APC_InpVoltNorm                                           "Eingangsspng Normal: [%.1f V]"                               <battery>
    String        USV_APC_UpsBeep                                               "Piepser: [%s]"                                               <battery>
    Number        USV_APC_UpsShutdDelay                                         "Herunterfahrverzö: [%.1f s]"                                 <battery>
    String        USV_APC_UpsFirmware                                           "Firmware: [%s]"                                              <battery>
    String        USV_APC_UpsFirmwareAux                                        "Firmware Aux: [%s]"                                          <battery>
    Number        USV_APC_UpsLoad                                               "Last: [%.1f W]"                                              <light>
    String        USV_APC_UpsMfr                                                "Ups Hersteller: [%s]"                                        <battery>
    String        USV_APC_UpsManFacDate                                         "Ups Herstellungsdat: [%s]"                                   <battery>
    String        USV_APC_UpsModel                                              "Ups Model: [%s]"                                             <battery>
    String        USV_APC_UpsProductId                                          "Ups Product Id: [%s]"                                        <battery>
    String        USV_APC_UpsSerNr                                              "Ups Seriennummer: [%s]"                                      <battery>
    String        USV_APC_UpsStatus                                             "Ups Status: [MAP(USV_Status.map):%s]"                        <battery>
    Number        USV_APC_UpsTimerReboot                                        "Ups Timer Reboot: [%.1f s]"                                  <battery>
    Number        USV_APC_UpsTimerShutdown                                      "Ups Timer Shutdown: [%.1f s]"                                <battery>
    String        USV_APC_UpsVendorId                                           "Ups Vendor Id: [%s]"                                         <battery>

Rules-File:

rule "Alle 20 Sekunden USV Status abfragen und auswerten"
  when
    Time cron "0/20 * * ? * * *"
  then
    val commandOutput = executeCommandLine("upsc ups@IP-NutServer", 15*1000) 
    if(!commandOutput.startsWith("Init SSL")) return; // fail fast
    val values = newHashMap
    commandOutput.split("\\r?\\n").forEach[ line |
        val parts = line.split(": ")
        if(parts.size > 1) values.put(parts.get(0), parts.get(1))
    ]

    USV_APC_BattProz.sendCommand(values.get("battery.charge"))    
    USV_APC_BattLowProz.sendCommand(values.get("battery.charge.low"))    
    USV_APC_BattWarnProz.sendCommand(values.get("battery.charge.warning"))    
    USV_APC_BattDate.sendCommand(values.get("battery.date"))    
    USV_APC_BattManFacDate.sendCommand(values.get("battery.mfr.date"))    
    USV_APC_BattRuntime.sendCommand(values.get("battery.runtime"))    
    USV_APC_BattRuntimeLow.sendCommand(values.get("battery.runtime.low"))    
    USV_APC_BattType.sendCommand(values.get("battery.type"))    
    USV_APC_BattVolt.sendCommand(values.get("battery.voltage"))    
    USV_APC_BattVoltNorm.sendCommand(values.get("battery.voltage.nominal"))    
    USV_APC_DevMfr.sendCommand(values.get("device.mfr"))    
    USV_APC_DevModel.sendCommand(values.get("device.model"))    
    USV_APC_DevSerNr.sendCommand(values.get("device.serial"))    
    USV_APC_DevTyp.sendCommand(values.get("device.type"))    
    USV_APC_DriverName.sendCommand(values.get("driver.name"))    
    USV_APC_DriverPollFr.sendCommand(values.get("driver.parameter.pollfreq"))    
    USV_APC_DriverPollInt.sendCommand(values.get("driver.parameter.pollinterval"))
    USV_APC_DriverPort.sendCommand(values.get("driver.parameter.port"))   
    USV_APC_DriverVers.sendCommand(values.get("driver.version"))
    USV_APC_DriverData.sendCommand(values.get("driver.version.data"))  
    USV_APC_DriverVersInt.sendCommand(values.get("driver.version.internal"))
    USV_APC_InpSens.sendCommand(values.get("input.sensitivity"))
    USV_APC_InpTransH.sendCommand(values.get("input.transfer.high")) 
    USV_APC_InpTransL.sendCommand(values.get("input.transfer.low"))
    USV_APC_InpVolt.sendCommand(values.get("input.voltage"))
    USV_APC_InpVoltNorm.sendCommand(values.get("input.voltage.nominal"))
    USV_APC_UpsBeep.sendCommand(values.get("ups.beeper.status"))
    USV_APC_UpsShutdDelay.sendCommand(values.get("ups.delay.shutdown"))
    USV_APC_UpsFirmware.sendCommand(values.get("ups.firmware"))
    USV_APC_UpsFirmwareAux.sendCommand(values.get("ups.firmware.aux"))
    USV_APC_UpsLoad.sendCommand(values.get("ups.load"))
    USV_APC_UpsMfr.sendCommand(values.get("ups.mfr"))
    USV_APC_UpsManFacDate.sendCommand(values.get("ups.mfr.date"))
    USV_APC_UpsModel.sendCommand(values.get("ups.model"))
    USV_APC_UpsProductId.sendCommand(values.get("ups.productid"))
    USV_APC_UpsSerNr.sendCommand(values.get("ups.serial"))
    USV_APC_UpsStatus.sendCommand(values.get("ups.status"))
    USV_APC_UpsTimerReboot.sendCommand(values.get("ups.timer.reboot"))
    USV_APC_UpsTimerShutdown.sendCommand(values.get("ups.timer.shutdown"))
    USV_APC_UpsVendorId.sendCommand(values.get("ups.vendorid"))
end

rule "Wenn USV in Batteriemodus wechselt"
  when
    Item USV_APC_UpsStatus changed to 'OB DISCHRG'
  then
        logInfo("Nachricht", "00 USV in Batteriebetrieb")
end
rule "Wenn USV in Batteriemodus wechselt"
  when
    Item USV_APC_UpsStatus changed to 'OL'
  then
        logInfo("Nachricht", "00 USV in Netzbetrieb")
end
rule "Wenn USV in Batteriemodus wechselt"
  when
    Item USV_APC_UpsStatus changed to 'OL CHRG'
  then
        logInfo("Nachricht", "00 USV Laedt")
end

Here i coded “.sendCommand” because of the Actions

Sitemap:

    Frame label="USV"{
	  Text label="USV" icon="battery" {
        Text item=USV_APC_BattProz
        Text item=USV_APC_BattLowProz    
        Text item=USV_APC_BattWarnProz   
        Text item=USV_APC_BattDate 
        Text item=USV_APC_BattManFacDate    
        Text item=USV_APC_BattRuntime 
        Text item=USV_APC_BattRuntimeLow    
        Text item=USV_APC_BattType    
        Text item=USV_APC_BattVolt    
        Text item=USV_APC_BattVoltNorm    
        Text item=USV_APC_DevMfr    
        Text item=USV_APC_DevModel    
        Text item=USV_APC_DevSerNr    
        Text item=USV_APC_DevTyp    
        Text item=USV_APC_DriverName    
        Text item=USV_APC_DriverPollFr    
        Text item=USV_APC_DriverPollInt    
        Text item=USV_APC_DriverPort    
        Text item=USV_APC_DriverVers    
        Text item=USV_APC_DriverData    
        Text item=USV_APC_DriverVersInt    
        Text item=USV_APC_InpSens    
        Text item=USV_APC_InpTransH    
        Text item=USV_APC_InpTransL    
        Text item=USV_APC_InpVolt    
        Text item=USV_APC_InpVoltNorm    
        Text item=USV_APC_UpsBeep   
        Text item=USV_APC_UpsShutdDelay    
        Text item=USV_APC_UpsFirmware    
        Text item=USV_APC_UpsFirmwareAux   
        Text item=USV_APC_UpsLoad   
        Text item=USV_APC_UpsMfr
        Text item=USV_APC_UpsManFacDate    
        Text item=USV_APC_UpsModel
        Text item=USV_APC_UpsProductId    
        Text item=USV_APC_UpsSerNr
        Text item=USV_APC_UpsStatus    
        Text item=USV_APC_UpsTimerReboot    
        Text item=USV_APC_UpsTimerShutdown    
        Text item=USV_APC_UpsVendorId  
	  }
	}

Map File “USV_Status.map”:

OB \DISCHRG=Batteriemodus
OL \CHRG=Lädt auf
OL=Netzbetrieb

Did you file an issue? It can’t be fixed if we don’t know it’s broken.

1 Like

It says “Nut processing error” “org.networkupstools.jnut.NutException: UNKNOWN-UPS”

But i configured it with the correct password

Yout solution above is smart but it gives me the error: “Error during the execution of rule ‘Alle 20 Sekunden UPS Status abfragen und auswerten’: 1”

Oh, I know what’s wrong. The first line doesn’t have the ": " because it’s the Init SSL line.

Change

if(parts.size > 1) values.put(parts.get(0), parts.get(1))

For the error, that has nothing to do with the password. That looks like you had the wrong path to the nut server. But if you are sure you configured everything correctly, file an issue so the developers can fix the problem if there is one.

Your solution is working perfectly thanks!

Can it be that if I run the Nut client in the background a second instance of the nut client (from the binding) is not possible?

I don’t know but that will but my understanding is the binding does exactly what your rule does. It just calls upsc, or more likely implements the same protocol as upsc to get the information. I’ve been using the binding for quite some time and can confirm that at least in my environment it works just fine. So it can work. There open question is why it doesn’t work for you.

I don’t know. But it is not so bad. The version above works great for me