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

**URL:** <https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990>\
**Category:** Questions & Help\
**Tags:** homeyscript, advancedflow\
**Created:** [April 10, 2024, 2:31pm UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990 "2024-04-10T14:31:55Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![llw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/llw/32/86532_2.png) [@llw](https://community.homey.app/u/llw)\
**Post date:** [April 10, 2024, 2:31pm UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/1 "2024-04-10T14:31:55Z")

</div>

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](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/6/3/632598a4715b44cfb0ba5b0bb679111260612b6e.png)

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 🙂 )  
Thanks!

---

<div class="post-metadata">

**Author:** ![CaptainVoni](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/captainvoni/32/3549_2.png) [@CaptainVoni](https://community.homey.app/u/CaptainVoni)\
**Post date:** [April 10, 2024, 3:54pm UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/2 "2024-04-10T15:54:45Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/0/1/010653a0d0a9a59aa78e5d4ec8395f8b5c5afe44.png)

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.

---

<div class="post-metadata">

**Author:** ![llw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/llw/32/86532_2.png) [@llw](https://community.homey.app/u/llw)\
**Post date:** [April 11, 2024, 7:13am UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/3 "2024-04-11T07:13:52Z")

</div>

Good morning,  
you understood it right, this was what I needed 👍  
Thanks!

Now I just have one more problem to solve:  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/e/e/ee54f08af4a480c73b6383db83ced35884dde52f.png)  
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.

---

<div class="post-metadata">

**Author:** ![CaptainVoni](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/captainvoni/32/3549_2.png) [@CaptainVoni](https://community.homey.app/u/CaptainVoni)\
**Post date:** [April 11, 2024, 8:54am UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/4 "2024-04-11T08:54:22Z")

</div>

Can you share your flow?

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [April 11, 2024, 9:09am UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/5 "2024-04-11T09:09:44Z")

</div>

`Returned: undefined`

So your script isn’t returning anything.

---

<div class="post-metadata">

**Author:** ![llw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/llw/32/86532_2.png) [@llw](https://community.homey.app/u/llw)\
**Post date:** [April 11, 2024, 9:29am UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/6 "2024-04-11T09:29:19Z")

</div>

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](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/0/a/0a8a8d8ecda5a9f890f0dcefe6d68699174ff167.png)

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [April 11, 2024, 9:38am UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/7 "2024-04-11T09:38:50Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![llw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/llw/32/86532_2.png) [@llw](https://community.homey.app/u/llw)\
**Post date:** [April 11, 2024, 10:29am UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/8 "2024-04-11T10:29:34Z")

</div>

Thanks a lot!  
It works now!

---

<div class="post-metadata">

**Author:** ![Pairis](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pairis/32/56439_2.png) [@Pairis](https://community.homey.app/u/Pairis)\
**Post date:** [May 16, 2024, 5:11am UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/9 "2024-05-16T05:11:40Z")

</div>

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 🙂

```auto
// 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);
  });

```

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [May 16, 2024, 5:29am UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/10 "2024-05-16T05:29:41Z")

</div>

> [@Pairis](#):
>
> Or is this custom only haha.

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.

---

<div class="post-metadata">

**Author:** ![Pairis](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pairis/32/56439_2.png) [@Pairis](https://community.homey.app/u/Pairis)\
**Post date:** [May 16, 2024, 5:50am UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/11 "2024-05-16T05:50:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [May 16, 2024, 5:53am UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/12 "2024-05-16T05:53:05Z")

</div>

> [@Pairis](#):
>
> I’d like to get the hourly forecast from the weerlive api.

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

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [May 16, 2024, 11:20am UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/13 "2024-05-16T11:20:41Z")

</div>

> [@Pairis](#):
>
> ```auto
> const liveweer = data.liveweer[0];
> console.log(`Location: ${liveweer.plaats}`);
> 
> ```

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](https://jsonpath.com/) 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:

> [@Weerlive.nl](https://community.homey.app/t/weerlive-nl/16016/8):
>
> - `$` representeert het (JSON) object dat teruggestuurd wordt door de server
> - `liveweer` is de naam van een “key” in dat object; de waarde die bij de key hoort is een array van waarden (elke waarde is weer een object, dus `$.liveweer` is een array-van-objecten);
> - `[0]` is het eerste element van dat array; als je het tweede element wilt, moet je `[1]` gebruiken (enz);
> - `d0tmax` is de naam van een key in het (in dit geval) eerste element van de `liveweer` array
> 
> De expressie is een JSONPath expressie. Je kunt [hier](https://jsonpath.com/) in realtime zulke expressies uitproberen: plak het JSON resultaat van de weerlive server in het “JSON” veld, vul bovenin de JSONPath expressie in, en rechts zie je het resultaat.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [May 16, 2024, 11:26am UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/14 "2024-05-16T11:26:37Z")

</div>

> [@Peter\_Kawa](#):
>
> wanting to get certain values from returned JSON data from weerlive.nl

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.

---

<div class="post-metadata">

**Author:** ![Pairis](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pairis/32/56439_2.png) [@Pairis](https://community.homey.app/u/Pairis)\
**Post date:** [May 16, 2024, 12:10pm UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/15 "2024-05-16T12:10:35Z")

</div>

> [@Peter\_Kawa](#):
>
> I think this is about the same issue you replied to some years ago

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

---

<div class="post-metadata">

**Author:** ![Pairis](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pairis/32/56439_2.png) [@Pairis](https://community.homey.app/u/Pairis)\
**Post date:** [May 16, 2024, 12:15pm UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/16 "2024-05-16T12:15:34Z")

</div>

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

> [@robertklep](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [May 16, 2024, 12:17pm UTC](https://community.homey.app/t/get-value-from-homeyscript-in-a-variable-that-can-be-used-in-advanced-flow/107990/17 "2024-05-16T12:17:34Z")

</div>

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

And you left out `, Robert` in your quote:

> [@Peter\_Kawa](#):
>
> I think this is about the same issue you replied to some years ago, Robert:
