[SOLVED] Executing python script and pushing result to different items

Good morning,

I am a complete newbee in regards to python integration into OH, but at the moment I am trying out the rest-api for HomematicIP cloud. So far all is fine and I am able to read my different devices and ask for the state in a python script, but now I am struggeling with the approach to feed those data into OH2.

When calling my python script (see below extract of a function)

def write_heatingthermostat(room,device):
print(room, " ", device.label, " ", device.lastStatusUpdate, " ", device.valveActualTemperature, " ", device.valvePosition, " ", device.setPointTemperature, " ", device.temperatureOffset, " ", device.valveState, " ", device.id)

I have different variables available available (at the moment they are printed in that example).

How can I achieve to push the different variables e.g.

device.label
device.valveActualTemperature
device.setPointTemperature

into items of OH2 without calling three different py scripts, one per item and variable?

Thanks for your help.
Scuby

Use HABApp to set the states accordingly. You can transform your scripts in to rules and call them accordingly.

Hi,
openHAB has a REST API which you can use to read / write item states. You should find a lot examples here.

Another way would be to query your HomematicIP Cloud API via openHAB rules directly.

Get your script to return a JSON string call it once and store the JSON string i a variable
Then use a transformation action to assign the values to diffrent items:

Rule extract:

val JSONString = executeCommandLine("/usr/bin/python /etc/openhab2/scripts/myscript.py", 10000)
DeviceStatus.postUpdate(transform("JSONPATH", "$.lastStatus", JSONString)
DeviceActualTemperature.postUpdate(transform("JSONPATH", "$.valveActualTemperature", JSONString)
....

Hi,

this I have done now and I am getting the following in my item:

{"label": "Fenster B\u00fcro oben", "unreach": false, "rssiDeviceValue": -95, "rssiPeerValue": null, "configPending": false, "dutyCycle": false, "windowState": "CLOSED"}

My rule is now doing the following:

rule "Fenster Buero oben"
  when
    Item bueroobenfenster_json changed
 then
    val JSONString = transform("JSONPATH", "$.windowState", bueroobenfenster_json.state.toString)
    bOFwindowState.postUpdate(JSONString)
 end

But I do not see any changes on the item bOFwindowState

The log is telling me the following all the time:
2019-09-02 17:48:59:0366 + 166 2019-09-02 17:48:48.484 [vent.ItemStateChangedEvent] - bueroobenfenster_json changed from {“label”: “Fenster B\u00fcro oben”, “unreach”: false, “rssiDeviceValue”: -95, “rssiPeerValue”: null, “configPending”: false, “dutyCycle”: false, “windowState”: “CLOSED”} to {“label”: “Fenster B\u00fcro oben”, “unreach”: false, “rssiDeviceValue”: -98, “rssiPeerValue”: null, “configPending”: false, “dutyCycle”: false, “windowState”: “OPEN”}

For me it seems that the transformation is not done correctly:

Definitiions:
Things:

exec:command:devicebueroobenfenster "Büro oben Fenster" [command="python3 /etc/openhab2/automation/jsr223/01_homematic/bueroobenfenster.py", interval=1, timeout=5, autorun=true, transform="REGEX((.*?))"]

Items:

String bOFwindowState

Rule:

rule "Fenster Buero oben"
  when
    Item bueroobenfenster_json changed
 then
    val JSONString = transform("JSONPATH", "$.windowState", bueroobenfenster_json.state.toString)
    logDebug("homematicip.rules", "Debug", JSONString )

    bOFwindowState.postUpdate(JSONString)
 end

Sitemap:

sitemap homematic label="HomematicIP" {
    Text item=bOFwindowState
}

I hope you can help me here

Is the transformation installed?

Hi,

ohhhhhh deer… Now its getting it as following :wink:

2019-09-02 18:18:30.484 [vent.ItemStateChangedEvent] - bueroobenfenster_json changed from {"label": "Fenster B\u00fcro oben", "unreach": false, "rssiDeviceValue": -99, "rssiPeerValue": null, "configPending": false, "dutyCycle": false, "windowState": "OPEN"} to {"label": "Fenster B\u00fcro oben", "unreach": false, "rssiDeviceValue": -93, "rssiPeerValue": null, "configPending": false, "dutyCycle": false, "windowState": "CLOSED"}

2019-09-02 18:18:30.489 [vent.ItemStateChangedEvent] - bOFwindowState changed from OPEN to CLOSED

But the strange thing now is, that I am not able to see the CLOSED on the sitemap.

The item has no label

Hi,

the label makes no difference at all. There must be something else.

no no, what Vincent is telling you is your item needs a label or it won’t function in sitemap correctly

try

1 Like

hi,

thanks a lot for this… It’s still confusing me what all needs to be defined! Thanks a lot!
Now it’s working as expected

No problem
Don’t forget the little do-dad in the square brackets, it is call a formatter
read more here:
Formatter Strings

https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

here is one for date time formatting
https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html