HomeyScript issue

I’m having problems with flow tags when created with HomeyScript.

currently I have 2 homeyScripts that both work fine when running from tests.
From 1 script the output flow tags are available for selection.
From script 2 the output flow tag does not show up.

The syntax for setting the tag is the same in each script.

Diagnostic report:
9fe37241-b3e0-4b41-beee-de16804596fe

Can you share the error message?
What flow tag do
Can you share the script, please?

Here is the entire script:

// Helper function to convert "mm:ss.SSS" to total seconds as a float
function parseTimeToSeconds(timeString) {
    if (!timeString) return null;
    const [minutes, seconds] = timeString.split(':');
    return parseInt(minutes) * 60 + parseFloat(seconds);
}

const apiuri = 'https://ergast.com/api/f1/current/next/qualifying.json'
const response = await fetch('https://ergast.com/api/f1/current/24/qualifying.json');
const data = await response.json();

// Check if the qualifying data exists
if (!data.MRData || !data.MRData.RaceTable || !data.MRData.RaceTable.Races[0]) {
    console.log('No qualifying data available.');
    return;
}

const qualifyingResults = data.MRData.RaceTable.Races[0].QualifyingResults;

if (!qualifyingResults || qualifyingResults.length === 0) {
     console.log('No qualifying results found.');
     return;
}

// Extract times from Q1, Q2, and Q3 for the first position
const firstPositionTimes = qualifyingResults[0];
const firstQ1Time = parseTimeToSeconds(firstPositionTimes.Q1);
const firstQ2Time = firstPositionTimes.Q2 ? parseTimeToSeconds(firstPositionTimes.Q2) : null;
const firstQ3Time = firstPositionTimes.Q3 ? parseTimeToSeconds(firstPositionTimes.Q3) : null;
const maxNameLength = Math.max(...qualifyingResults.map(q => q.Driver.familyName.length + q.Driver.givenNamelength));
//console.log(maxNameLength);

// Generate formatted output for each position
const outputLines = qualifyingResults.map(result => {
    const position = result.position.padEnd(2, ' ');
    const driverNumber = result.Driver.permanentNumber.padStart(2, ' ');
    const driverName = `${result.Driver.givenName} ${result.Driver.familyName} [${driverNumber}]`.padEnd(maxNameLength + 6,' ');

    // Parse Q1, Q2, Q3 times
    const q1Time = parseTimeToSeconds(result.Q1);
    const q2Time = result.Q2 ? parseTimeToSeconds(result.Q2) : null;
    const q3Time = result.Q3 ? parseTimeToSeconds(result.Q3) : null;

    // Calculate differences from P1 times
    const q1Difference = firstQ1Time !== null && q1Time !== null ? (q1Time - firstQ3Time).toFixed(3) : 999;
    const q2Difference = firstQ2Time !== null && q2Time !== null ? (q2Time - firstQ3Time).toFixed(3) : 999;
    const q3Difference = firstQ3Time !== null && q3Time !== null ? (q3Time - firstQ3Time).toFixed(3) : 999;
    const qFastestDifference = Math.min(q1Difference,q2Difference,q3Difference).toFixed(3);

    // Return formatted string
    return `P${position} : ${driverName} + ${qFastestDifference}s`;
});

let outputValue = String(outputLines.join('\n'));
//console.log(outputValue);
await tag('QualifyingResults', outputValue);

return outputValue;

There is no error message. The flow tag ‘QualifyingResults’ is simply not created / available for selection.

It gets created here just fine under the “HomeyScript” tags (you are probably just looking at the wrong spot for your variable):

P1 : Lando Norris [ 4] + 0.000s
P2 : Oscar Piastri [81] + 0.209s
P3 : Carlos Sainz [55] + 0.229s
P4 : Nico Hülkenberg [27] + 0.291s
P5 : Max Verstappen [33] + 0.350s
P6 : Pierre Gasly [10] + 0.389s
P7 : George Russell [63] + 0.537s
P8 : Fernando Alonso [14] + 0.601s
P9 : Valtteri Bottas [77] + 0.609s
P10 : Sergio Pérez [11] + 0.669s
P11 : Yuki Tsunoda [22] + 0.824s
P12 : Liam Lawson [30] + 0.877s
P13 : Lance Stroll [18] + 1.134s
P14 : Charles Leclerc [16] + 0.707s
P15 : Kevin Magnussen [20] + 1.037s
P16 : Alexander Albon [23] + 1.226s
P17 : Guanyu Zhou [24] + 1.285s
P18 : Lewis Hamilton [44] + 1.292s
P19 : Franco Colapinto [43] + 1.317s
P20 : Jack Doohan [61] + 1.510s

Unfortunately I am certain that I am looking at the correct spot, since variables from another script DID show up there. Only the one from this script (which works as you’ve demonstrated), does not show up.

When you didn’t already: The script has to run at least once before the tag is selectable from the flow.

I’ve ran it from test mode, from an advanced flow, and from a basic flow. All of them at least 10 times when trying to debug this issue :frowning:

The script works every time, but it never results in a flow tag becoming available.

It works:

Try emptying browser cache and force reload the my.homey.app page

Thanks for the tip, did not try that yet and seems like a plausible cause.

Just tried opening the site with ‘InPrivate’ mode, so no browser cache at all. Unfortunately, same result :frowning:

Also I tried completely rebooting the Homey. Also did not change the outcome.

Hmmm, that’s really odd.
I’m on Homey 2019, dunno if that’s related?

I’m on the early 2023 version, but I doubt that it matters.

I did notice you are using a Homey Code activity instead of the Homey Script activity. I expect the behaviour to be slightly different between those two. Anyways, I tried Homey Code aswell and this also does not work for me.

Idea: Try to delete the tag (even if you think it don’t exist):

await tag('QualifyingResults', null);

Maybe after this it gets created as should when you run the script?

1 Like

I find it hard to believe, but your suggestion actually solved the issue!!!

2 Likes

Nice to hear! I’m going to buy a “staatslot” now.

Why is it hard to believe? I just seem to have good ideas every now and then :rofl::crazy_face::grimacing:

This particular issue made me think about bitrot.