Homey Community Forum

HomeyScript App

Here is a sniplet which logs battery status for all devices (works with Homey 2.x for me):

let devices = await Homey.devices.getDevices();

const MEASURE_BATTERY = 'measure_battery';
const ALARM_BATTERY = 'alarm_battery';

function logBatteryLevels() {
    _.forEach(devices, device => {
        if (device.capabilitiesObj[MEASURE_BATTERY]) {
            console.log(`Device ${device.name} has battery level: ${device.capabilitiesObj[MEASURE_BATTERY].value}`);       
        } if (device.capabilitiesObj[ALARM_BATTERY]) {
            console.log(`Device ${device.name} has ${device.capabilitiesObj[ALARM_BATTERY].value ? 'battery alarm' : 'no battery alarm'}`);            
        }
    });
}
1 Like

thanks Volkov now I just need to understand it I got an error because of measure null and after put in a consol log I found that it was two unit that diden’t exist in reality it had a problem with.
question how can one found out the capoability of a device?
for examle as simple as temp on a xiaomi device.

You should have all the capabilities with their current values in device.capabilitiesObj. Try to log that to the console for your device and you will see the values.

A device that is not ready will not have a capabilitiesObj and can very well cause the error.

Now it feels a lot better a real jumpstart to get all capabilities. thanks for a fast response.
add my smsall program that list all devices capabilities
// list capabilityes
let devices = await Homey.devices.getDevices();

Object.values(devices).forEach(device => {
//write out name of device and all the capabilities
console.log(device.name, device.capabilitiesObj);
});

To use logic variabels you use code like this
let Logic = await Homey.logic.getVariables()
console.log(Logic);

updateVariable

await Homey.logic.updateVariable({id: yourid, variable: yourVar})

You have access to the API https://developer.athom.com/docs/api
so Homey.whatyouwannareach gives alot of options. send me a question if you need more help.

1 Like

How do I get the id if I only know the name of the variable?

Use get function and console log it. That will give you the I’d

let devices = await Homey.devices.getDevices();
_.forEach(devices, device => {
console.log(device.name, ’ ',device.id);
});

This is for devices. Need this for logic variables.

@gagga Sorry, I did not read well, but how about this:

Did you try this?

Or go to: https://developer.athom.com/tools/api-playground
and add a new line: Homey.logic.getVariables(); and press Run :blush:

1 Like

Hi.

Does the HomeyScript “Run a script with an argument” (in Flow) support passing multiple arguments or only one?

Thank you.

Have a look at the old forum, if I remember well, there was an example:

I can get the id this way.
I also need to get a bunch of variables by their NAME. Any idea how to get the variable Id from a call to getVariables();?

All I know is from reading and copy - past the examples on this forum and the old forum, search, combine, test and get wiser.

1 Like

Hi, any chance to explain or even better provide an example of how to use/modify this await Homey.logic.updateVariable({id: yourid, variable: yourVar})?

I have add the ID: something like dfb4ab9b-c711-4… but I do not know, how to modify the variable parametr…

If I add variable=“xy” I get An unkown error has occured [invalid_json]
If I try to add params inside the variable such name, value etc… I get You have no access to do this.

Thank you very much, M

Why will this script doens’t disable all my Apps as a test?

// Restart apps in case of crash
console.log(“Start”);

let apps = await Homey.apps.getApps();
crash = false;

_.forEach(apps, app => {

//For test
console.log(app.name);
console.log(app.id);
console.log(app.state);
Homey.apps.disableApp({id: app.id});
//End test

if(app.crashed == false) return;
console.log("App crashed, restart...");
//Homey.apps.restartApp(app.name);
crash = true;

});

console.log(“Done”);

return crash;

Here’s is some code that might help other diggers for data. especially the retrieving of tokens (which also contain Better Logic variables) which syntax is changed in the past (Homey 2.0?). It’s only outputting the names (To quickly see the contents), but if you want to see the complete object with all the data, uncomment the console log code:

// Log everything

console.log('-------------APPS----------');
let apps = await Homey.apps.getApps();
_.forEach(apps, app => {
       console.log(app.name);
    });
//console.log(apps);

console.log('-------------TOKENS----------');
let tokens = await Homey.flowToken.getFlowTokens();
_.forEach(tokens, token => {
       console.log(token.title+': '+token.value);
    });
//console.log(tokens);

console.log('-------------DEVICES----------');
let devices = await Homey.devices.getDevices();
_.forEach(devices, device => {
       console.log(device.name);
    });
//console.log(devices);

console.log('-------------FLOWS----------');
let flows = await Homey.flow.getFlows();
_.forEach(flows, flow => {
       console.log(flow.name);
    });
//console.log(flows);

console.log('-------------ZONES----------');
let zones = await Homey.zones.getZones();
_.forEach(zones, zone => {
       console.log(zone.name);
    });
//console.log(zones);  

console.log('-------------LOGIC----------');
let logics = await Homey.logic.getVariables()
_.forEach(logics, logic => {
       console.log(logic.name);
});
//console.log(logics);
2 Likes

Hello,

I cannot get the https://homeyscript.homey.app/ page to work. I says “Homey offline” while my Homey is definitely online! The page gives the following error in the console:
Failed to load resource: the server responded with a status of 401 ()

The sample scripts do not appear on the screen, but they can be selected in flows. However, the ones that are supposed to log to the console, don’t log to the console.

Am I missing something?

Log out and log back in again?