Homey Community Forum

[Solved] Help setting correct type in tags in HomeyScript

I’ve made a script that calculates the dim level depending on the luminance of a Hue sensor. The values I’m sending in as arguments are the luminance, max level of luminance and the max level of the dim level, ie "214.4,50,0.7".

const tokens = args[0].split(',');
const lumOrg = parseFloat(tokens[0]);
const maxLuminance = parseInt(tokens[1], 10);
const maxLight = parseFloat(tokens[2]);

const lumPercent = (lumOrg > maxLuminance) ? 1 : lumOrg / maxLuminance;
const lumPercentRev = 1-lumPercent;
const lumLightValue = Math.round(lumPercent * maxLight, 1);
let lumLightValueRev = lumPercentRev * maxLight;

lumLightValueRev = lumLightValueRev < 0 ? 0 : Math.round(lumLightValueRev * 10) / 10;

await setTagValue('lumvalue', {type: 'number', title: 'lumVal'}, lumLightValue);
await setTagValue('lumvaluereverse', {type: 'number', title: 'lumValReverse'}, lumLightValueRev);

return true;

The thing is that if I set a string (ie await setTagValue('lumvalue', {type: 'string', title: 'lumVal'}, '' + lumLightValue);) it works fine, but when trying to set a number I get an error:

Flow Card Error: invalid_type

Can anyone please send me into the right direction here and tell me what I should do to set the tag value to the right variable type? :smiley:

This happens when you have previously created the tag as a string. From that point on (up to, probably, a reboot), you can only set it as a string. If you change the tag name (lumvalueAsNumber) it should work.

1 Like

That’s it! Thanks @robertklep!

Just chiming in to say thanks for that. I’m new to Homey and I had used the same tag name in two different homeyscripts, and couldn’t for the life of me figure out why I couldn’t get the correct value. Then I stumbled upon your answer, and voila!

Kinda counterintuitive that a tag is stuck with the very first type it gets, to be honest.