Copy of script not working anymore

I have a simple script to reset some variables.
Since shortly I get the message “Classic”, meaning I seem to use an old API library or something
When I copy and past the script to a script with a new name, but same content, it does not work anymore. I can’t figure out what’s wrong. Debugging results in that it suddenly cannot find await Homey.logic.getVariables anymore by its name, it returns “undefined”. In the original version the exact same code returns the correct values. So apparently I need to use convert my code, but no idea, read the tutorial, but could not find the solution.

My simple code

setparvalue(“testvar”,“test”);
setparvalue(“testvar1”,“test”);
setparvalue(“testvar2”,“test”);

//

async function setparvalue(varname, varvalue)
{
const advancedvar = await Homey.logic.getVariables(({filter: { name: varname }}));
const parval=Object.values(advancedvar)[0].value
const parvalid=Object.values(advancedvar)[0].id

log(parval,parvalid)
await Homey.logic.updateVariable({id: parvalid, variable: {value: varvalue}})
}

I am not a Homey programmer, but maybe this helps: HomeyAPI - Homey Web API

Could be, but I usually like some coding examples. I often use the examplexys.js that are in my homey scripting evironment. But also these examples all have a orange flag, probably because of being classic. So a bunch of new examples in the scripting environement would also be helpful.
This tutorial does not help very much either. Tutorial: Migrating deprecated scripts - HomeyScript

I got a different error


setparvalue("testvar","test");
setparvalue("testvar1","test");
setparvalue("testvar2","test");

//

async function setparvalue(varname, varvalue)
{
const advancedvar = await Homey.logic.getVariables(({filter: { name: varname }}));
const parval=Object.values(advancedvar)[0].value
const parvalid=Object.values(advancedvar)[0].id

log(parval,parvalid)
await Homey.logic.updateVariable({id: parvalid, variable: {value: varvalue}})
}


"root":{1 item
"error":string"Can't find variable: log"
}

You may try use " instead of “

So if you run this code:

console.log(Homey.logic.getVariables);

It logs undefined?

The code + result in the existing old code


Pasted this in a new script, and it does not work anymore, does not find the correct variable, 3 time the same id. which is not correct. In the old code the correct id’s are found.

To answer Robert’s Q:

When ran as edited ‘classic’ script, other result:

I’m not a developer, so apologies if this is besides the point, but aren’t you missing semicolons at the end of lines 13, 14, 15 and 18?

No.

If that (or incorrect quotes, as was suggested earlier) was of relevance, the script would stop with a syntax error, and it doesn’t.

“Script Success” :man_shrugging:t3:

It says “Returned: undefined” because the script doesn’t return anything.

It’s unclear to me what the actual problem is here.

The problem to me is that I have an old script that I use as an example for new scripts, but in the new script exactly the same code does not work. The old script is since shortly indicated as “classic” , with the suggestion to migrate it to something else. Also this is left to the programmer to find out for him/herself, because to me the manual offered is abacadabra, I need examples. It would be helpful if there was a kind of indication what pieces of code need attention, or even an upgrade service, should be possible in these days of AI.

Anyway, I tried some different code I found in a post, and that works, but I have a bunch of scripts with the “classic” indication so I would rather not spent a day per script migrating it to the new HomeyScript version. I will post a message to the Homey support team.

I know the frustation on finding updated examples etc… been there myself… this script example will do what you want i belive, there is a slight difference from your orginal code:

setparvalue("testvar","test");
setparvalue("testvar1","test");
setparvalue("testvar2","test");

//

async function setparvalue(varname, varvalue) {
  const advancedvar = await Homey.logic.getVariables({ filter: { name: varname } });
  
  // Find the variable object by its name
  const variable = Object.values(advancedvar).find(v => v.name === varname);

  if (!variable) {
    throw new Error(`Variable ${varname} not found.`);
  }

  const parval = variable.value;
  const parvalid = variable.id;

  log(parval, parvalid);
  
  await Homey.logic.updateVariable({ id: parvalid, variable: { value: varvalue } });
}

tested and working with changing the variables.
i claim no expertise in coding, this was done with GPT O1

and it seem log wil return undefined no matter… might be a Homey thing…

Thanks, I also tried AI through Gemini, but that came up with non working code, so you are better at AI prompting than me.

1 Like

Well, i should try be better reading full posts before posting myself… Well done on finding the solution yourself :slight_smile:

@TheKos I see more changes in your code. So I am curious to know if you have any idea what has changed? Is it the library or the script language version? Or are you merely following examples?