# Homeyscript and storage of variables

**URL:** <https://community.homey.app/t/homeyscript-and-storage-of-variables/50772>\
**Category:** Developers\
**Created:** [August 24, 2021, 12:16pm UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772 "2021-08-24T12:16:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 24, 2021, 12:16pm UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/1 "2021-08-24T12:16:38Z")

</div>

Is there a way to store your variables on your own server. I know you can store them with better logic. That is a solution. For my purposes i want to store them on my Synology server, and also retrieve them from my server.  
Why? It are a awful lot of variables i have, and they are growing and growing.

Because these variables are all from 1 script, it would be nice if this is possible.

I assume store them in the script is not possible? The variables i am talking about are requested from a api call from my Solar Edge converter and some other api calls. So i never know what the values are that day.

---

<div class="post-metadata">

**Author:** ![RonnyW](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ronnyw/32/10879_2.png) [@RonnyW](https://community.homey.app/u/RonnyW)\
**Post date:** [August 24, 2021, 2:00pm UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/2 "2021-08-24T14:00:55Z")

</div>

Is MQTT a possible solution?

You could provide the variables in the script as flow tags.  
Start the flow in Homey and fill each flow tag (your variables) into a MQTT topics.  
If you run the MQTT broker on your Synology, you have “stored” the data in MQTT (send as retained message to keep the data stored).  
You can retrieve the variables using the MQTT client reading the topics.

You need a way to “export” teh data from HomeyScript and the flow tags are the easiest way. Instead on MQTT you can use http/Webhooks. Then you will need something on your NAS to react on the Webhooks.

---

<div class="post-metadata">

**Author:** ![Jero](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jero/32/17556_2.png) [@Jero](https://community.homey.app/u/Jero)\
**Post date:** [August 24, 2021, 4:02pm UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/3 "2021-08-24T16:02:09Z")

</div>

You can store globals in a script [https://github.com/athombv/com.athom.homeyscript#globalsetstring-key-mixed-value—undefined](https://github.com/athombv/com.athom.homeyscript#globalsetstring-key-mixed-value---undefined).

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 25, 2021, 10:37am UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/4 "2021-08-25T10:37:37Z")

</div>

I am not that good with js. Do you have an example how i store and retrieve a variable in another script.

I think this can be the solution for my question.

---

<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:** [August 25, 2021, 10:38am UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/5 "2021-08-25T10:38:43Z")

</div>

```auto
// store
global.set('my_variable', value);

// retrieve
const value = global.get('my_variable');

```

---

<div class="post-metadata">

**Author:** ![RonnyW](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ronnyw/32/10879_2.png) [@RonnyW](https://community.homey.app/u/RonnyW)\
**Post date:** [August 25, 2021, 11:23am UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/6 "2021-08-25T11:23:51Z")

</div>

To set a flow tag in a script:  
await tag(“tagname”, value );

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 25, 2021, 3:31pm UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/7 "2021-08-25T15:31:13Z")

</div>

Ok and where will the variable be stored. Do i have to give the name of the script somewhere?

---

<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:** [August 25, 2021, 4:04pm UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/8 "2021-08-25T16:04:47Z")

</div>

They are stored on Homey, “globally”, so accessible from any Homeyscript.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 25, 2021, 6:11pm UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/9 "2021-08-25T18:11:12Z")

</div>

Very very nice

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 25, 2021, 6:45pm UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/10 "2021-08-25T18:45:38Z")

</div>

As always when you know where to search….  
In homeyscript there are a few examples. 1. Example-global-set 2. Example-global-get

It works😇

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/0/d/0d4df08c5d79e612bc67f2c782ba61796104a81a.jpeg)

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 26, 2021, 11:41am UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/11 "2021-08-26T11:41:22Z")

</div>

I am trying this, but cannot get to retrieve these variables with values

```auto
//Updating better logic variables once every day

let urltoday = ('https://api.sunrise-sunset.org/json?lat='+Latitude +'&lng='+Longitude +'&date=today'+'&formatted=0');

response = await fetch(urltoday);

ApiSunriseSunset = await response.json();

DayLightToday = Number(ApiSunriseSunset.results.day_length);

//BLApp.apiPut('DayLightToday/'+DayLightToday); 

global.set('DayLightToday',DayLightToday)   

      console.log('DayLightToday after update is : ' +DayLightToday +' seconds');

DayLightUntilToday = Number(DayLightUntilToday + DayLightToday);

//BLApp.apiPut('DayLightUntilToday/'DayLightUntilToday);

global.set('DayLightUntilToday','DayLightUntilToday')    

      console.log('DayLightUntilToday after update is : ' +DayLightUntilToday +' seconds');

}

```

```auto

```

---

<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:** [August 26, 2021, 11:45am UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/12 "2021-08-26T11:45:47Z")

</div>

~~Your code isn’t retrieving any variables.~~

Hang on. Do you mean Logic variables and not _code_ variables?

Also, the second `.set()` is setting a string, not the value of `DayLightUntilToday`.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 26, 2021, 12:10pm UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/13 "2021-08-26T12:10:09Z")

</div>

You are right i was adjusting the script but hadn’t the global.get command not at the right point

Thx

---

<div class="post-metadata">

**Author:** ![Ollem](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ollem/32/9413_2.png) [@Ollem](https://community.homey.app/u/Ollem)\
**Post date:** [August 27, 2021, 7:58am UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/14 "2021-08-27T07:58:22Z")

</div>

Using script is of course more fun, but are you aware of this app?

> **[Zonnestanden](https://homey.app/nl-nl/app/com.cyclone-software.sunevents/Zonnestanden/)**
>
> Bij diverse zonnestanden kunt u triggers maken, u kunt ook uw eigen events defineren.

It may have all the variables you are looking for…

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 27, 2021, 9:29am UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/15 "2021-08-27T09:29:40Z")

</div>

Absolutely more fun. But this app doesn’t count the minutes the sun is up per day for a whole month. That is what i need to make a prognosis for my solarpanels for every day in a month.

But i also like to use different api’s because sometimes they have more information as in the relevant app

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 27, 2021, 6:44pm UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/16 "2021-08-27T18:44:37Z")

</div>

@robertklep Is it also possible to set an array with global.set?

---

<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:** [August 28, 2021, 5:00am UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/17 "2021-08-28T05:00:39Z")

</div>

Seems like a really simple thing to test…?

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [August 28, 2021, 6:51am UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/18 "2021-08-28T06:51:10Z")

</div>

Have tried yesterday several things, but can’t get it working. I will post tonight a screenshot, because somehow it looked like there was array

---

<div class="post-metadata">

**Author:** ![RonnyW](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ronnyw/32/10879_2.png) [@RonnyW](https://community.homey.app/u/RonnyW)\
**Post date:** [August 28, 2021, 12:36pm UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/19 "2021-08-28T12:36:48Z")

</div>

Perhaps you can only use basic types like number or string.  
You could convert the array to string and store it in the variable.

---

<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:** [August 28, 2021, 12:43pm UTC](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772/20 "2021-08-28T12:43:51Z")

</div>

> [@RonnyW](#):
>
> Perhaps you can only use basic types like number or string.

Like I said, it’s trivial to test. And it works as expected:

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/c/f/cf94bae6a3ff8e846eb8fe4acd07d58edc2d48b1.jpeg)

[Next page](https://community.homey.app/t/homeyscript-and-storage-of-variables/50772.md?page=2)
