sendHttpPostRequest in OH3: Could not invoke method on instance: null

Cannot send POST request containing item state, variable or received command from OpenHab3 to a remote OpenHab2 instance

items:

Number Cctv_monitorid "CCTV Monitor Id"

Rules:

rule "test rule"
     when
         Item Cctv_monitorid received command
     then
         logInfo("Rules", "Received command: [{}]",receivedCommand)
         sendHttpPostRequest ("https://user:password@ddns.url/rest/items/Cctv_monitorid", "text/plain", receivedCommand, 10000 )
     end 

log entry:

[INFO ] [org.openhab.core.model.script.Rules ] - Received command: [1]
[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'cctvevents-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.HTTP.sendHttpPostRequest(java.lang.String,java.lang.String,java.lang.String,int) on instance: null in cctvevents
  • Platform information:
    • Hardware: Rpi4 4Gb
    • OS: Raspbian GNU/Linux 11 (bullseye)
    • Java Runtime Environment: openjdk version “11.0.15” 2022-04-19
    • openHAB version: openHAB 3.2.0 Release Build

i’ve already tried this approach:

rule "test rule"
     when
         Item Cctv_monitorid received command
     then
         val String url = "https://user:password@ddns.url/rest/items/Cctv_monitorid"
         val String header = "text/plain"
         var String data = Cctv_monitorid.state
         logInfo("Rules", "url: [{}]",url)
         logInfo("Rules", "header: [{}]",header)
         logInfo("Rules", "data: [{}]",data)
         sendHttpPostRequest(url,header,data,20000)
     end 

with the same result:

[INFO ] [org.openhab.core.model.script.Rules ] - url: [https://user:password@ddns.url/rest/items/Cctv_monitorid]
[INFO ] [org.openhab.core.model.script.Rules ] - header: [text/plain]
[INFO ] [org.openhab.core.model.script.Rules ] - data: [2]
[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'cctvevents-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.HTTP.sendHttpPostRequest(java.lang.String,java.lang.String,java.lang.String,int) on instance: null in cctvevents

i’m completely clueless.
also tried using curl executeCommandLine but i’m apparently completely lost with OH3

executeCommandLine("curl", "-X", "POST", "--header", "Content-Type: text/plain", "--header", "Accept: application/json", "-d", Cctv_monitorid.state, "https://user:password@ddns.url/rest/items/Cctv_monitorid")

i could not find anything useful in documentaton, can anyone give me some hints?

In OH a command and a state are both very specific classes of information. What’s most important in this case, is that neither of them are strings and the sendHttpPostRequest is expecting a string: that’s what the error is telling you.

The variable receivedCommand is a command, so you can’t put it in a function call where the function expects something else. Sometimes you can get away with just using receivedCommand in other places because there are some automatic conversions that rulesDSL will do, but in this case you have to be explicit about it. So the solution is to use receivedCommand.toString() which will force the Command object into a string and make the function happy.

(The exact same reasoning applies to Cctv_monitorid.state: you have to make that state object a string with toString() if you want to use it, but in this case receivedCommand is the better choice.)

1 Like

the function is now happy, and the user too.
during the years i set up 4 different remote Openhab 2.5 installations and recently added a new Openhab 3 system.
got to say that some changes have been a very painful experience for my limited knowledge.

thanks a lot for your kind explanation. i’m sure this will be very useful for other beginners without java background.