Rest API JSON POST script needed

Moderator: leecollings

Post Reply
User avatar
mgerhard74
Posts: 40
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

Rest API JSON POST script needed

Post by mgerhard74 »

I want to control a Hoymiles microinverter (HM-800) with a Lua script, because it runs more often than a dzVents script. A special hardware Ahoy DTU in between domoticz and the microinverter.

I want to send a Json string to a specific url.
As in https://github.com/lumapu/ahoy/blob/main/User_Manual.md described, i want to send

Code: Select all

{
    "id": 0,
    "cmd": "limit_persistent_absolute",
    "val": 50
}
to my inverter http://ip-adress/api

Background: LiPoFe4 batteries supply the microinverter at night, i want to regulate the inverter power to the actual power use of our house.
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
User avatar
waltervl
Posts: 5154
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Rest API JSON POST script needed

Post by waltervl »

Don't have the answer for Lua but why do you think a Lua script runs more often then a dzvents script? A dzvents script can run every minute and even more if you do some tricks in the scripts.
Edit: see for example viewtopic.php?p=294493
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
mgerhard74
Posts: 40
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

Re: Rest API JSON POST script needed

Post by mgerhard74 »

OK, also dzvents scipts are welcome.
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
User avatar
waltervl
Posts: 5154
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Rest API JSON POST script needed

Post by waltervl »

Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
niekniek89
Posts: 9
Joined: Saturday 04 November 2017 13:48
Target OS: Linux
Domoticz version: 2021.1
Contact:

Re: Rest API JSON POST script needed

Post by niekniek89 »

Hello @mgerhard74

did you manage to read the ahoydtu with domoticz yet?
I also have an ahoydtu since 2 weeks, but would like to have the logging in domoticz.
User avatar
mgerhard74
Posts: 40
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

Re: Rest API JSON POST script needed

Post by mgerhard74 »

Hi,
i tryed a LUA, but i have a error in xml parsing.
Create a new sensor:
http://[DOMOTICZ-IP]:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Hoymiles&sensortype=248
Put in the DeviceIDx (here: 141) in the Lua code:

Code: Select all

JSON = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
commandArray = {}
    jsondata    = assert(io.popen('curl "http://[AHOY-DTU-IP]/api/live"'))
    device = jsondata:read('*all')
    jsondata:close()
    data = JSON:decode(device) 
    
    if (data ~= nil) then
        commandArray[1] = {['UpdateDevice'] = 141 .. "|0|" ..tonumber(data['inverter'][0]['ch'][0][2])}
    else
        commandArray[1] = {['UpdateDevice'] = 141 .. "|0|" .. 0 }
    end
return commandArray
The script has still an error in data['inverter'][0]['ch'][0][2] ... but i cant fix it....

Gerhard
niekniek89 wrote: Tuesday 21 February 2023 16:41 Hello @mgerhard74

did you manage to read the ahoydtu with domoticz yet?
I also have an ahoydtu since 2 weeks, but would like to have the logging in domoticz.
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
niekniek89
Posts: 9
Joined: Saturday 04 November 2017 13:48
Target OS: Linux
Domoticz version: 2021.1
Contact:

Re: Rest API JSON POST script needed

Post by niekniek89 »

Hello,

sorry it hasn't worked yet.
I'm also still working on getting the data into domoticz.
I still can't do this either.
User avatar
mgerhard74
Posts: 40
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

Re: Rest API JSON POST script needed

Post by mgerhard74 »

niekniek89 wrote: Tuesday 21 February 2023 19:40 Hello,

sorry it hasn't worked yet.
I'm also still working on getting the data into domoticz.
I still can't do this either.
Please look at viewtopic.php?t=39976
lg
Gerhard
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
niekniek89
Posts: 9
Joined: Saturday 04 November 2017 13:48
Target OS: Linux
Domoticz version: 2021.1
Contact:

Re: Rest API JSON POST script needed

Post by niekniek89 »

Hello,

look, that looks good!
Do I need to apply my own inverter name somewhere in your script?
and what did you have to create in domoticz itself? (add hardware)?
is it also possible to get the yield day per panel in domoticz?
User avatar
mgerhard74
Posts: 40
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

Re: Rest API JSON POST script needed

Post by mgerhard74 »

No, the first inverter added to Ahoy dtu has number "0" - in this case do no edits in the script.
No extra hardware needed. With that url in your brower you add a device "Hoymiles":
http://[ip of domoticz]:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=HOYMILES&sensortype=248
Yield of day is also possible - study the Ahoy DTU api and modify the script.

Gerhard
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
niekniek89
Posts: 9
Joined: Saturday 04 November 2017 13:48
Target OS: Linux
Domoticz version: 2021.1
Contact:

Re: Rest API JSON POST script needed

Post by niekniek89 »

Hello,

thanks for your help.
I created a sensor using the URL.
however, it stays gray in Domoticz, and doesn't do much.

do I have to put the script somewhere, and create hardware?
I don't think the sensor alone is enough.
User avatar
mgerhard74
Posts: 40
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

Re: Rest API JSON POST script needed

Post by mgerhard74 »

You must change a few things in the script
viewtopic.php?t=39976
to "fit" your environment:
Change 10.0.0.156 to the IP of your Ahoy dtu
Change in the line with "commandArray[1]=..." the "141" to the id of your HOYMILES sensor.
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
niekniek89
Posts: 9
Joined: Saturday 04 November 2017 13:48
Target OS: Linux
Domoticz version: 2021.1
Contact:

Re: Rest API JSON POST script needed

Post by niekniek89 »

thanks for your help.
do I have to create hardware myself?
an HTTP/HTTPS poller?

when I adjust the script, to the id of the sensor, it still remains gray.
User avatar
mgerhard74
Posts: 40
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

Re: Rest API JSON POST script needed

Post by mgerhard74 »

Hello,
i dont understand your question. The only device you must create is with one http call - i think you have the device created.
Your Ahoy dtu is running? In the same IP area of domoticz? In Ahoy dtu you add at least one Hoymiles inverter, the first has number 0.
Do you have errors in domoticz log concerning this hoymiles script?
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
niekniek89
Posts: 9
Joined: Saturday 04 November 2017 13:48
Target OS: Linux
Domoticz version: 2021.1
Contact:

Re: Rest API JSON POST script needed

Post by niekniek89 »

i have done the following steps:

Step 1: http://[ip of domoticz]:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=HOYMILES&sensortype=248
step 2: place the script within domoticz, within the correct folder and change the id's.

after this I also see the hoymiles sensor within domoticz, but it remains gray.

do I have to arrange something for the script within domoticz?
or do I just have to put it in a folder, and the script will be started automatically?
User avatar
waltervl
Posts: 5154
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Rest API JSON POST script needed

Post by waltervl »

Be sure the IDX of the dummy hardware (IDX 4) is correct. See the list in menu setup - hardware.
Better is to create dummy hardware manually from the user interface. https://www.domoticz.com/wiki/Dummy_for ... l_Switches
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
niekniek89
Posts: 9
Joined: Saturday 04 November 2017 13:48
Target OS: Linux
Domoticz version: 2021.1
Contact:

Re: Rest API JSON POST script needed

Post by niekniek89 »

Hello,

the dummy sensor has the same IDX as in the script.

1. I create a dummy in hardware.
2 inside the dummy I create a sensor.
3. I adapt the script to the IDX of the created sensor.

4. how does the dummy/sensor know which script to look at?

I just put the script in a folder: /home/docker/domoticz/scripts/lua_parsers

I don't have a reference to the script anywhere, is that correct?
the sensor is now no longer gray but does nothing.
User avatar
mgerhard74
Posts: 40
Joined: Friday 28 July 2017 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Austria, Upperaustria
Contact:

Re: Rest API JSON POST script needed

Post by mgerhard74 »

Add you script in Domoticz in Setup / more options / events. (+) Lua
Paste your script here. Save.
View Logs for Errors. The script update the sensor with the id of the script.
Domoticz improves my photovoltaik ownconsumption (Rpi3, wifi plugs) - PV 6,5kWp (Fronius Symo inverter) - 10kWh PV batterie - Nissan Leaf2 (40kWh) and Kia eNiro (64kWh)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests