you can? thought only by activating corresponding flows you could do that, not by updating other app’s devices, as it didn’t have the permission to do that.
Hi everyone,
I used lots of examples, and tried all kinds of stuff.
But how do I create a HomeyScript script from the web API playground “Find broken flows” script.
Homey.flow.getFlows().then(f => Object.values(f).reduce((r,b)=> Object.assign(r, b.broken ? {[b.name]:b.broken} : ''), {}) );
I can’t get my head around this cryptogram. So far I have this, but then…
await Homey.flow.getFlows();
var badflows = Homey.flow.getFlows();
await tag("BrokenFlows", badflows); //creating new HomeyScriptTag 'BrokenFlows'
console.log ("Broken flows: ", badflows);
return(true);
Returns: ❌ Script Error ⚠️ Error: invalid_type at Remote Process
Anyone a hint or pointer?
const flows = await Homey.flow.getFlows({ filter: { broken: true }});
console.log(flows)
Thanks a million Jero!
In case filter ever gets removed or something
const flows = await Homey.flow.getFlows();
const brokenAsArray = Object.values(flows).filter(flow => flow.broken);
const brokenAsObj = {};
for (const flow of Object.values(flows)) {
if (flow.broken) {
brokenAsObj[flow.id] = flow;
}
}
console.log(brokenAsArray);
console.log(brokenAsObj);
Thanks Jero, the output makes sense on the console.
But…
I want to send the output to f.i. my timeline and/or as push message.
But then it only sais [object, Object],[object, Object],[object, Object],[object, Object], and so on. (I have 30+ broken flows, I am aware of that)
This, because creating & filling a variable results in an error invalid_type at Remote Process
Only filling the variable is also fine with me, as long as I can display the names of the flows found somewhere.
// Script FindBrokenFlows2.js
const flows = await Homey.flow.getFlows();
const brokenAsArray = Object.values(flows).filter(flow => flow.broken);
const brokenAsObj = {};
for (const flow of Object.values(flows)) {
if (flow.broken) {
brokenAsObj[flow.id] = flow;
}
}
// filling a variable does result in an console error: [Script Error: invalid_type at Remote Process]
// await tag("BrokenFlows", brokenAsArray ); // (create&)write tag "BrokenFlows" with results
console.log("Broken Flows:", brokenAsArray);
//console.log("Broken Flows:", brokenAsObj);
// Timeline notification
await Homey.flow.runFlowCardAction({
uri: 'homey:manager:notifications',
id: 'create_notification',
args: {
text: 'Broken Flows today: ' + brokenAsArray + ' [script: FindBrokenFlows2.js]'
//text: '' + brokenAsArray + ''
},
});
// Push msg to mobile phone
await Homey.flow.runFlowCardAction({
uri: 'homey:manager:mobile',
id: 'push_text',
args: {
user: {
name: 'Peter Kawa',
id: '6b333a3e-288a-4099-a0fb-f3903a605c74',
image: 'https://api.athom.com/user/xxxxxxxxxxxxxxxxxxxxxx/avatar',
athomId: 'xxxxxxxxxxxxxxxxxxxxxxxx',
},
text: 'Broken Flows today: ' + brokenAsArray + ' [script: FindBrokenFlows2.js]'
},
});
Any hints?
You could do JSON.stringify(brokenAsArray) but that would result in an very long string. In case you are only interested in a list of flow names you can use to following:
const flows = Object.values(await Homey.flow.getFlows({filter: { broken: true }})).map(f => f.name)
In this case you don’t need JSON.stringify
Thanks @tb1962 & @Jero
Works like a charm now.
Script:
// Get broken flows and write result to a variable/tag
const flows = Object.values(await Homey.flow.getFlows({filter: { broken: true }})).map(f => f.name)
const flowsString = JSON.stringify(flows) // Create string [FlowString] from array [flows], so it can be written into variable "BrokenFlows"
await tag("BrokenFlows", flowsString ); // (create&)write variable/tag [BrokenFlows] with results of [FlowString]
// Just to check the results in HomeyScript console
// console.log("Broken Flows (Array):", flows);
// console.log("Broken Flows (String):", flowsString);
return (true);
Flow:
Timeline:
Only the names:
const flows = await Homey.flow.getFlows({ filter: { broken: true }});
for (const flow of Object.values(flows)) {
console.log(flow.name)
}
Hey!
I can’t get the HomeyScript console to work.
I’ve tried running several of the examples. But the console is just blank.
Scripts seem to work, I can make Homey say something, but nothing is being written to the console.
Is this a known issue? Any way to get it working?
Thanks!
/Olof
Which browser?
Chrome, but I’ve tried Edge as well. Same result.
I’ve also tried uninstalling HomeyScript and reinstalling. Logging out and logging back on again.
So far I haven’t tried a re-start of Homey, I guess that might be next.
Maybe you have to scroll-up or enlarge the monitor-window (drag upwards)
This is what it looks like when I’m running a script, no output. I tried on my iPad using safari. There I can get the output, so most likely something is wrong with my laptop browser… much annoying…
Maybe you can try a portable browser. You can find different browsers at Portableapps website.
Then you don’t have the problem of installed plug-ins, custom settings, which can cause strange effects.
Did you try Firefox?
Are you using any browser extensions that might be causing this issue? Check your browser’s developer console to see if there are any errors popping up.
Hej!
Thanks for the suggestions!
It seems like the issue might be that I was using my company-laptop. There must be something blocking the Homey Script.
I’ve finally found my old private MacBook and using that one I got the Homey Script to work.
Now I just need to learn scripting!
Hoi ik wil een export maken naar google sheets en ik kan dan als variabel de datum meenemen b.v. 17-01-2023.
Wat ik graag zou willen is ook alleen de maand ( 01 ) van die datum (huidige datum van de export) meenemen in mijn export.
Dat moet volgens mij niet moeilijk zijn alleen heb ik geen kaas gegeten van Homeyscript c.a. programeren.
Of eigenlijk nog beter zou ik de datum los als jaar, maand en dag willen zien, nu ik toch bezig ben.
Here is some code. All data will be given as a tag. De tags you don’t need, just add // in front of ‘await tag’. This will give e.g //await tag(‘CurrentDay’, currentDay); and this tag will not be available
var date = new Date();
date.setDate(date.getDate());
const currentDay = date.getDate();
await tag('CurrentDay', currentDay);
console.log('1 CurrentDay is: ' +currentDay);
const currentMonth = date.getMonth() +1; // getMonth() returns month from 1 to 12
await tag('CurrentMonth', currentMonth);
console.log('2 CurrentMonth is: '+currentMonth);
const currentYear = date.getFullYear();
await tag('CurrentYear', currentYear);
console.log('3 CurrentYear is: '+currentYear);
var date = new Date(); // current date
date.setDate(1); // going to 1st of the month
date.setHours(-1);// going to the last day of the previous Month
const yearPreviousMonth = date.getFullYear();
await tag('YearPreviousMonth', yearPreviousMonth);
console.log('4 YearPreviousMonth is: '+yearPreviousMonth);
const lastDayPreviousMonth = date.getDate();
await tag('LastDayPreviousMonth', lastDayPreviousMonth);
console.log('5 LastDayPreviousMonth is: '+lastDayPreviousMonth);
const previousMonth = date.getMonth() +1;
await tag('PreviousMonth', previousMonth);
console.log('6 PreviousMonth is: '+previousMonth);
var date = new Date(); // current date
date.setDate(1); // going to 1st of the month
date.setMonth(-2)// going back two months
date.setHours(-1);// going to the last day of the previous Month
const yearPreviousTwoMonth = date.getFullYear();
await tag('YearPreviousTwoMonth', yearPreviousTwoMonth);
console.log('7 YearPreviousTwoMonth is: '+yearPreviousTwoMonth);
const previousTwoMonth = date.getMonth() +1;
await tag('PreviousTwoMonth', previousTwoMonth);
console.log('8 PreviousTwoMonth is: '+previousTwoMonth);
var date = new Date();
date.setDate(date.getDate());
function getDaysInMonth(year, month) {
return new Date(year, month, 0).getDate();
}
const daysInCurrentMonth = getDaysInMonth(currentYear, currentMonth);
await tag('DaysInCurrentMonth', daysInCurrentMonth);
console.log('9 DaysInCurrentMonth is: '+daysInCurrentMonth);
const daysInPreviousMonth = getDaysInMonth(currentYear, previousMonth);
await tag('DaysInPreviousMonth', daysInPreviousMonth);
console.log('10 DaysInPreviousMonth '+previousMonth +' is: '+daysInPreviousMonth);
var date = new Date();
date.setDate(date.getDate()-1);
const yesterDay = date.getDate();
await tag('YesterDay', yesterDay);
console.log('11 YesterDay is: ' +yesterDay);
const yesterDayMonth = date.getMonth() +1; // getMonth() returns month from 0 to 11
console.log('12 YesterDayMonth is: ' +yesterDayMonth);
await tag('YesterDayMonth', yesterDayMonth);
const yesterDayYear = date.getFullYear();
await tag('YesterDayYear', yesterDayYear);
console.log('13 YesterDayYear is: '+yesterDayYear);
var date = new Date();
date.setDate(date.getDate());