The QUESTION is at the bottom of this if you don’t need all of this extra detail.
Using a trigger node, when a ^person.*
triggers their location to home
, I use a function to strip the text person.
from the entity_id in the msg and pass that along:
var str = msg.data.entity_id
var person = str.replace("person.","");
flow.set('sfx_name', person)
var name = flow.get('sfx_name');
msg.payload = name;
return msg;
Using the simple queue node, each message stacks up to accommodate the potential arrival of two or more people simultaneously. To release those messages one at a time, a change node sends a trigger
property to the input of the simple queue node, but only if the media player is sitting idle, otherwise the message waits until that’s the case.
When the message is released from the queue, the next function node defines a source
for the media player (the play list in Volumio):
const gHomeAssistant = global.get('homeassistant').homeAssistant;
var sfx_holiday = gHomeAssistant.states['calendar.holidays_in_united_states'].attributes.message;
if (gHomeAssistant.states['calendar.holidays_in_united_states'].state == "on") {
const randomIndex = Math.floor(Math.random() * 9);
fileNumber = randomIndex + 10;
msg.payload = sfx_holiday + fileNumber;
} else {
msg.payload = "arrival_" + msg.payload;
}
return msg;
So the above function checks to see if today is a holiday. If so, we store the name of the holiday plus a random file number so corresponding playlists can be loaded. Today would produce 9 possibilities from Halloween10
to Halloween19
.
If it’s NOT a holiday, we keep the original payload, which is the person’s name, i.e., firstname_lastname.
Note: The number of random options will change from a number to a payload that counts the number of available files in the hassio www folder to avoid extra update work when new playlists are added.
QUESTION(S):
If I have 9 playlists as indicated in randomIndex
in the function example above, how would I modify the code to eliminate numbers as they’re used until all have been used, then reset and start over again so nothing is repeated until all have been used? Right now it’s truly random, but with such a small number, repeats happen frequently.
After this is accomplished, I would assume that a restart of HASS.io or even re-deploying after a change in NodeRED would reset the process…is this correct?