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

help with net.HTTPClient()


Question

Posted (edited)

Any experts on the net.HTTPClient() coding in here?

 

Am trying to build a QA for my car charger (Charge Amps HALO) and have access to the REST API. They have a bit different implementation to what I have worked with before, you need to login with email, password & APIkey to get a ”token” that you then use in other request. If I do this login on their swagger page I am able to get a token and then build a simple function to pull out information about my Charger, and this works just fine:

 

Code:

function QuickApp:GetData()
  http = net.HTTPClient()
    local url = "

Please login or register to see this link.

d"
    local requestBody = '

Please login or register to see this link.

d'
    local token 
="aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz"
 
    http:request(url, 
          {
            options = {
                data = requestBody, 
                method = "GET",
                headers = {
                           ["accept"] = "application/json"
                           ["Authorization"] = "Bearer " ..token
                           }
                       },
         success = function(response) 
                      print("response status: " ..response.status)
                      print("Response data: " ..response.data)
                    end
            })
end

Request URL from the Charge Amps swagger page>

Please login or register to see this code.

Curl from the Charge Amps swagger page>

Please login or register to see this code.

 

200

Response body from the Charge Amps swagger page>

Please login or register to see this code.

 

Log output in Fibaro HC3>

[21.04.2022] [18:34:25] [DEBUG] [QUICKAPP977]: response status: 200
[21.04.2022] [18:34:25] [DEBUG] [QUICKAPP977]: Response data: [{"id":"XXXXXXXXXXXXXXX","name":"XXXXXXXXXXXX","password":"XXXXXXXXXX","type":"HALO","isLoadbalanced":true,"firmwareVersion":"72","hardwareVersion":"2","ocppVersion":null,"settings":null,"connectors":[{"chargePointId":"XXXXXXXXXX","connectorId":1,"type":"Charger","settings":null},{"chargePointId":"XXXXXXXXXXX","connectorId":2,"type":"Schuko","settings":null}]}]
 
So the above part are working just fine, sensitive information have above been replaced with bogus information. But the issue I have is that I want to do another ‘function’ that handles the login to get a token (there is a login “swagger”on the Charge Amps swagger page) that can be stored in the HC3 as a variable, the  “tokens" are only valid for 2 hours and need to be regenerated several times each day to keep the QA working.
 
To be able to do this you need to provide apiKey, email & password, but I can’t get that to work….. Doing it on the Charge Amps swagger page works just fine, but need this in a ‘function’ in the QA to be able to automate the collection of ‘token’ :

Curl from the Charge Amps swagger page>

Please login or register to see this code.

Request URL from the Charge Amps swagger page>

Please login or register to see this code.

Response body from the Charge Amps swagger page>

Please login or register to see this code.

Code I have tried:

function QuickApp:CAlogin()
 http = net.HTTPClient()
 local requestBody = 'https://eapi.charge.space/api/v4/chargepoints/owned'
 local APIkey = "xxzzyyxxzzyyxxzzyy=="
 
http:request("https://eapi.charge.space/api/v4/auth/[email protected]&password=aabbccddeeff",{
    options = {
               data = requestBody, 
               method = "POST",
               headers = {
                          ["Content-Type"] = "application/json",
                          ["accept"] = "*/*",
                          ["apiKey"] = APIkey
                          }
              },
              success = function(response) 
                        print("Status", response.status)
                        print("Data", response.data)
                        end,
              error = function(message)
                      print("error:", message)
                      end
                }) 
end
 
This simply doesn’t  work and just gives a 400 as status code in the Fibaro log. But how do I do a net.HTTPClient() request that includes apiKey, email and password as required by Charge Amps? Tried to put email and password in the URL but that does not seem to work. Have also tried to put it in other parts of the code but it simply doesn’t work, always gives me a 400 status code.
Edited by JcBorgs

Recommended Posts

  • 0
Posted

A brief update.  Got the API key quickly. The app works great in relation to the ChargeAmp API. 

 

I run the Yubii app in "dark mode". That made the text impossible to read some of the text, but I quickly changed all #0 and #000000 to #5794f2 as you use in other places. 

 

Something I have not yet figured out though is the automatic calculation. It does not seem to trigger properly, or it is running a bit unexpected.  When I change the "calculate charging hours at" to an hour not yet happened, it is empty the "start charger at",  and "stop charger at". If I change the "calculate charging hours at" to a time that has passed, then I get a value at "start charger at" and "stop charger at".  It does not take the numbers of hours in considerations. It always present a span of 5 hours, not the number of hours I have selected (4) in at "charge the car for".  Will continue to read the code and see where it might fail. 

 

One idea I immediately got is that the scheduled recalculation could get a "calc now" button as well to create a new block (valid until next automatic calculation). 

 

  • 0
  • Inquirer
  • Posted

    Yes, know that it is not good formatet for "dark mode" 🫣

     

    That is strange, the automatic calculations have been working fine in the tests I have done so far. But was a couple of month ago so will check that in my setup as well.. If you get strange behavior your could try the pause/resume buttons. Regarding the number of hours I do believe it is the same issue, the QA gets in an incorrect mode and does not work as it should, also here the pause/resume could work.

    But will dig in a bit when I have more time and is at home later this week.

    • 0
  • Inquirer
  • Posted

    @Håkan Enhager

     

    Did a quick look now but can not reproduce your issues. 

     

    Below are output from my log, I had the QA set to calculate at hour 20 and charge for 4 fours. Now changed this to calculate at hour 18 and charge for 7 hours.

     

    Every (TRACE) in the log is an button press in the QA and at the bottom of the log at 18:05 you see that it gives me new charging start and stop hours and that the charge time is 7 hours.

     

    Could you please do some similar tests in your HC3 and look at the log?

    Please login or register to see this code.

     

    • 0
    Posted

    Problem solved (embarrassing)...I was too fast yesterday, made a minor typo in the TibberMonitorQA-variable.  The QA still had values in StartHour and StopHour, which was not visible in the QA immediately, but after I had paused/resumed. So I thought it had updated properly since the hours also were correct based on last night spot prices.  Once I corrected the TibberMonitorQA-variable it works perfect. 

    • 0
  • Inquirer
  • Posted

    @Håkan Enhager

     

    Good to hear, thanks for the update.

    Will look into the Calculate Now button, was a good idea! Please reach out if you see anything else or have any other improvement suggestions.

     

    • 0
    Posted

    Hi, after a few days of testing I have found an issue.  Not sure yet what it relates to, and has not yet had time to check code, or API definitions from CA. 

     

    Whenever the QA now changes from On to Off or from Off to On (both ways), it sets the "Current limit" from 16A to 0A leading to that when the car should charge, the car will complain that there is "no power in the cable".  If I in the CA-app edits the "Current limit" to 16A it starts immediately.  Currently only identified the symptom.  Not sure if it could be related to e.g. how my firmware version (73) interpret the calls from the QA.

     

    Setting the Current Limit could by the way be a nice feature.  Running Solar Panels, and especially if the electricity is very cheap when sun is shining (then I do earn so much when selling electricity), I might want to allow a scene to identify that it is a good time to allow the charger to be ON, but with regards of the capacity on the solar panels I would like to set a lower current limit to avoid that I purchase electricity.

     

    I will come back when I no more about the issue with the "current limit". 

    • 0
  • Inquirer
  • Posted (edited)
    4 hours ago, Håkan Enhager said:

    Hi, after a few days of testing I have found an issue.  Not sure yet what it relates to, and has not yet had time to check code, or API definitions from CA. 

     

    Whenever the QA now changes from On to Off or from Off to On (both ways), it sets the "Current limit" from 16A to 0A leading to that when the car should charge, the car will complain that there is "no power in the cable".  If I in the CA-app edits the "Current limit" to 16A it starts immediately.  Currently only identified the symptom.  Not sure if it could be related to e.g. how my firmware version (73) interpret the calls from the QA.

     

    Setting the Current Limit could by the way be a nice feature.  Running Solar Panels, and especially if the electricity is very cheap when sun is shining (then I do earn so much when selling electricity), I might want to allow a scene to identify that it is a good time to allow the charger to be ON, but with regards of the capacity on the solar panels I would like to set a lower current limit to avoid that I purchase electricity.

     

    I will come back when I no more about the issue with the "current limit". 

     

    Thanks for the finding @Håkan Enhager !

     

    This is due to an incomplete cleaning work in the code after some tests I did long ago….

    I will update the code and provide an updated that work and doesn’t have this issue.

     

     

    Edited by JcBorgs
    • 0
  • Inquirer
  • Posted (edited)

    @Håkan Enhager

     

    Attached is an updated version with corrections for what you found.

    Thanks for the testing and feedback!

     

    Please login or register to see this attachment.

    Edited by JcBorgs
    • 0
  • Inquirer
  • Posted
    1 hour ago, Håkan Enhager said:

    Thanks a lot, installed and ready to try for tonight!

    Looking forward to your tests results

    • 0
  • Inquirer
  • Posted

    @Håkan Enhager

    How did your test go?

    • 0
    Posted

    After a few busy weeks... everything works  great with the app!

    • 0
  • Inquirer
  • Posted
    On 1/29/2023 at 7:15 AM, Håkan Enhager said:

    After a few busy weeks... everything works  great with the app!

    Thanks for the update!

     

    • 0
  • Inquirer
  • Posted (edited)

    @Håkan Enhager

    Added a new feature, it is now possible to change the Current(Ampere) you want to charge the car with. Simple implementation of what I think you was asking for earlier.

     

    The range is fixed to 4->16 and you choose it with +/- buttons in the QA.

    Please login or register to see this attachment.

    Edited by JcBorgs
    Added image
    • 0
  • Inquirer
  • Posted

    @Håkan Enhager Sorry! Will try to fix an updates version for you in the besinning of the weekend.

     

    • 0
  • Inquirer
  • Posted

    @Håkan Enhager

     

    Sorry for the delay!! New version attached, you need to replace your existing version with this one.

     

    Please login or register to see this attachment.

    • 0
    Posted

    The updates version seem to work well!

     

    One thing I noticed which differs from native ChargeAmp app is that after I change the "current limit" I must either turn it off or turn it on in the QA to have the "current limit" to be updated. Easy to compensate for by additional step in a scene. 

    • 0
    Posted

    Another observation, still manageable, but the names of the buttons trying to trigger them from a scene are the same, several buttons called ON, OFF, +, - etc.  Since I know in what order they are, it is not a problem.  

    Please login or register to see this image.

    /monthly_2023_04/image.png.521ee9dac3ada8d2c08393c42a9272d2.png" />

    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...