My first homeyscript. All feedback appreciated

Hi all,

This is my first Homeyscript. I’m trying to code as much as possible since I don’t want to end up with massive amounts of flows.
Very new to Javascript so if you have any feedback on bettering the code it will be appreciated.
ID’s have been replaced by ‘xxx’

  • Note: found some bugs in the code and replaced them.
// Light Engine
// Finds light 
// Parameter 1: Light Name
// Parameter 2: On or Off

let lightArray = [
    { id: 0, name: 'Study', lightid: "Study Lights", lightid: "5bc823d1-9e7a-460c-9942-b35b8bc046b8", luminanceid: "7c7bdacc-596f-415e-b514-ebc82d945f4b", setluminance: true },
    { id: 1, name: 'Hall Entrance', light: "Hall Entrance", lightid: "5bc823d1-9e7a-460c-9942-b35b8bc046b8", luminanceid: "7c7bdacc-596f-415e-b514-ebc82d945f4b", setluminance: true },
];

let params = args[0].split(',');
let light = params[0];                      // Set the Device Name
let onoff = params[1] == 'On'               // Turn lights on or off
// TODO Check if params are correct


// Finds objects in array. This can done by querying all devices, but this is quicker (I think)
function findObjectByKey(array, key, value) {
    for (var i = 0; i < array.length; i++) {
        if (array[i][key] === value) {
            return array[i];
        }
    }
    return null;
}

// Find device
let obj = findObjectByKey(lightArray, 'name', "Study");

// Get current Luminance 
let luminance = await Homey.devices.getDevice({id: obj.luminanceid});
// Would be nice if luminance extracts value from object. No idea how to do this

// Luminance table. 
if (luminance.state.measure_luminance > 200 ) {
    set_luminance = 0.2;
} else { set_luminance = 0.8 }
// TODO, more options for Living Room (else if)

let device = await Homey.devices.getDevice({id: obj.lightid});
// console.log (set_luminance)
if (!onoff) { // Turn off light 
    await device.setCapabilityValue('onoff', false);
    console.log('onoff false')
} else {
// Check required state with current state. If not the same set correctly
    if (device.state.dim != set_luminance) { await device.setCapabilityValue('dim', set_luminance) }
    await device.setCapabilityValue('onoff', true)
}
1 Like

Hi,
I don´t manage to code in Javascript all I do gets wrong, but I tried to read your script since you didn´t explained what you´re script is doing and if I understood it correctly you use it to toggle a specific set of lights and set liminance, correct?

I was trying to code a script to turn of a whole bunch of lights both over ZigBee and 433Mhz but failed. Javascripts example on the web doesn´t work on HomeyScript when I just copy paste it to the workbench.

Great job!

Hi Markus,

I posted my first, very inelegantly coded script to help others and you seem to struggle with the same things I am struggling with.

Can you share your script? Maybe I can help you figure out what’s going on.

Hey Arnizzle, you can format you code by placing it in with in ``` block.

I’m not a JS developer but perhaps one can answer, if you are not doing something with the the response of an async request ie. setCapbailityValue('onoff', true); do you need to await the response before moving on?

Jamie,

Thanks for the feedback. This is my very first Javascript and I’m fully aware that the code looks really bad and probably doesn’t follow the rules :slight_smile:

The reason I put this on here is because there’s not much going on on this forum. Starting Homey users need some kind of example (even if it’s bad) to play around with code. This script took me a long time to create simply because lack of information on both the site as the forum. Most of the info I got is from the old forum (https://forum.athom.com). I haven’t found a good resource anywhere for Javascript / Homey resources.

Cheers

I think its great and wish it was around when I first had a look at Homey Script. Keep them coming.

Of course. My script is at the moment just the specified ID:s for som lights and then code to turn them of.
What I would like to do is to check if a lamp is on and an if it is then turn it of and verify that then loop it through all lamps added in a array.
But since I never coded in Javascript before this was to hard for me to accomplish.

// Turn off a bunch of lights
// Specify ID for a specific lamp
let KitchentableRight = await Homey.devices.getDevice({id: 'ef6bae9c-af02-af02-af02-905f90d16589'});
let KitchentableMiddle = await Homey.devices.getDevice({id: 'e29ab963-af02-af02-af02-ec25c103a62a'});
let KitchentableLeft = await Homey.devices.getDevice({id: '8b102bc6-af02-af02-af02-f67a53ffd9d9'});
let HerLamp = await Homey.devices.getDevice({id: '69c93c74-af02-af02-af02-32ec0cafb070'});
let HisLamp = await Homey.devices.getDevice({id: '1032c9cc-af02-af02-af02-ca4c8c4f76d3'});
let LivingRoomLights = await Homey.devices.getDevice({id: 'bd02260e-af02-af02-af02-686ba138f6e0'}); 
let LightstripHall = await Homey.devices.getDevice({id: 'fe5d4348-af02-af02-af02-d03d772cadfc'});
let Hall1 = await Homey.devices.getDevice({id: '6f2fd3e1-af02-af02-af02-ea8a73ed45f4'});
let Hall2 = await Homey.devices.getDevice({id: '654f5edb-af02-af02-af02-53da40285b2c'});
let Hall3 = await Homey.devices.getDevice({id: '186ce13f-af02-af02-af02-c5d4193632fa'});
let SpotsMasterBedroom = await Homey.devices.getDevice({id: 'eb45bc22-af02-af02-af02-02c662ec498f'});
let LivingroomWindowLights = await Homey.devices.getDevice({id: 'c4cb9e0e-af02-af02-af02-8ea5fec5fac1'});
let SecondFloorHall = await Homey.devices.getDevice({id: '37a87965-af02-af02-af02-bf30eccc5f7e'});

// Turn off all lights based on their ID:s
// Choose true to turn on all lights and false to turn off them 
var Statement = false

KitchentableRight.setCapabilityValue('onoff', Statement);
KitchentableMiddle.setCapabilityValue('onoff', Statement);
KitchentableLeft.setCapabilityValue('onoff', Statement);
HerLamp.setCapabilityValue('onoff', Statement);
HisLamp.setCapabilityValue('onoff', Statement);
LivingRoomLights.setCapabilityValue('onoff', Statement);
LightstripHall.setCapabilityValue('onoff', Statement);
Hall1.setCapabilityValue('onoff', Statement);
Hall2.setCapabilityValue('onoff', Statement);
Hall3.setCapabilityValue('onoff', Statement);
SpotsMasterBedroom.setCapabilityValue('onoff', Statement);
LivingroomWindowLights.setCapabilityValue('onoff', Statement);
SecondFloorHall.setCapabilityValue('onoff', Statement);

return true
2 Likes

New question in this old topic:
can someone tell/show me how to retrieve the “week-number” in Homey-script?

Edit: Never mind, got the answer via Slack/HomeyScript.