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

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:

[{"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}]

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

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!

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