Homey Community Forum

How to lauch screensaver from HomeyScript

Hello, I trying to start a random screensaver from HomeyScript.

Code seems te be ok, I only get a 403 error (missing_scopes). Anyone some tips?

const devices = await Homey.ledring.getScreensavers();
let random = getRandomItem(Object.values(devices));
log(random.id);

let opt = { value : { uri : random.uri, id : random.id }};

await Homey.ledring.setOptionScreensaver(opt)
.then(() => log(‘OK’))
.catch(error => log(Error:, error));

function getRandomItem(arr) {

// get random index value
const randomIndex = Math.floor(Math.random() * arr.length);

// get random item
const item = arr[randomIndex];

return item;

}

Basically, setOptionScreensaver (I assume that’s what’s causing the error) requires permissions that HomeyScript doesn’t have (and you can’t change those permissions, Athom decides which permissions HomeyScript should have).

2 Likes

Actually, you can do it in javascript using runFlowCardAction. I have modified your code a little

const screensavers = await Homey.ledring.getScreensavers();
let random = getRandomItem(Object.values(screensavers));
log(random);

setScreenSaver(random)

await wait(15*1000)

const turnOff = { uri : 'homey:manager:ledring', id : 'off' };
setScreenSaver(turnOff)

function  setScreenSaver(opt) {
  Homey.flow.runFlowCardAction({
    id: 'set_screensaver',
    uri: opt.uri,
    args: { screensaver: { uri: opt.uri, id: opt.id } },
    delay: null,
  });
}

function getRandomItem(arr) {
  // get random index value
  const randomIndex = Math.floor(Math.random() * arr.length);
  // get random item
  const item = arr[randomIndex];
  return item;
}
3 Likes

Thanks a lot, works like a charm, now everytime a new Spotify song starts, I start a new screensaver :wink:

I only hard coded the uri of the flowcard since I also use different external screensavers.

function setScreenSaver(opt) {

Homey.flow.runFlowCardAction({

id: 'set_screensaver',

uri:  'homey:manager:ledring',

args: { screensaver: { uri: opt.uri, id: opt.id } },

delay: null,

});

}

2 Likes