Homey Community Forum

Homeyscript and storage of variables

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.

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.

1 Like

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

1 Like

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.

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

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

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

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

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

1 Like

Very very nice

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😇

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

//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');

}

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.

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

Thx

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

It may have all the variables you are looking for…

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

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

Seems like a really simple thing to test…?

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

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

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

1 Like