Homescript capabilitiesObj.measure_power.lastUpdate

Hello,
I have created a script to get the lastupdated (date/time) of capabilitesObj.measure_power of one of my devices.

The script below works fine if I run it manually in the script window. But if I run the script in a flow it does not get the measure_power.lastupdated. It keeps using the value of the last time i ran the script manually.
Any suggestions how to retrieve the actual capabilitiesObj.measure_power.lastUpdate while running the script in a flow?

let device = await Homey.devices.getDevice({id : ā€˜8f3bc736-8121-4bf4-b277-d2b6b14cca9dā€™});

var lastUpdated = new Date(device.capabilitiesObj.measure_power.lastUpdated);

let now = new Date();

let Minutes = parseInt((now - lastUpdated)/(1000*60));

let BLApp = await Homey.apps.getApp({id:ā€œnet.i-dev.betterlogicā€ });
BLApp.apiPut(ā€œKoelkastkeuken/ā€+Minutes);

Can you also share your flow?

image

maybe you can try:
let result = await BLApp.apiPut("Koelkastkeuken/"+Minutes);
return(result);

And probably some delay the on the second Script-call if this has to be done after the first.
Otherwise they will execute at the same time.

Removed the second script from the flow and tried the suggestion, but still not wokrng.
It looks like that the statement
var lastUpdated = new Date(device.capabilitiesObj.measure_power.lastUpdated);
is not working if it being called in the flow.
Because the calculation and updating the variable is working, only it is using the lastupdated date from the last time i did run the script manual.

Did a test on this and that is true, have no idea how this is possible,
maybe some JavaScript Guru can answer? @Dijker ??

Instead of using device.capabilitiesObj, which is lazily updated, you should create a capability instance:

const device      = await Homey.devices.getDevice({id : '8f3bc736-8121-4bf4-b277-d2b6b14cca9d'});
const capability  = device.makeCapabilityInstance('measure_power');
const lastUpdated = new Date(capability.lastChanged); // not `lastUpdated` because that would be too consistentā€¦
ā€¦

That looks great, Thanks @robertklep

Thanks, it is working now!

1 Like