This is the forum archive of Homey. For more information about Homey, visit the Official Homey website.

The Homey Community has been moved to https://community.athom.com.

This forum is now read-only for archive purposes.
Official HomeyScript

HomeyScript - share your scripts - main discussion topic

edited April 2018 in Apps

HomeyScript

Write scripts for Homey, for when the Flow Editor might not be enough.

App store link: HomeyScript App 

Wiki: HomeyScript Wiki (additional examples and information can be added here)

Visit https://homeyscript.athom.com to manage your scripts.

Features

  • Write a script in JavaScript and have direct access to Homey's Web API (bèta)
  • Run a script as a Flow 'and' or 'then' card

Documentation

In HomeyScript, you can access a few global objects:

  • Homey - A HomeyAPI instance. https://developer.athom.com/
  • _ - Lodash. https://lodash.com/
  • fetch - The Fetch API. https://developer.mozilla.org/
  • console - V8 Logging API's.
  • log(...args) - A shortcut function to append to the output log.
  • say(text) - A shortcut function to make Homey speak.
  • __filename__ - The script filename.
  • __script_id__ - The script ID.
  • args - An (optional) Array of script args.

API

It is possible to trigger a script remotely using the Homey Web API.

let HomeyScript = await Homey.apps.getApp('com.athom.homeyscript');
HomeyScript.ApiPost('script/<ScriptID>/run', [arg1, arg2]);


«134

Comments

  • Homey just launch the HomeyScript app to the app store:
    Let's share the scripts you are using in this topic!
  • Thats AWSOME - was dreaming about this yesterday and it came true!
  • exiof said:
    Thats AWSOME - was dreaming about this yesterday and it came true!
    Looking back @JeroenVollenbrock's comment on Slack showed some inside information  ;)
    But you asked for it, dreamt about it, got it..... now time to share the scripts you will dream about this night  :D
  • isnt there an hello world example from athom  ?

  • exiof said:
    Thats AWSOME - was dreaming about this yesterday and it came true!
    Looking back @JeroenVollenbrock's comment on Slack showed some inside information  ;)
    But you asked for it, dreamt about it, got it..... now time to share the scripts you will dream about this night  :D
    LOL :-) my girl hates me if she enters this forum :-|
  • this


    let devices = await Homey.devices.getDevices();
    return Object.values(devices).some(device => {
    console.log(device.name);
    });

    gives this


    Homey2 Homey2 AMST-606 AMST-606 X10OnOffH2 X10OnOffH2 X10OnOffH1 X10OnOffH1 X10MS13EK6 X10MS13EK6 X10MS13EA1 X10MS13EA1 X10DimH3 X10DimH3 X10MS13EA3 X10MS13EA3 Denon AVR Denon AVR 5d6081 5d6081 HMS100 HMS100 X10MS13EC2 X10MS13EC2 X10MS13EA5 X10MS13EA5 HMS100 2 HMS100 2 HMS100 4 HMS100 4 HMS100 5 HMS100 5 HMS100 3 HMS100 3 rfx5d6018 rfx5d6018 Denon AVR Denon AVR 1d20bc 1d20bc Samsung Remote Samsung Remote 2de stekkerdoos computer 2de stekkerdoos computer homey haamstede homey haamstede lamp ther haamstede lamp ther haamstede led voor garage haamstede led voor garage haamstede CV-ketel haamstede CV-ketel haamstede frituur haamstede frituur haamstede led zijkantschuur led zijkantschuur versterker schuur versterker schuur afwasmachine haamsdtede afwasmachine haamsdtede magnetron haamstede magnetron haamstede inx10 h1 haamstede inx10 h1 haamstede oven haamstede oven haamstede nachtlamp haamstede nachtlamp haamstede thermostaat haamstede thermostaat haamstede boombox boombox muziekschijf muziekschijf Setop box haamstede Setop box haamstede Televisie alles haamstede Televisie alles haamstede Televisie zelf haamstede Televisie zelf haamstede versterker haamstede versterker haamstede Hue Lamp Hue Lamp Hue Lamp 1 Hue Lamp 1 Hue Lamp 2 Hue Lamp 2 nacht boven nacht boven nacht midden nacht midden bolletjes bolletjes slaapkamer jilles slaapkamer jilles nacht onder nacht onder Inez Inez halletje halletje keuken keuken led kast led kast LivingWhites Plug 3 LivingWhites Plug 3 LivingWhites Plug 4 LivingWhites Plug 4 LivingWhites Plug 5 LivingWhites Plug 5 rene rene X10OnOffA1 X10OnOffA1 2Lan0536ae 2Lan0536ae New Device New Device ---------------- Script returned: false ---------------- Script returned: false

    all devices are double and where comes the false from 
  • Homey.speechOutput.say({data:{text:"I am Homey, and this is written in HomeyScript! I am grown up now "}});
  • @NattelipWorks for me:
    let devices = await Homey.devices.getDevices();
    Object.values(devices).forEach(device => {
    console.log(device.name);
    });

    Returns:
    Homey
    IT03717
    AMST-606
    bureaulamp
    AYCT-102
    tv
    blueray
    radio
    muurlamp
    WeMo Switch
    Energie
    Philips TV
    basthuis
    var3
    Waterkoker
    wac3500
    PW Stretch broken
    diepvries
    TV huiskamer
    D35D17
    Wasmachine
    NP2900
    Homey
    Philips NP2900
    Philips TV
    Nachtkastje beneden
    boven voor kamer
    MCI500H
    WAC3500
    koelkast en magnetron
    Computers
    WAC3500
    mci500 stereo
    Serre
    badkamerkachel
    Chromecast3548
    Alle lampen
    Beweging1
    Virtual holiday
    Virtual sleep
    Away
    KPN (Motorola) SetTopBox
    plafond bureaulamp
    plafond keuken
    lamp boekenkast
    Dimmer
    Virtual Switch
    
    ----------------
    Script returned:
     undefined
  • Nattelip said:
    this


    let devices = await Homey.devices.getDevices();
    return Object.values(devices).some(device => {
    console.log(device.name);
    });

    gives this


    .......  ---------------- Script returned: false ---------------- Script returned: false

    all devices are double and where comes the false from 
    There was a bug that could cause log lines to show double. It has been resolved now. The script returns false because Array.some returns false if none of the invocations of the callback returned true. You might be looking for the forEach function instead. HomeyScript also includes Lodash, so the following code might actually be easier to understand:


    let devices = await Homey.devices.getDevices();
    _.forEach(devices, device => {
        console.log(device.name);
    });
    
  • MatjaLipuMatjaLipu Member
    edited September 2017
    Here is one for a Friday night :) Nice job guys!
    try {
    const user = await Homey.users.getUserMe()
    const response = await fetch(`http://api.icndb.com/jokes/random?limitTo=[nerdy]&firstName=${encodeURIComponent(user.name)}&lastName=`)
    const joke = await response.json()
    say(joke.value.joke)
    return true
    } catch (e) {
    return false
    }
  • LurendrejerLurendrejer Member
    edited September 2017
    Hi.
    completely new to javascript, hoping someone will help, how would i go about doing this? In a working fashion.
    if(device.name contains '(Light)') return false;
  • device.class == "light" could work, depending on what you want to achieve :)
  • I want to achieve not using device classes :)

    Until i can change device classes i name my devices (L) for lights, (H) for heat and so forth. 
  • I even have devices with hardcoded device classes in the app, that i'm using for heat and garage controls.
  • LurendrejerLurendrejer Member
    edited September 2017
    The optimal solution would be, if the device is in a zone called *(L) then return false. But i'm starting slow :)
  • swtttswttt Member
    edited September 2017
    if(device.name.substr(device.name.length - 3) === "(L)" ) return false;

    Should do the trick then :)
  • Thank you :)
  • Neato, thanks.
    My head exploded the last time I tried to finish my script, so I'll probably be saving it for a day without too much else going on :)
  • If Homey2 runs a Windows nano server and has PowerShell cmdlets, i might have a chance :-D
  • Just ... meh.. why would this:
    let devices = await Homey.devices.getDevices();

    return Object.values(devices).some(device => {
    if(device.name.substr(device.name.length - 3) === '(L)' ) return false;
    console.log(device.name);
    return true;
    });

    Return:
    Homey
    Homey
    True
    True 

    ?!
  • If Homey2 runs a Windows nano server and has PowerShell cmdlets, i might have a chance :-D
    LOL Exact dame issue here - well could run PoSH on Mono :-)
  • Hehe.... Goddamn-tree-huggin-hippie-linux-homey-baaah... :-D
  • Could we please just get wildcard support in the devicecards? :smiley:
  • home78home78 Member
    edited September 2017
    I've written a small script to retrieve all devices in a zone and all devices in zones contained in that zone:

    let zone = 'Boven';
    
    let zones = await Homey.zones.getZones();
    let devices = await Homey.devices.getDevices();
    log('Devices in zone', zone);
    log('='.repeat(60));
    function deviceInZone(deviceZone, matchZone) {
    if (deviceZone.name == matchZone) {
    return true;
    }
    else if (deviceZone.parent) {
    return deviceInZone(zones[deviceZone.parent], matchZone);
    }
    }
    Object.values(devices).forEach(device => {
    if(deviceInZone(device.zone, zone)) {
    log(device.name, device.zone.name);
    }
    });
  • home78home78 Member
    edited September 2017
    To get a list of ZWave devices and their node number:

    let devices = await Homey.devices.getDevices();
    let count = 0;
    Object.values(devices).forEach(device => {
    if (device.driver.metadata && device.driver.metadata.zwave) {
    log(device.name, device.class, device.zone.name, "/ node:", device.settings.zw_node_id);
    count ++;
    }
    });
    return count + " devices";
  • Here's one that shows all flows in the correct folder-hierarchy:

    let folders = await Homey.flow.getFolders();
    let flows = await Homey.flow.getFlows();
    log('Flows');
    log('='.repeat(60));
    function showFolder(folder, depth) {
    log(' '.repeat(depth), folder.title, ':')
    Object.values(flows).forEach(flow => {
    if (flow.folder === folder.id) {
    log(' '.repeat(depth + 1), flow.title);
    }
    });
    Object.values(folders).forEach(subfolder => {
    if (subfolder.folder === folder.id) {
    showFolder(subfolder, depth + 1);
    }
    });
    }
    Object.values(folders).forEach(folder => {
    if (!folder.folder) {
    showFolder(folder, 0);
    }
    });

  • home78home78 Member
    edited September 2017
    To get a list of all devices with low battery (can be called from a flow both from the condition or from the then column):

    let count = 0;
    let devices = await Homey.devices.getDevices();
    
    Object.values(devices).forEach(device => {
       if (device.state.measure_battery != null && device.state.measure_battery < 30) {
         console.log(device.name, 'op/in', device.zone.name, 'battery', device.state.measure_battery ? device.state.measure_battery + '%' : '?');
         count ++;
       }
    });
    
    if (count == 1) {
       say("Er is 1 apparaat waarvan de batterij bijna leeg is.");
    }
    else if (count > 1) {
       say("Er zijn " + count + ' apparaten waarvan de batterij bijna leeg is.');
    }
    
    return count > 0;
    
  • This is really cool.
    Is far from close to do any programming myself - but I will definitely keep an eye on what all you programmers publish here ;-)
Sign In or Register to comment.