[SOLVED] Create items from php json script


(Jozef Szabo) #1

Hi,
how can I create item solarUAC from this php script please ?

<?php
$apiURL = "http://.solarIP./solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollectio$

// Get the raw JSON
$jsonData = file_get_contents($apiURL);

// Decode into an object
$solar = json_decode($jsonData, true);

// Parse variables
$solarUAC = $solar["Body"]["Data"]["UAC"]["Value"];
?>

(Vincent Regaud) #2

You can get the json directily in openHAB and then extract the JSON value of solarUAC

rule "Extract UAC"
when
    Time cron "0 0/5 * ? * * *" //Every 5 minutes
then

    //url is incomplete in your link and 5 seconds time out
    var jsonString = sendHttpGetRequest("http://.solarIP./solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollectio", 5000)
    solarUAC.postUpdate(transform("JSONPATH", "$.Body.Data.UAC.Value", jsonString))
end

You could also use the HTTP binding to poll the jsonString on a regular basis and cache the item so you can extract several values from it. But this should get you started


(Jozef Szabo) #3

All is OK for me.

How could I send a Number Value (setpoint) in oposite way, from OH item to json solar API ?

Please show me how to use HTTP binding to pool jsonString for more values.
Best regards,
Jozef


(Vincent Regaud) #4

See: https://www.openhab.org/docs/configuration/actions.html#http-actions

See: https://www.openhab.org/addons/bindings/http1/

Have a go, if it doesn’t work, post what you tried and we’ll try to fix it together


(Jozef Szabo) #5

Big thanks for all.


(Jozef Szabo) #6

Both method from http binding and json are OK.
Example for other user:

http binding example
items

Number PAChttp  "PAChttp [%.1f W]" <solarplant> (Fronius) { http="<[froniusCache:1000:JSONPATH($.Body.Data.PAC.Value)]" }

services

froniusCache.url=http://solarIP/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData
froniusCache.updateInterval=5000

====================================================================

jsonpath example
items

Number PACjson          "PACjs [%.1f W]" <solarplant> (Fronius)

rules

rule "Extract FV Vars json"
when
    Time cron "0 0/5 * ? * * *" //Every 5 minutes
then
    //url complete and 5 seconds time out
    var jsonString = sendHttpGetRequest("http://solarIP/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverte$
    PACjson.postUpdate(transform("JSONPATH", "$.Body.Data.PAC.Value", jsonString))
end

(Vincent Regaud) #7

Please use the code fences to edit you last post. Thanks