Skip to content

Latest commit

 

History

History
57 lines (45 loc) · 1.51 KB

javascript.md

File metadata and controls

57 lines (45 loc) · 1.51 KB

Logo

ioBroker.birthdays

Common function to send messages / notifications

function sendText(text) {
    // Own logic (pushover, telegram, ...)
    sendTo('pushover', 'send', {
        message: text,
        sound: '',
        title: 'Birthday calendar'
    });
}

Notify 1 day before birthday

schedule('0 7 * * *', async () => {
    const nextDaysLeft = getState('birthdays.0.next.daysLeft').val;
    const nextText = getState('birthdays.0.next.text').val;

    const nextAfterDaysLeft = getState('birthdays.0.nextAfter.daysLeft').val;
    const nextAfterText = getState('birthdays.0.nextAfter.text').val;

    // Birthday today
    if (nextDaysLeft == 0) {
        sendText(`Birthdays today: ${nextText}`);

        // If tomorrow is also a birthday
        if (nextAfterDaysLeft == 1) {
            sendText(`Birthdays tomorrow: ${nextAfterText}`);
        }
    } else if (nextDaysLeft == 1) {
        sendText(`Birthdays tomorrow: ${nextText}`);
    }
});

Reminder of birthdays in the upcoming week

// Run script at the beginning of the week
schedule('0 7 * * 1', async () => {
    const summaryObj = JSON.parse(getState('birthdays.0.summary.json').val);

    const nextBirthdays = summaryObj
        .filter(b => b.daysLeft < 7)
        .map(b => `${b.name} turns ${b.age} on ${formatDate(new Date(b._nextBirthday), 'WW')}`);

    if (nextBirthdays.length > 0) {
        sendText(`Birthdays this week: ${nextBirthdays.join(', ')}`);
    }
});