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

External API calls from LUA on HC3


zedmeister

Question

I have written the following code:

 

-- Function to get prayer times
function getPrayerTimes(latitude, longitude)
    -- URL of the Aladhan API
    hub.debug("",type(latitude))
    local url = "http://api.aladhan.com/v1/timings?latitude=" .. latitude .. "&longitude=" .. longitude .. "&method=2"
    fibaro.debug("","Prayer times URL: " .. url)
    -- Make a GET request to the Aladhan API
    local http = net.HTTPClient.new({ timeout = 5000 })
    http:request(url, {
        options = {
            method = 'GET',
            --headers = {['Accept'] = 'application/json'},
            --headers = {["Content-Type"] = "application/json"},
        },
        success = function(response)
            -- Parse the JSON response
            local data = json.decode(response.data)
            fibaro.debug("","Prayer times response data: " .. response.data)
 
            -- Check if the response contains the expected data
            if data and data["data"and data["data"]["timings"then
                -- Get the prayer times
                local timings = data["data"]["timings"]
                fibaro.debug("","Prayer times: " .. json.encode(timings))
 
                -- Set global variables
                fibaro.setGlobalVariable("FAJR", timings["Fajr"])
                fibaro.setGlobalVariable("ZUHR", timings["Dhuhr"])
                fibaro.setGlobalVariable("ASR", timings["Asr"])
                fibaro.setGlobalVariable("MAGRIB", timings["Maghrib"])
                fibaro.setGlobalVariable("ISHA", timings["Isha"])
 
                -- Debug log
                fibaro.debug("","Prayer times set successfully")
            else
                fibaro.debug("","Unexpected response data: " .. response.data)
            end
        end,
        error = function(err)
            -- Handle errors
            fibaro.debug("","Error getting prayer times: " .. err)
        end
    })
end
 
-- Get location from global variables
local latitude = fibaro.getGlobalVariable("LocationLatitude")
local longitude = fibaro.getGlobalVariable("LocationLongitude")
 
-- Get prayer times
getPrayerTimes(latitude, longitude)
 
As you can see, I have several debug messages to check what is going on including the URL. When I copy and paste the URL shown in the console into a browser, I get the correct data back. However, the console shows:
 
[DEBUG] []: Unexpected response data: { "code": 400, "status": "BAD_REQUEST", "data": "Please specify a latitude and longitude." }
 
Any idea why? Help would be most appreciated! I can't figure out whether is some kind of encoding issue (string, number, base64, etc) or header issue or there is something I am doing wrong in the call?
Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • Just to add, I have tried the longitude and latitude as both strings and numbers. The API is sensitive to case I have seen on the command line.

    Link to comment
    Share on other sites

    • 0

    This works for me - guess it's something strange with you LocationLatitude or LocationLongitude

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 9 hours ago, jgab said:

    This works for me - guess it's something strange with you LocationLatitude or LocationLongitude

    Please login or register to see this code.

     

    Many thanks for your help. The output from your print statement is:

     

    [28.02.2024] [17:21:38] [DEBUG] [QUICKAPP921]: { "cod

     

    I have tried to reboot the HC3 but it just shows this truncated line so I can't really tell if its working.

    Link to comment
    Share on other sites

    • 0

    Shouldn’t there be a date in the URL?

     

    Like: 

     

    local url = "http://api.aladhan.com/v1/timings/28-02-2024?latitude=" .. latitude .. "&longitude=" .. longitude .. "&method=2"

     

     

     

    Edited by SmartHomeEddy
    • Like 1
    Link to comment
    Share on other sites

    • 0
    2 hours ago, zedmeister said:

    Many thanks for your help. The output from your print statement is:

     

    [28.02.2024] [17:21:38] [DEBUG] [QUICKAPP921]: { "cod

     

    I have tried to reboot the HC3 but it just shows this truncated line so I can't really tell if its working.

    It's just that the data contains the "<" character and then the HC3 logs cuts the output.

    Try

    Please login or register to see this code.

     

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