Reading item as json answer from exec rule

  • Platform information:
    • Hardware: RPi4 8GB
    • OS: rapbian bullseye
    • Java Runtime Environment: java 8
    • openHAB version: 2.5.4

Hi. I need to exec some python script in a rule like:

rule "SE25K merania"
when
    Time cron "0/15 * * ? * * *"
then
    var String jsonMsg = executeCommandLine("python3 /etc/openhab2/scripts/SolaredgeModbus/example.py --unit 3 --json 212.89.229.22 1502")
    postUpdate(rawMessage, jsonMsg)
end

items rawMessage is a string item.

I need to transfer answer from script to this item and than reparse it to lot of subitems.

Error in eclipse:
Type missmatch. Cannot convert from void to string.

executeCommandLine will only return a value if you give it a duration to wait for that value, otherwise it just fires the command and moves onto the next line of the script. So the error you’re getting is that there is no data in jsonMsg to convert to a string.

You’re still on 2.5, so you need to look up the old version of executeCommandLine:

1 Like

Thanks, for OH2
val results = executeCommandLine(cmd, timeout)
timeout in ms.

How to parse string item rawMessage into partially items if contens of json is like:

{
    "c_id": "SunS",
    "c_did": 1,
    "c_length": 65,
    "c_manufacturer": "SolarEdge",
    "c_model": "SE25K-RW000BNN4",
     ....
    "meters": {
        "Meter1": {
            "c_manufacturer": "WattNode",
            "c_model": "WND-3Y-400-MB",
              ...
            "energy_reactive_scale": -32768
        }
    },
    "batteries": {}
}

ANSWER:
val String manufacturer = transform("JSONPATH", "$.c_manufacturer", (rawMessage.state).toString)
val String import_energy_active = transform("JSONPATH", "$.meters.Meter1.m_import_energy_active", (rawMessage.state).toString)