Need help with homeyscript

I need to create a base64 encoded timestamp, and chatgpt made the following:

function base64EncodedTimestamp() {
  // Get the current Unix timestamp (epoch) in seconds
  const unixTimestamp = Math.floor(Date.now() / 1000);

  // Convert the Unix timestamp to a Base64-encoded string
  const base64EncodedString = btoa(String.fromCharCode(...new Uint8Array(new ArrayBuffer(4)).fill(unixTimestamp >> 0).reverse()));

  return base64EncodedString;
}
  1. Will this work with homeyscript?
  2. How can I set a local variable in the flow with either return value or set from the script?

Have you tried?

I was at work, so not yet. I will, though :wink: I admit to probably have been a bit too quick to ask once I finally cracked the dynamic api key and was hoping to get a quick answer to if this would even work with homeyscript…

…and no, btoa isn’t a function. So I need to find a way to base64 encode something in homeyscript. Will now begin to tear the script apart to see if there are more things missing.

buffer.toString('base64')

More importantly though, what does “a base64 encoded timestamp” actually mean?

“buffer” doesn’t seem to be a recognized keyword in the homeyscript editor…?..

A base64 encoded timestamp is the unix time encoded with base64. Not sure how to describe it in any other way…

@robertklep Thanks! A little hacking and googling, this seems to work :slight_smile:

// Get the current Unix timestamp (epoch) in seconds

const unixTimestamp = Math.floor(Date.now() / 1000);

//console.log(unixTimestamp);

//console.log("Encoded string: "+ Buffer.from(unixTimestamp.toString()).toString('Base64'));

//console.log("Decoded string: ", decodedString);

return Buffer.from(unixTimestamp.toString()).toString('Base64');

Now, back to the search function for the rest of my query :slight_smile:

I meant “if you have a buffer containing the data that needs to be Base64-encoded, this is how you do it”.

So like this?

Buffer.from(String(Date.now() / 1000)).toString('base64')

Yes, your shorthand should be better than mine :slight_smile: