Activate Hue scene with HomeyScript?

I want to control my lighting with motion sensors.
When active => Bright light hue scene
When not active => Back to previous hue scene

The idea was to store the current scene in a variable when activating it with Homey. And restore the scene when there’s no motion detected. Sadly you can’t put in a variable (or “Tag”) inside the “Activate scene” card.
afbeelding
I don’t like to create separate flows for every situation, so I thought I should do this with HomeyScript. Maybe I can inject a variable in there. Does anyone know if this possible? Maybe point me in the right direction? Are there comparable sample codes?

ps. I know it’s possible using hue Labs. But I want it to control with Homey because of extra rules I like to implement.

1 Like

Let me know it there is an solution for this.
I would hope there were an ability to set Scene from a logic text, but it doesn’t work.

You can run cards from HomeyScript so you can also run the scene card. You would just have to find the flow card uri and id and the argument name to place in args. There is an example script that logs all cards so you would have to find it there.

/*
 * This script demonstrates how to run an 'And' Flow Card,
 * and how to run an 'Then' Flow Card.
 */

// Run 'Day Equals' Flow 'And' Card
const { result: isWeekend } = await Homey.flow.runFlowCardCondition({
  uri: 'homey:manager:cron',
  id: 'day_equals',
  args: {
    day: 'weekend',
  },
});

log('Is Weekend:', isWeekend);

// Run 'Show Animation' Flow 'Then' Card
// This will pulse Green (#00FF00) when it's a weekend
// or pulse Red (#FF0000) when it's a weekday
await Homey.flow.runFlowCardAction({
  uri: 'homey:manager:ledring',
  id: 'show_animation',
  args: {
    animation: 'pulse',
    color: isWeekend
      ? '#00FF00'
      : '#FF0000',
  },
});

bump :slight_smile:

Can someone who has done it, point to what I do wrong in my script. I found the following code for Philips hue flow card Set scene:

-----------
Activate the [[scene]] scene
{
  "uri": "homey:app:nl.philips.hue",
  "id": "setScene",
  "args": [
    {
      "name": "scene",
      "type": "autocomplete",
      "title": "Scene"
    }
  ]
}
-----------

And by following the example flow, also shown in the post above, I’m writing it as:

await Homey.flow.runFlowCardAction({
     uri: 'homey:app:nl.philips.hue',
     id: 'setScene',
     args: [
                 {
                      name: 'Energize Kitchen',
                      type: 'autocomplete',
                      title: 'Energize Kitchen'
                  }
               ]
})

And I get an error:

———————————————————
❌ Script Error
⚠️ Cannot read property 'bridge_id' of undefined: Cannot read property 'bridge_id' of undefined
    at /node_modules/athom-api/dist/index.js:1:1186403
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

Am I supposed to reference my particular Hue bridge in some way?

Any help is greatly appreciated!

I am trying to do similar to the above (store a scene name in a variable, activate another scene, and then return to the saved scene name some time later), but am currently failing to make any scene changes through HomeyScript.

I can set scene from a normal (non-script) Flow. I can set individual lights on or off through HomeyScript and Philips Hue app (official version using Hue bridge). I can list the available scenes by interrogating the FlowCards available through the Philips Hue app and HomeyScript API.

I can call the runFlowCardAction with what I believe should be the right information to setScene, and it doesn’t return any errors - but it doesn’t cause any change in the light setting.

After failing with setScene, I went back and tried groupOn/groupOff, but they didn’t work for me either (no error, but no action). Again, controlling individual lights works fine (HomeyScript + Philips Hue official app), but not these wider hub controls (scenes & groups).

Is anyone able to help spot what I am (presumably) doing wrong? I haven’t found any discussion in the forum, and I assume that I can’t be the first to try this with the Homey Pro (early 2023), so I’d guess other people must have this working?

(I have also seen mention of the Zone Memory app, which sounds potentially interesting to save & restore settings, but I would like to get this scenes & group control working anyway)

HomeyScript trial:

// HomeyScript using Philips Hue official app from Athom B.V
// Developed using versions:
// - Philips Hue app v5.9.1
// - Homey Web App version 1.9.57
// - Homey Pro (Early 2023), version 10.0.0-rc.122

// Status as of 8-July-2023:
// Not setting scene; everything before that works, but not the setScene
// runFlowActionCard doesn't return any errors, but the lights don't change state
// (can set scene via non-script flow, and even call that from HomeyScript, but not locally in script)
// why??

const flowcard_uri = 'homey:manager:flow';
const owner_name = 'Philips Hue';
const card_id = 'homey:app:nl.philips.hue:setScene';

// edit to match an available Hue scene, or empty string to list all available scenes
const scene_name = 'Front garden evening';
// const scene_name = '';

log('Philips Hue: Flow Card Actions:')
const actionCards = await Homey.flow.getFlowCardActions();
for( const card of actionCards ) {
  if( card.ownerName == owner_name ) {
    log(" ", card.id)
   }
}

for( const card of actionCards ) {
  if (card.id == card_id) {
    const options = await Homey.flow.getFlowCardAutocomplete({
      uri: flowcard_uri,
      id: card.id,
      name: 'scene',
      query: '',
      type: 'flowcardaction'
    })
    log('\n');
    log('Card properties for:', card_id);
    log(card)
    log('\n');
    log('Philips Hue: Scenes:');
    for( scene of options ) {
      if( !(scene_name.length) || (scene.name == scene_name) ) {
        log(scene.description, " : ", scene.name, "; scene id=", scene.id);
      }
    }
   }
}

log('\n');
if(!scene_name.length) {
  log('No scene name specified so skipping setScene step')
} else {
  log("runFlowCardAction: flowcard uri=", flowcard_uri, "; card id=", card_id, "; name=", `'${scene_name}'`);
  await Homey.flow.runFlowCardAction({
    uri: flowcard_uri,
    id: card_id,
    args: {
            name: `'${scene_name}'`,
            // type: 'autocomplete',
          }
  })
  .then(() => log('Scene set OK'))
  .catch(error => log(`Scene set Error:`, error));
}

Re-targeted my question over here (with an Apps tag), and got an answer that resolved my misunderstandings and got this working for me: