Getting Started - Logic variables

Okay Morgan, I’ll give HS a try. Thanks!

Is there a way to calculate an avarage over a certain period, f.e. take a value every 30 mins and calculate the average on the last 30 values that were taken (so the value is an avarage over the last 30 mins)
I’ve tried a couple of times but I soon get stuck into a difficult web of flows and variables, maybe I’m overlooking something

I googled and came to this tutorial which mentions an average, but is far as I understand it just an avarage between 2 or more sensors that is talked about in the OP

This is a way to put an array random into a variable. I use it to shuffle my radiostations and playlist and put them into better logic variables. It does not work with Logic variables.

// Script by Marcel for randomly playing radiostations and playlists from Spotify with your Sonos Device
// With the use of a Synology Server and the Sonos Docker Api 
// Using Homey's apps Better Logic and HTTP request

// Add your Server's Ipadres and PortnumerPrefix voor radiostations en playlists
const IpAdres = ''; //Add your local ip adres of your server e.g ''
const Portnumber = '5005';//Add your portnumber of your server for the Sonos api

// Do not adjust 
const RadioPrefix = 'http:%2F%2F' + IpAdres + ':' + Portnumber + '%2Fwoonkamer%2Ffavorite%2F';
const PlayListPrefix = 'http:%2F%2F'+ IpAdres + ':'+ Portnumber + '%2Fwoonkamer%2Fspotify%2Fnow%2Fspotify:user:spotify:playlist:';

//Add your station at //Radiostations. In your Sonos app, from Sonos(not Homey) you
//need to add your radiostation as favorite. Use this exact name in this script
//I have added three playlists. Therefore the variable names Radio12, Radio13 en Radio14 must be
//stay the same and not been altered. 

//To add some random playlist I have made 17 playlist which you can adjust. Just search the ID 
//of your playlist in your Spotify App(not Homey) and paste this ID at //PlayList between 
//the quotes

// Add 14 string variables in Better Logic, called RadioDay1, RadioDay2.....Radioday3, three extra
//string variables called PlayListDay1, PlayListDay2 and PlayListDay3, and three string variables 
// called PlayListName1, PlayListName2 and PlayListName 3. The variable names are case sensitive.

//Radiostations=> Add the name of your Sonos favorite between the quotes
const Radio1 = RadioPrefix + '538 Ibiza Radio';
const Radio2 = RadioPrefix + 'Deep Radio';
const Radio3 = RadioPrefix + '538 Dance Radio';
const Radio4 = RadioPrefix + 'Dance FM';
const Radio5 = RadioPrefix + 'Deep House Ibiza';
const Radio6 = RadioPrefix + 'Deep Radio';
const Radio7 = RadioPrefix + 'ON Disco';
const Radio8 = RadioPrefix + 'Dance FM';
const Radio9 = RadioPrefix + 'Sky Radio 101 FM';
const Radio10 = RadioPrefix + 'Disco Radio Action';
const Radio11 = RadioPrefix + 'Hit List';
const Radio12 = 'Playlist1';//Do not adjust this variable
const Radio13 = 'Playlist2';//Do not adjust this variable
const Radio14 = 'Playlist3';//Do not adjust this variable

//Playlist => Add the ID of your Spotify playlist between the quotes. For your own needs you can add the 
//name of the playlist after the //

const PlayListId0 = '7GQS3BcOeogh8wKEeA3fN8?si=taswh15eRZuXmFAkyn6IfQ'//12inch Classics Part 1
const PlayListId1 = '1C5F8dA3nWRN46u6lCehqm?si=67RdCbXtSG27gx6C7qP2Sw'//Deep Delight Part 2
const PlayListId2 = '1QdAD45sKZQwptkm4VeKmO?si=_5P2-G9-SZG91WsNHt0f4g'//360 Dance Part 2
const PlayListId3 = '4YKqPMOlV5JJK6XVKKdWJ9?si=SL7iH53eSNu-PnsZJIcr6A'//Deep House Part 2
const PlayListId4 = '7jzMzltNsYhaI5Xfkxe9If?si=IwDTD4ylQJC9c5JWxEUXQw'//Massive Pop Remixes Part 1
const PlayListId5 = '5sInx9KNxVqnONg9EsIptX?si=8T0QWluxSDqI3bClrf0OOQ'//Dance Classics Part 1
const PlayListId6 = '0NhMKI95ujIjriBaRXrIBs?si=pG_J4OXGTz6mhN_kG1mvMg'//Dance Pop Remixes Part 1
const PlayListId7 = '1s4YUBb95LaWazpgvNGzAu?si=35wEzzqcQIKAgSZq7bkEaA'//Deep House Relax Part 3 
const PlayListId8 = '60HqFyCYzr6dxr4OMxDtS7?si=x7Oa5bNBQXy4DQlC2COLaw'//Truly Deeply House Part 1
const PlayListId9 = '00JuraVC5HSlXCLpYJ4RQx?si=LJFWf4UoQ-2DtayVAGb26g'//360 Dance Part 1
const PlayListId10 = '0C66rsP0YemHrh3dUhSszt?si=gdtcDlzURLWQVZCKi8Evkw'//Deep House Relax Part 2
const PlayListId11 = '4Vaus40H2m6gkwNqhzzril?si=6y9qg0HxQAqfB_B3bhfeIQ'//Deep Delight Part 1
const PlayListId12 = '0nXxaEdtBNILZHvltonbpT?si=4IxGc-mzQ7OGoFKCPGD2vA'//Dance Pop Part 1
const PlayListId13 = '3xDZn9OQLBxJBtWYSfNO3p?si=9C5nZsdZT7Kd3knE2KcngA'//Marcels Favorieten 2018 en 2019
const PlayListId14 = '28MJ5Ws6Hw7K3sM0buLxvS?si=sR9304GXTzikRt1n3so-MQ'//Marcels Favorieten 2017
const PlayListId15 = '4oH8GC0TMvQGlTXlj3luUq?si=RnmzYP4YRCWSPa9ptJTuRA'//Deep House Relax Part 1
const PlayListId16 = '6tN0M7JUSAUnPbqe4cPz8I?si=juDi1DjuQjms0iBzMowMag'//Marcels Favorieten 2016

//PlaylistNames => Add the Name ad you want to get presented of your Spotify playlist between the quotes. 

const PlayListName0 = '12inch Classics Part 1'
const PlayListName1 = 'Deep Delight Part 2'
const PlayListName2 = '360 Dance Part 2'
const PlayListName3 = 'Deep House Part 2'
const PlayListName4 = 'Massive Pop Remixes Part 1'
const PlayListName5 = 'Dance Classics Part 1'
const PlayListName6 = 'Dance Pop Remixes Part 1'
const PlayListName7 = '/Deep House Relax Part 3'
const PlayListName8 = 'Truly Deeply House Part 1'
const PlayListName9 = '360 Dance Part 1'
const PlayListName10 = 'Deep House Relax Part 2'
const PlayListName11 = 'Deep Delight Part 1'
const PlayListName12 = 'Dance Pop Part 1'
const PlayListName13 = 'Marcels Favorieten 2018 en 2019'
const PlayListName14 = 'Marcels Favorieten 2017'
const PlayListName15 = 'Deep House Relax Part 1'
const PlayListName16 = 'Marcels Favorieten 2016'

//Do not change the next variables
const PlayList0 = PlayListPrefix + PlayListId0//12inch Classics Part 1
const PlayList1 = PlayListPrefix + PlayListId1//Deep Delight Part 2
const PlayList2 = PlayListPrefix + PlayListId2//360 Dance Part 2
const PlayList3 = PlayListPrefix + PlayListId3//Deep House Part 2
const PlayList4 = PlayListPrefix + PlayListId4//Massive Pop Remixes Part 1
const PlayList5 = PlayListPrefix + PlayListId5//Dance Classics Part 1
const PlayList6 = PlayListPrefix + PlayListId6//Dance Pop Remixes Part 1
const PlayList7 = PlayListPrefix + PlayListId7//Deep House Relax Part 3 
const PlayList8 = PlayListPrefix + PlayListId8//Truly Deeply House Part 1
const PlayList9 = PlayListPrefix + PlayListId9//360 Dance Part 1
const PlayList10 = PlayListPrefix + PlayListId10//Deep House Relax Part 2
const PlayList11 = PlayListPrefix + PlayListId11//Deep Delight Part 1
const PlayList12 = PlayListPrefix + PlayListId12//Dance Pop Part 1
const PlayList13 = PlayListPrefix + PlayListId13//Marcels Favorieten 2018 en 2019
const PlayList14 = PlayListPrefix + PlayListId14//Marcels Favorieten 2017
const PlayList15 = PlayListPrefix + PlayListId15//Deep House Relax Part 1
const PlayList16 = PlayListPrefix + PlayListId16//Marcels Favorieten 2016

function wait(ms){//Declaring function wait
   var start = new Date().getTime();
   var end = start;
  while(end < start + ms) {
  end = new Date().getTime();}}

var nums = [Radio1,Radio2,Radio3,Radio4,Radio5,Radio6,Radio7,Radio8,Radio9,Radio10,Radio11,
var usedNums = [];

let BLApp = await Homey.apps.getApp({id:"net.i-dev.betterlogic" });
while (nums.length) {
    var nummer = nums.length.toString();
    var randomIndex = Math.floor(Math.random() * (nums.length));
    var randomNumber = nums.splice(randomIndex, 1)[0];
 //       console.log( 'First radiostation is: '+randomNumber.toString() + "   " + nummer) ;
 //       console.log('Nummer: '+nummer)
 //       console.log('Randomnummer is :'+randomNumber)
 //       console.log('RadioDay is :'+randomNumber)
let result=BLApp.apiPut("RadioDay"+nummer+"/" + randomNumber.toString());

var nums = [PlayList1,PlayList2,PlayList3,PlayList4,PlayList5,PlayList6,PlayList7,
PlayList16, PlayList0];
var usedNums = [];

while (nums.length) {
var nummer = nums.length.toString();
    var randomIndex = Math.floor(Math.random() * (nums.length));
    var randomNumber = nums.splice(randomIndex, 1)[0];
let result=BLApp.apiPut("PlayListDay"+nummer+"/" + randomNumber.toString());


let PlayListDay1 = await BLApp.apiGet('PlayListDay1');//Retrieving Better Logic variable 
PlayListDay1 = PlayListDay1.value
console.log('PlayListDay1: ' +PlayListDay1)
let PlayListDay2 = await BLApp.apiGet('PlayListDay2');//Retrieving Better Logic variable 
PlayListDay2 = PlayListDay2.value
console.log('PlayListDay2: ' +PlayListDay2)
let PlayListDay3 = await BLApp.apiGet('PlayListDay3');//Retrieving Better Logic variable 
PlayListDay3 = PlayListDay3.value
console.log('PlayListDay3: ' +PlayListDay3)

let PlayListDay = [PlayListDay1, PlayListDay2, PlayListDay3]
let PlayListDayNumber = 0
let PlayListIdNumber = -1
let PlayListId = [PlayListId0, PlayListId1, PlayListId2, PlayListId3, PlayListId4, PlayListId5, PlayListId6, PlayListId7, PlayListId8, PlayListId9, PlayListId10, PlayListId11, PlayListId12, PlayListId13, PlayListId14, PlayListId15, PlayListId16]
let PlayListName = [PlayListName0, PlayListName1,PlayListName2,PlayListName3,PlayListName4,PlayListName5,PlayListName6,PlayListName7,PlayListName8,PlayListName9,PlayListName10,PlayListName11,PlayListName12,PlayListName13,PlayListName14,PlayListName15,PlayListName16]

for (let Count = 0; Count <= 16; Count++){

  case Count:{
if (PlayListDayNumber === 3){break;}
PlayListIdNumber = PlayListIdNumber + 1
console.log('Count is: '+Count)
console.log('PlayListDayNumber is: '+(PlayListDayNumber+1))
console.log('PlayListIdNumber is : '+PlayListIdNumber)
console.log('PlayListId is: '+PlayListId[PlayListIdNumber])

x = PlayListDay[PlayListDayNumber].includes(PlayListId[PlayListIdNumber].substring(0,22))

if(x === true){
{BLApp.apiPut('PlayListDay'+[PlayListDayNumber +1] +'Name/' +PlayListName[PlayListIdNumber])
console.log('PlayListIdNumber is : '+PlayListIdNumber)
console.log('PlayListDay'+[PlayListDayNumber + 1] +'Name is: ' +PlayListName[PlayListIdNumber])
console.log('PlayListDayNumber is  nu nog: '+(PlayListDayNumber +1))
PlayListDayNumber = PlayListDayNumber +1
PlayListIdNumber = 0
Count = 0
console.log('PlayListDayNumber is nu : '+(PlayListDayNumber +1))


else continue;}

1 Like

Thanks @Marcel_Ubels, but… omg🤪

1 Like


  • Edit: added example flows

Did you check Insights i.c.w. the Insight Trends app?

With the Insights Trends app you might have your answer

There are 3 Insights apps available b.t.w.:

Example flows:
Insights trends average livingroom temp:

Result: Timeline message:

If you just use this script above and change Radio1, Radio2 etc for the names you want random you will everytime get a random name

Change result=BLApp.apiPut(“ RadioDay etc etc into result=BLApp.apiPut(“ Name etc etc
Make a better logic variabel called Name1 and everytime you run this script you will get random name

1 Like

Seems to do the trick! tnx, I’m going to look into it.
I think without an app it’s not possible using only Homey flows/logic

1 Like

With the better logic app you have the ability to use these variables in flows

What variables, from the Insight Trends app? I have Better Logic installed but did not yet get a real usecase to use it instead of homey logic (I’m sure there is, but I’m just starting building flows while finishing our new home)
I’m justing checking it, but I think you can use the Trends app also in homey logic?

BTW I mean: I think using just homey flows/logic it’s not possible to calculate an actual average without additional apps

I added a set of example flows to my post.
It works fine!
Insights trends example of average room temp over 24hrs:

Timeline result:

@zakraket I think Marcel replied to me but I’m not sure🙃

1 Like

I have a question about the Insight Trends app.
Is it also possible to monitor the power consumption between a certain time?
I suddenly have a power-consuming device or devices at night, but I have no idea which device it would be. I first want to know if it happens more often and then see which device it is.
If I want to view the Insight data from Homey and then the detailed 6 hours I have to turn on the PC in the morning and sometimes I am already gone. So if the app could do it, it would be great.
Where does the app possibly write it to or where can I have it written if the app can do this?
So that I can see what consumption does afterwards.

Insight trends takes one value over time as input. Not an array of f.i. all power values…
From what I understand the most simple way is to export Insights to .csv (for use with Excel) with this app: Exporteer Insights | Homey
In Excel you can sort stuff, and probably find the culprit that way.

I know you were looking for an automagic way but… This is also an option:
An alternative way is to open on a laptop or PC and click the Insights symbol on the left. Open a random graph, on the right of the graph, you’ll see a “+Add Graph” button. A popup appears, and you can filter all available graphs. Enter ‘Power’ f.i. Now only graphs with Power as measure phrase are selectable. Select all the available items.
Now you can fiddle with the timeframe selection. Take “last 7 days” f.i. Now I think you’ll find the culprit by scrolling down all the graphs.

Thank you @Peter_Kawa . I will take a look at both options.
Maybe I can see if there is some strange thing going on.
Today I had the same graph with the same lines of use of energy.
I hope I can find it. If is normal us it is ok, but at that time we are in bed.

1 Like

I already managed to create some flows (f.e. average humidity in two rooms), the logic behind the Trends cards was not immediatly clear to me. I thought Trends needed to monitor values for the set duration, but I now understand it just takes it’s data from insights (which is kinda logic of course :wink: ) so the wanted values is immedialty available as soon as you use one of these cards
Tnx for the tip, it does exactly what I need. Trends calculated values are not directly available in Homey Logic to use in other flows but you can assign them to Logic variables in a flow similar to your second.

edit: what I don’t get is how you would use the Trend card in the THEN part of a flow. so, IF thisflowstarted … AND…THEN “calculate Trend something” but you can’t do anything with what you calculate after that moment? Like when you start a flow with a Calculate Trend you have that available as a tag, but not when you calculate in the THEN section of the flow

Ah wait, I do know (because I used them in my own testflows :smile: ) when you “THEN calculate Trend” you can use that calculation as a trigger. In your example: you don’t need to trigger the second flow using “Start Flow” in your first flow, it will be triggered on the “IF calculated” in your second flow

So it would be f.e.

  • IF every 5 min THEN calculate Trend temperature
  • IF Trend Calculated Temperature AND < 20 THEN push “temp too low”

You also could have the first flow calculated more than 1 values, then you can use these as triggers in other flows

Flow 1 calculate all trends:

  • IF every 5 min THEN calculate Trend temperature, calculate Trend Humidity, calculate Trend Luminosity

Flow 2 do something with temp

  • IF trend calculated Temperature AND …THEN send push

Flow 3 do something with hum

  • IF trend calculated Humidity AND …THEN put in Logic variable

When you use calulate Trend in the AND section you can’t use the outcome in the THEN
f.e. IF trigger AND calculate Trend average > 0 then set Var to Average
Outcome is not available as a tag, which might be a shame.

edit: woops…app-author has a fight with Athom

I am building a ArrayAdapter for this precicly in HOOP.

Sounds good @Arie_J_Godschalk, but what is HOOP?

@zakraket Glad 2 hear you discovered the magic yourself.
I also didn’t get it at first🙃, but the THEN action card explains what do do

And how nice! The new testversion let’s you also calc trends live! No flows needed. Handy for troubleshooting

Tag “Average” value:

Here r my 2 example flows:

As u can see, you can use calculated tag(s) for further use.
Or did you mean someting else to re-use the tagvalue with:

{“When you use calulate Trend in the AND section you can’t use the outcome in the THEN
f.e. IF trigger AND calculate Trend average > 0 then set Var to Average
Outcome is not available as a tag, which might be a shame.”}

H.O.O.P. (Hope) - Homey Object Oriented Programming - Apps - Homey Community Forum (

1 Like