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

124»

Comments

  • @GeurtDijker
    Okay, understood this and you are right, the example code says that Homey should fetch all devices and do something _.forEach.....

    Well, now what to do if I just want to "talk" to a single device? What do I have to do, if I want Home to ask a single device an what to do if I want an single device to do something? Is there a possibility instead of "forEach"? I know there is a support site but I do not understand what to do.

    Right now the example script looks like this:

    let devices = await Homey.devices.getDevices();
    _.forEach(devices, device => {
    if(device.class != 'light') return;

    - Homey get all devices
    - for each device found Homey is doing something
    - But skip each device that is not like "light"

    Would this be a right way tor talk to a single device?:
    if(device.name != 'EG WZ-HWA_B.102-Leselampe') return;

    And if this is the right way, why does this shut off all devices?
    let devices = await Homey.devices.getDevices();
    _.forEach(devices, device => {
    if(device.name != 'EG WZ-HWA_B.102-Leselampe') return;
    console.log("NAME: ", device.name, " - Zone: ", device.zone.name);
    device.setCapabilityValue('onoff', !device.state.onoff);
    });

    Regards
    Andreas

  • LammyLammy Member
    edited July 2018
    @Andreas, you can find the id of a specific device with the following code:
    let devices = await Homey.devices.getDevices();
       _.forEach(devices, device => {
          if (device.name == 'EG WZ-HWA_B.102-Leselampe)
             console.log(device.id);
    });
    You only have to run this once, just to get the id of your Leselampe.
    In your final script, just use:
    let leselampe = await Homey.devices.getDevice({id: '7ed5a811-7e24-43d9-ad25-7073c728bed0'});
    Replace the id with the one you found with the previous script.
    Your code gets much faster, because not all devices are retrieved, just the one you really need.

    You can now do something like:
    leselampe.setCapabilityValue('onoff', false);

    And if this is the right way, why does this shut off all devices?
    It doesn't really. It shuts off all devices except your Leselampe (assuming that the device name is a match).
    The innermost curly brackets { } form a function body that gets called by _.forEach() for every device. If that function returns false, _.forEach() stops, anything else and it will continue with the next device. 

    Search for "lodash" on the net for more details. There are many other powerful functions. 
    Have fun!
  • @Lammy
    Thank you very much for your help, this is great for my start of learning.
  • Hello community, 

    now that a learned something new from @Lammy the next question comes up.

    Has someone ever tried to trigger the app "MQTT Client" (from Menno van Grinsven)? I know the ID and I am able to start a script based on the things Lammy told me in the postings before but now...how to tell the app to do something?

    Regards
    Andreas
  • Information about how to use tags from scripts by Julius van Dis from Slack:
    (Response on question from roel_hendr)

    I've been playing around a little bit with this issue, and came to the following conclusion:

    When you use a card from a specific group as the 'trigger' card, you can only use the variables which are bound to that group. 

    What this means: 
    • you set a tag from Homeyscript. This is then bound to the homeyscript namespace within the tags. You can see this when you look up the tags in the tags menu (when creating flows).
    • you use a card from the 'logic' group to actually detect if there are changes. I more or less found out that the logic group can only trigger to changes that occur with tags that are registered in the 'logic' namespace. I played around with this and checked to see if I could detect changes in my dim settings for a couple of Hue lights with the logic card. I could not... Same was true for tags set by HomeyScripts. When I however change it a 'logic' variable, then changing that variable actually triggers the flow I want.

    Now for the solution: a quick and dirty one could be: 
    1. Leave your trigger for running the homey_script as iis.
    2. Move the execution of the homey_script to the ...AND... clause. If the script successfully updated the tag, return true. Otherwise, return false (stop). If you have conditions here already, make sure that you put the homey_script at the last position (the are executed in order). For now I assume you don't use the ...OR... clause
    3. Trigger the flow you want executed when the tag changes. For this you need to update that flow as well (change the trigger from 'a variable has changed' to 'this flow has been started' from the flow group.

    The issue with not being able to read tags from other groups / namespaces, is only an issues in the 'trigger' (...when..) column.
  • thekingjuniorthekingjunior Unconfirmed, Member
    Anyone has an example to target a grouped device (3 ikea spots), set the brightness to 0, and then increase it by 0,1 over an X amount of time?

    Something like this:

    //JS
    var valueElement = document.getElementById('value');

    var start     = 0;
    var end       = 1;
    var duration  = 10000; // In milliseconds (divide by 1000 to get seconds).
    var framerate = 2000;    // In milliseconds (divide by 1000 to get seconds).

    var toAdd = ( ( end - start ) * framerate ) / duration;

    var interval = setInterval(function() {
        var currentValue = parseFloat(valueElement.innerHTML);
     
      if (currentValue >= end) {
          clearInterval(interval);
        return;
      }
     
        valueElement.innerHTML = (!isNaN(currentValue) == true ? currentValue + toAdd : toAdd);
    }, framerate);

    //HTML
    <span id="value">0</span>

    Thank you!


Sign In or Register to comment.