Help with Scene

Help with scene creation! I am trying to integrate my Hozelock cloud controller to my Vera Plus I am trying to create a scene for turning on and off the unit from my Vera I have found the api for Hozelock but I cannot integrate it into a scene can someone help me with this code. https://github.com/martynjsimpson/HozelockAPI

POST http://hoz3.com/restful/support/hubs/{hubId}/controllers/actions/waterNow

Request Body
{
“controllerIDs”:[{controllerId}],
“duration”:300000
}

Response
{
“errorCode”: 0
}

You will need to use Lua code to send a POST request with a json body,

similar to this [solved]Problem with http post request

Hi thank you for your reply appreciate your help but I don’t know how to incorporate the code to the Lua! I was hoping that someone can set it up for me so I can just put my credential and copy and paste into a scene I will much appreciate if someone can help me!

something like below i have marked where deviceid and duration are set

local https = require "ssl.https"
local http = require "socket.http"
local ltn12 = require "ltn12"
local json = require "dkjson"
 -- edit deviceid here this can be an array of ids {"deviceid1","deviceid2"}
local payload = {"the_deviceid"}
-- edit for duration
local DurationLength = "300000" 
local request_body = {controllerIDs = payload, duration = DurationLength}

local response_body = {}
request_body = json.encode(request_body)

local r, c, h, s = https.request {
  url = 'http://hoz3.com/restful/support/hubs/{hubId}/controllers/actions/waterNow',
  method = 'POST',
  headers = {
    ["Content-Type"] = "application/json",
    ["Content-Length"] = string.len(request_body)
  },
  source = ltn12.source.string(request_body),
  sink = ltn12.sink.table(response_body)
}

also in user account info - security, check allow unsafe code

Hi thank you very much for your effort I have copied and paste the code but is not working!

add at bottom luup.log("JSON HTTP - " .. c .. " - " .. tostring(r) .. "\n")
Then run code in apps- develop - run test Lua.
Then check what is in the vera log.
To see log enter this
http://vera_ip/cgi-bin/cmh/log.sh?Device=LuaUPnP
into browser, add your vera’s IP in link.

edit also make sure the url is correct as i just copied your example.

Hi what exactly I should look for in the log file?

JSON HTTP - and any thing after it.

c is the response code
and
r is the response

Failed to test code, please try again

the code has to be at the end of the other code.

also this version may be better as it will turn the json response into a string for easy printing in log
luup.log("JSON HTTP - " .. c .. " - " .. tostring(r) .. "\n")

Hi thank you very much for your effort to help me still the same! Failed to test code, please try again

I don’t know what to say as it works for me
from my log
50 04/28/20 22:06:11.891 luup_log:0: JSON HTTP - 400 - 1

I will try to reset the unit to its factory default and I will give it a try again! Looks like my Vera is not working well!

no don’t reset

have you check allow unsafe Lua in User account info - security?

Hi I have managed to get some logs I don’t know if that helps you or not!

01 04/28/20 11:06:15.852 LuaInterface::LoadCode: [string “local https = require “ssl.https”…”]:6: malformed number near ‘8TE44F’ <0x74118520>
01 04/28/20 11:06:15.853 JobHandler_LuaUPnP::RunLua failed: local https = require “ssl.https”
local http = require “socket.http”
local ltn12 = require “ltn12”
local json = require “dkjson”
– edit deviceid here this can be an array of ids {854RT}
local payload = {{854RT}
– edit for duration
local DurationLength = “300000”
local request_body = {controllerIDs = payload, duration = DurationLength}

local response_body = {}
request_body = json.encode(request_body)

local r, c, h, s = https.request {
url = ‘http://hoz3.com/restful/support/hubs/{854RT/controllers/actions/waterNow’,
method = ‘POST’,
headers = {
[“Content-Type”] = “application/json”,
[“Content-Length”] = string.len(request_body)
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body)
}luup.log("JSON HTTP - " … c … " - " … tostring® … “\n”) <0x74118520>
02 04/28/20 11:06:15.853 JobHandler_LuaUPnP::RunAction device -1 action urn:micasaverde-com:serviceId:HomeAutomationGateway1/RunLua failed with 401/Code failed

This is not right “tostring®” should be "tostring(r)"

But the issue is your getting a 401 code unauthorized. The url is incorrect or you are not authorised to access the api.

the url looks wrong remove the {

also when you add code to the forum please use ``` before and after the code

Here’s another one!

local http = require “socket.http”
local ltn12 = require “ltn12”
local json = require “dkjson”
– edit deviceid here this can be an array of ids {“854RT”}
local payload = {“854RT”}
– edit for duration
local DurationLength = “300000”
local request_body = {controllerIDs = payload, duration = DurationLength}

local response_body = {}
request_body = json.encode(request_body)

local r, c, h, s = https.request {
url = ‘http://hoz3.com/restful/support/hubs/854RT/controllers/actions/waterNow’,
method = ‘POST’,
headers = {
[“Content-Type”] = “application/json”,
[“Content-Length”] = string.len(request_body)
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body)
}luup.log("JSON HTTP - " … c … " - " … tostring® … “\n”) <0x74574520>
50 04/28/20 11:11:31.771 luup_log:0: JSON HTTP - 400 - 1

The api access authorisation is your hub number!

please use ``` before and after code you enter on forum so it reanders correctly

400 is bad request

your device nuber is the same as the hub number. is that correct? do you have a hub?

Yes I do have the hub and the number is printed on the back!
I think from this example it should look something like this!
POST http://hoz3.com/restful/support/hubs/{hubId}/controllers/actions/waterNow

Request Body
{
“controllerIDs”:[{controllerId}],
“duration”:{duration}
}

Response
{
“errorCode”: 0
}

Best Home Automation shopping experience. Shop at getvera!

© 2021 Ezlo Innovation, All Rights Reserved. Terms of Use | Privacy Policy | Forum Rules