# Need help with homeyscript

**URL:** <https://community.homey.app/t/need-help-with-homeyscript/79813>\
**Category:** Developers\
**Created:** [April 28, 2023, 12:12pm UTC](https://community.homey.app/t/need-help-with-homeyscript/79813 "2023-04-28T12:12:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tiwas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tiwas/32/10066_2.png) [@Tiwas](https://community.homey.app/u/Tiwas)\
**Post date:** [April 28, 2023, 12:12pm UTC](https://community.homey.app/t/need-help-with-homeyscript/79813/1 "2023-04-28T12:12:53Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [April 28, 2023, 12:19pm UTC](https://community.homey.app/t/need-help-with-homeyscript/79813/2 "2023-04-28T12:19:31Z")

</div>

> [@Tiwas](#):
>
> Will this work with homeyscript?

Have you tried?

---

<div class="post-metadata">

**Author:** ![Tiwas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tiwas/32/10066_2.png) [@Tiwas](https://community.homey.app/u/Tiwas)\
**Post date:** [April 28, 2023, 1:58pm UTC](https://community.homey.app/t/need-help-with-homeyscript/79813/3 "2023-04-28T13:58:11Z")

</div>

I was at work, so not yet. I will, though 😉 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…

---

<div class="post-metadata">

**Author:** ![Tiwas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tiwas/32/10066_2.png) [@Tiwas](https://community.homey.app/u/Tiwas)\
**Post date:** [April 28, 2023, 2:00pm UTC](https://community.homey.app/t/need-help-with-homeyscript/79813/4 "2023-04-28T14:00:57Z")

</div>

> [@Tiwas](#):
>
> ```auto
> // 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;
> 
> ```

…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.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [April 28, 2023, 2:15pm UTC](https://community.homey.app/t/need-help-with-homeyscript/79813/5 "2023-04-28T14:15:47Z")

</div>

> [@Tiwas](#):
>
> So I need to find a way to base64 encode something in homeyscript

```auto
buffer.toString('base64')

```

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

---

<div class="post-metadata">

**Author:** ![Tiwas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tiwas/32/10066_2.png) [@Tiwas](https://community.homey.app/u/Tiwas)\
**Post date:** [April 28, 2023, 2:18pm UTC](https://community.homey.app/t/need-help-with-homeyscript/79813/6 "2023-04-28T14:18:45Z")

</div>

“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…

---

<div class="post-metadata">

**Author:** ![Tiwas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tiwas/32/10066_2.png) [@Tiwas](https://community.homey.app/u/Tiwas)\
**Post date:** [April 28, 2023, 2:33pm UTC](https://community.homey.app/t/need-help-with-homeyscript/79813/7 "2023-04-28T14:33:58Z")

</div>

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

```auto
// 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 🙂

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [April 28, 2023, 2:35pm UTC](https://community.homey.app/t/need-help-with-homeyscript/79813/8 "2023-04-28T14:35:33Z")

</div>

> [@Tiwas](#):
>
> “buffer” doesn’t seem to be a recognized keyword in the homeyscript editor…?..

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

> [@](#):
>
> A base64 encoded timestamp is the unix time encoded with base64. Not sure how to describe it in any other way…

So like this?

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

```

---

<div class="post-metadata">

**Author:** ![Tiwas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tiwas/32/10066_2.png) [@Tiwas](https://community.homey.app/u/Tiwas)\
**Post date:** [April 28, 2023, 3:24pm UTC](https://community.homey.app/t/need-help-with-homeyscript/79813/9 "2023-04-28T15:24:03Z")

</div>

Yes, your shorthand should be better than mine 🙂
