Trying to call maker endpoint using JS

Is there something unique about trying to call the maker endpoint from javascript? I have a simple fetch calling a valid URL (I tried it in browser) and I get a 200 response code. However, there is nothing in the response.

https://cloud.hubitat.com/api//apps/453/devices/162?access_token=;

Am I missing something? I try the same code with a different endpoint (not hubitat) and it works fine.

No cloud token?

thanks for the reply - something happened when I pasted in the URL (it is correct in the code). I do see the URL formed correctly and I do get a 200 response code.

Can you show the entire command? Just use "x" in place of the cloud token and access token. I am not sure what the semi-colon (";") at the end of your command is for.

Put the same URL in your browser. Do you get the JSON response you expect? That will test both the URL and the Maker API setup itself while taking your JS code out of the picture as a possible issue.

1 Like

Yes, as noted I do get the json that I am expecting when I put it into the browser.

I put together a fiddle to show the issue in action. The code works when I call the public api but not when I call hubitat. Is it the length of the URL that is causing an issue? Or maybe the question mark?

document.getElementById("basicFetchButton").addEventListener("click", function(){
        // Loading the jQuery code

 // var url = 'https://cloud.hubitat.com/api/1e57ce5a-c3cf-468e-8a0b-3519bdb8da5f/apps/453/devices/all?access_token=<access token>';
  var url = 'https://date.nager.at/api/v2/publicholidays/2020/US';
        fetch(url)
            .then(function (response) {
                response.text().then(function (responseText) {
                    console.log(responseText);
                    alert("Done using Fetch!");
                });
            });
    });

Sorry, I missed that point in your original post.

There's an error that comes back with the HTTP 200 response in your fiddle environment.

I'm not knowledgeable enough on JS to debug it, but this example worked for me: javascript - How to make a JSON call to an URL? - Stack Overflow

This is the environment that I used for my test: Online JavaScript Compiler (Editor)

thank you again for your response. It is weird how sensitive this can be depending on the environment in which it is operating.

I need to clean this up a bit. This is what I got working when trying to call the thermostat via maker from an AWS Lambda function (for use with Alexa).

 case "WhatIsNewThermostatTemperature":
                 https.get(thermostatURL, (resp)=>{

                  let data = '';
                
                  // A chunk of data has been received.
                  resp.on('data', (chunk) => {
                    data += chunk;
                  });
                
                  // The whole response has been received. Print out the result.
                  resp.on('end', () => {
                    console.log("DATA", data);
                     var conditions = JSON.parse(data);

                   var values = conditions['attributes'];
                   var thermTemp = '';
                   values.forEach((thermType) => {
                   console.log (thermType.name);
                        if (thermType.name === 'temperature') thermTemp = (thermType.currentValue);});
                         context.succeed(
                  generateResponse(
                    buildSpeechletResponse(`Current thermostat temperature is ${thermTemp} degrees `, true),
                 {}  )
                )
                  });
                  
                }).on("error", (err) => {
                    console.log("Error: " + err.message);
                });
1 Like

I'm intrigued by your AWS/Alexa use-case. Could you point me to a reference on what you're doing with that?

Sure thing! Long story short, I wanted to be able to pull back the temperature of my pool. I had this working via a raspberry pi pool sensor that I created a while back. That unit died, so I am now trying to connect ecowitt gateway and sensors to Hubitat so that I can query it.

So long story short, Alexa can access any device in Hubitat using maker API. The hubitat skill is the easier way to do this, but if you go this route you could have additional access and be able to customize the responses that Alexa gives. For example, if my wife asks for the humidity from the weath station, the message tells her not to even think about putting on the AC.

Here is a short blog that I created: Alexa, What's the Pool Temperature? - Adam's Projects

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.