Get value from homeyscript in a variable that can be used in advanced flow

Hello,
I would like to use the value returned by the function as variable so I can use this one further in the Flows.
I don’t know how to register this value in a variable (not in homeyscript nor in Flow).
image

function solarangle(azimuth,altitude){
var teta=90-altitude;
var beta = 90 - Math.atan(Math.tan(teta*Math.PI/180)*Math.sin((azimuth-167)*Math.PI/180))*180/Math.PI;
return beta;
}

So I would like to get the value of “beta” in an existing variable to use in Flow.
I hope that someone can help me and sorry if the answer is already existing but I haven’t found the solution yet (or at least not understood it :slight_smile: )
Thanks!

Maybe I misunderstand you, but have you tried the respective logic flow card (sorry, screenshot is in German):

Connect your homeyscript card with the logic card to set variables, select the variable in the logic card (add it if necessary in the variables menu at the top) and select the tag #Result as value.

2 Likes

Good morning,
you understood it right, this was what I needed :+1:
Thanks!

Now I just have one more problem to solve:
image
When I check the return value in the script, I get the expected number so I don’t understand why I get this error message. All values of the variables were checked and are correct and only numerical.

Can you share your flow?

Returned: undefined

So your script isn’t returning anything.

Here is the function:
function solarangle(azimuth,altitude){

let teta = 90 - altitude;
let beta = 90 - Math.atan(Math.tan(teta*Math.PI/180)*Math.sin((azimuth-167)*Math.PI/180))180/Math.PI;
let percentage = 0;
if (beta >150){
percentage = 100;
} else {
percentage = Math.round(beta/150
100);
}
return percentage;
}

image

Can you see why the script would not return the value?

Your script only declares a function but doesn’t actually call it. It also doesn’t do anything to parse the argument.

Try adding this at the end of the script:

const input = JSON.parse(args[0]);
return solarangle(input.azimuth, input.altitude);
1 Like

Thanks a lot!
It works now!

Hi Robert,

Could you breakdown your solution so it might be used universally? Or is this custom only haha.

I’m facing the same thing with my script :slight_smile:

// Define the API key and location
const apiKey = '';
const location = '';

// Construct the API URL
const url = `https://weerlive.nl/api/weerlive_api_v2.php?key=${apiKey}&locatie=${location}`;

// Send a GET request using fetch
fetch(url)
  .then(response => {
    if (!response.ok) {
      throw new Error('Network response was not ok');
    }
    return response.json(); // Parse the response as JSON
  })
  .then(data => {
    console.log(data); // Log the JSON response
    // Access specific data points to verify parsing
    const liveweer = data.liveweer[0];
    console.log(`Location: ${liveweer.plaats}`);
    console.log(`Temperature: ${liveweer.temp}°C`);
    console.log(`Summary: ${liveweer.samenv}`);
  })
  .catch(error => {
    console.error('There has been a problem with your fetch operation:', error);
  });

Not really, it’s fairly basic Javascript. I don’t know what your script is supposed to do, so not sure what exactly should be fixed.

I’d like to get the hourly forecast from the weerlive api.

Calling the api gives everything (day& hour ect), with its okay, but it also gives the error mentioned above.

I can see the overall purpose of your script, what what should it return exactly?

I think this is about the same issue you replied to some years ago, Robert: wanting to get certain values from returned JSON data from weerlive.nl

Your post below in English:

  • $ represents the (JSON) object returned by the server
  • liveweer is the name of a “key” in that object; the value associated with the key is an array of values (each value is another object, so $.liveweer is an array-of-objects)
  • [0] is the first element of that array; if you want the second element, you have to use [1] (etc);
  • d0tmax is the name of a key in the (in this case) first element of the liveweer array

The expression is a JSONPath expression. You can here try such expressions in real time: paste the JSON result from the weerlive server into the “JSON” field, enter the JSONPath expression at the top, and on the right you can see the result.

Original Dutch post:

1 Like

Yes, I understand that, but the script is logging three values and I have no idea what @Pairis wants the script to actually return.

However, it’s probably a lot easier to not use a script at all and just use the (advanced) flow cards that can be used to retrieve and extract JSON.

1 Like

Did I? If so, I am sorry but I do not have any recollection of that.

Apologies for derailing the OP. I was hoping @robertklep quick fix could also work for mine.

Probably so, I might make a separate topic explaining the case that will make things more clear.

Ah, I think there’s a misunderstanding here. I was under the impression I replied to Robert, and I quoted Robert’s reply from back then in 2019 :wink:.

And you left out , Robert in your quote:

1 Like