Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

api.put - What am I doing wrong?


JcBorgs

Question

Simply trying via api.get and api.put to read and write the “Energy panel Tariff rate value”. api.get works just fine and I can read the “rate” value without any issues. But when I try to write a new value with api.put I simply can’t get it to work.

 

Anyone with any good knowledge around the api.put command in Fibaro that can assist and solve this problem?

 

In the Energy Panel Settings the Tariff value is put to 0.64 before the code is executed

 

Current code in testning:

--Define data to put

local TariffData = {
  name = "Main",
  rate = 0.73,
  returnRate = 100,
  additionalTariffs = {}
}
--print ”rate” value to ensure it is formated correctly
print("Tariffdata:", TariffData.rate)
 
--Convert Data to JSON format
local TariffDataJSON = json.encode(TariffData)
--Print JSON format of data
print("JSON Format:", TariffDataJSON)
 
--api put data to /energy/billing/tariff
--trying a couple of different format
--I know I don’t need all four rows here, just trying different format to see what could work.
api.put("/energy/billing/tariff",TariffDataJSON)
api.put("/energy/billing/tariff",{TariffDataJSON})
api.put("/energy/billing/tariff",TariffData)
api.put("/energy/billing/tariff",{TariffData})
 
--api get data from /energy/billing/tariff
local TariffGETdata = api.get("/energy/billing/tariff")
--print data gotten with api.get
print("Tariff rate is:", TariffGETdata.rate)
print("---------------------------------")
 
Gives the following output in the Logg:
[17.04.2022] [17:50:19] [DEBUG] [SCENE548]: Tariffdata: 0.73
[17.04.2022] [17:50:19] [DEBUG] [SCENE548]: JSON Format: {"returnRate":100,"name":"Main","additionalTariffs":[],"rate":0.73}
[17.04.2022] [17:50:19] [DEBUG] [SCENE548]: Tariff rate is: 0.64
[17.04.2022] [17:50:19] [DEBUG] [SCENE548]: ---------------------------------
 
So the api.get part is working as it should and collects the correct data that was stored in the Energy Panel setting before the scene was executed.
 
But what is wrong with the api.put part of the code? I know there is only need for 1 api.put command but have four today to experiment with different formats. But feels like there is something missing from the code as the data provided isn’t written as it should be. I don’t get any error messages in the log.
 
Anyone have any good tips what I am missing????
 
Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0
3 hours ago, JcBorgs said:

 

Simply trying via api.get and api.put to read and write the “Energy panel Tariff rate value”. api.get works just fine and I can read the “rate” value without any issues. But when I try to write a new value with api.put I simply can’t get it to work.

 

Anyone with any good knowledge around the api.put command in Fibaro that can assist and solve this problem?

 

In the Energy Panel Settings the Tariff value is put to 0.64 before the code is executed

 

Current code in testning:

--Define data to put

local TariffData = {
  name = "Main",
  rate = 0.73,
  returnRate = 100,
  additionalTariffs = {}
}
--print ”rate” value to ensure it is formated correctly
print("Tariffdata:", TariffData.rate)
 
--Convert Data to JSON format
local TariffDataJSON = json.encode(TariffData)
--Print JSON format of data
print("JSON Format:", TariffDataJSON)
 
--api put data to /energy/billing/tariff
--trying a couple of different format
--I know I don’t need all four rows here, just trying different format to see what could work.
api.put("/energy/billing/tariff",TariffDataJSON)
api.put("/energy/billing/tariff",{TariffDataJSON})
api.put("/energy/billing/tariff",TariffData)
api.put("/energy/billing/tariff",{TariffData})
 
 

api.put needs actual array of data (it is converted to jsonString inside)

Please login or register to see this spoiler.

Please try to use that TariffData as an array of data and not data converted to json string.

By the way, it could be that Fibaro doesn't allow to change the data by api.put(). 

Edited by cag014
Link to comment
Share on other sites

  • 0
  • Inquirer
  • 18 minutes ago, cag014 said:

    api.put needs actual array of data (it is converted to jsonString inside)

    Please login or register to see this spoiler.

    @cag014There are both ‘put’ and ‘get’ in the swagger so should be allowed?

     

    So what you are saying is to skip the json converting I have done and use the TariffData in “raw” format as an array?

    Edited by JcBorgs
    Link to comment
    Share on other sites

    • 0

    Yes,.... the data converted to json string inside api.put function

     here is the code of api.put  in HC2  (same goes for HC3/Lite/Yubii)

     

    Please login or register to see this code.

     

    By the way same for api.post

    Edited by cag014
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Ok, changed the code to this:

     
    --Define data to put
    local requestData = {
      name = "Main",
      rate = 0.99,
      returnRate = 100,
      additionalTariffs = {}
    }
     
    --print "rate" value to ensure it is formated correctly
    print("requestData:", requestData.rate)
     
    --api put data to /energy/billing/tariff
    local responsData, status = api.put("/energy/billing/tariff", requestData)
     
    print("ResponsData:", responsData.type)
    print("ResponsData:", responsData.reason)
    print("Status:", status)
     
    --api get data from /energy/billing/tariff
    local TariffGETdata = api.get("/energy/billing/tariff")
    --print data gotten with api.get
    print("Tariff rate is:", TariffGETdata.rate)
    print("---------------------------------")
     
    Which gives the following output in log:
    [18.04.2022] [01:07:52] [DEBUG] [SCENE548]: requestData: 0.99
    [18.04.2022] [01:07:52] [DEBUG] [SCENE548]: ResponsData: ERROR
    [18.04.2022] [01:07:52] [DEBUG] [SCENE548]: ResponsData: http://{$gateway-ip}/json-schema/energy/PUT_billing_tariff.json. Invalid schema: #. Invalid keyword: type. Invalid document: #/additionalTariffs
    [18.04.2022] [01:07:52] [DEBUG] [SCENE548]: Status: 400
    [18.04.2022] [01:07:52] [DEBUG] [SCENE548]: Tariff rate is: 0.64
    [18.04.2022] [01:07:52] [DEBUG] [SCENE548]: ---------------------------------
     
    So still doesnt work… 
     
    Edited by JcBorgs
    Link to comment
    Share on other sites

    • 0

    Your  code has a  problem with   additionalTariffs = {} definition.

    (you should change the line print("ResponsData:", responsData) to print("ResponsData:", json.encode(responsData) to see the response

    here the response for your code 

    ResponsData: {"message":"","type":"ERROR","reason":"http://{$gateway-ip}/json-schema/energy/PUT_billing_tariff.json. Invalid schema: #. Invalid keyword: type. Invalid document: #/additionalTariffs"}

    Status: 400

     

    just to test that please change your code to follow (change the data inside received TariffGETdata)

     

    Please login or register to see this code.

     

    Have tested that and it works.

    Let me know if it works for you also

    • Like 1
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @cag014 

     

    Yes I saw that issue also, tired to work around it and it did work if I added an additionalTariffs (which i don’t want) like this:

    local requestData = {
      rate = 0.99,
      name = "Main",
      returnRate = 100,
      additionalTariffs= {
        {rate = 0.99,
          name = "Main2",
          startTime = "00:00",
          endTime = "00:00",
          days = {
            "monday"
          }
        }
      }
    }
     
     
    But your solution is much better! Smart to get the current data and then change a value in that and use it to “put it back”.
     
    Just tried your solution and it work just perfect!
     
    Thank you very much for your help to isolate and solve this!
    Link to comment
    Share on other sites

    • 0

    You're welcome?

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...