# Different TTS messages on each button press, reset after 30min

**URL:** https://discourse.nodered.org/t/different-tts-messages-on-each-button-press-reset-after-30min/64741
**Category:** General
**Created:** [5 July 2022 19:28 UTC](https://discourse.nodered.org/t/different-tts-messages-on-each-button-press-reset-after-30min/64741 "2022-07-05T19:28:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![TheSirSpence](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thesirspence/32/64243_2.png) [@TheSirSpence](https://discourse.nodered.org/u/TheSirSpence)
#### Post date: [5 July 2022 19:28 UTC](https://discourse.nodered.org/t/different-tts-messages-on-each-button-press-reset-after-30min/64741/1 "2022-07-05T19:28:48Z")

</div>

Hi all, sorry to have to post this but I am defeated. I have been scouring the forum and google looking for a solution to what I want to achieve, I guess being new has limited this a little as I don't always know the correct terms to search.

Anyway, I have a button on my fridge. when I press it, it tells the kids to come and eat. The next time I press it, it tells them to come and eat in a slightly more passive agressive way and so on.

I struggled to come up with a way to do this until I found a forum post about the colour outputs from a function node. So I stole that node and edited it slightly (and I suspect badly).

This works well as it stands, except that the message placement does not reset, each meal time the messages get more passive agressive rather than resetting after a short while.

If anyone has any suggestions how I can return to the first message after say 30 min of the button not being pressed, I would appreciate the advice.

a simplified version of the flow below:

```auto
[{"id":"fd722046941b9f41","type":"api-call-service","z":"6988740dd3b88c4d","name":"GH TTS","server":"709b8b7e.6e6514","version":5,"debugenabled":false,"domain":"tts","service":"cloud_say","areaId":[],"deviceId":[],"entityId":["media_player.bedroom_clock","media_player.loft_speaker"],"data":"{\"message\":\"{{payload}}\", \"language\":\"en-GB\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":640,"y":540,"wires":[[]]},{"id":"465167a9e36f814c","type":"debug","z":"6988740dd3b88c4d","name":"payload","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":720,"y":700,"wires":[]},{"id":"2b14f73f9d790dd0","type":"function","z":"6988740dd3b88c4d","name":"Message cycle","func":"\nconst colours = [\n \"Come downstairs, it is time to eat!\", \n \"Food is on the table getting cold! Come and Eat it!\", \n \"I have told you twice already, I expect you in the kitchen in ten seconds.\",\n];\n\n/ ****dont change***** /\nlet colour = context.get(\"colour\"); //restore last colour\ncolour = colour == null ? 0 : colour + 1;//inc colour\nif(colour >= colours.length) {\n colour = 0;\n}\nmsg.payload = colours[colour];\ncontext.set(\"colour\", colour); //store last colour\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":700,"wires":[["465167a9e36f814c","fd722046941b9f41"]]},{"id":"845b27612c2a182b","type":"inject","z":"6988740dd3b88c4d","name":"fake button","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":280,"y":700,"wires":[["2b14f73f9d790dd0"]]},{"id":"709b8b7e.6e6514","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":30}]

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [5 July 2022 21:01 UTC](https://discourse.nodered.org/t/different-tts-messages-on-each-button-press-reset-after-30min/64741/2 "2022-07-05T21:01:40Z")

</div>

you need to store the last time the function ran, then each time compare that to the actual time.  
e.g.

```auto
const colours = [ 
 "Come downstairs, it is time to eat!", 
 "Food is on the table getting cold! Come and Eat it!", 
 "I have told you twice already, I expect you in the kitchen in ten seconds.",
];
const reset_time = 30; // time in minutes
/ ****dont change***** /
const old_time = context.get("old_time") || 0; // get last time function ran
let colour = context.get("colour") || 0; //restore last colour or default
const new_time = new Date().valueOf();
if ((new_time - old_time) > (reset_time * 60000) || colour >= colours.length) colour = 0;
msg.payload = colours[colour];
context.set(["colour", "old_time"], [colour+1, new_time]); //store last colour and time
return msg;

```

---

<div class="post-metadata">

### Author: ![TheSirSpence](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thesirspence/32/64243_2.png) [@TheSirSpence](https://discourse.nodered.org/u/TheSirSpence)
#### Post date: [6 July 2022 08:30 UTC](https://discourse.nodered.org/t/different-tts-messages-on-each-button-press-reset-after-30min/64741/3 "2022-07-06T08:30:10Z")

</div>

> [@E1cid](#):
>
> ```auto
> const colours = [ 
> "Come downstairs, it is time to eat!", 
> "Food is on the table getting cold! Come and Eat it!", 
> "I have told you twice already, I expect you in the kitchen in ten seconds.",
> ];
> const reset_time = 30; // time in minutes
> / ****dont change***** /
> const old_time = context.get("old_time") || 0; // get last time function ran
> let colour = context.get("colour") || 0; //restore last colour or default
> const new_time = new Date().valueOf();
> if ((new_time - old_time) > (reset_time * 60000) || colour >= colours.length) colour = 0;
> msg.payload = colours[colour];
> context.set(["colour", "old_time"], [colour+1, new_time]); //store last colour and time
> return msg;
> 
> ```

Fantastic, that seems to work perfectly! Thank you so much!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [20 July 2022 08:30 UTC](https://discourse.nodered.org/t/different-tts-messages-on-each-button-press-reset-after-30min/64741/4 "2022-07-20T08:30:27Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
