# \[HomeyScript\] Timeline Wrangler

**URL:** <https://community.homey.app/t/homeyscript-timeline-wrangler/90714>\
**Category:** Tutorials\
**Tags:** homeyscript, homey-pro\
**Created:** [October 1, 2023, 8:31pm UTC](https://community.homey.app/t/homeyscript-timeline-wrangler/90714 "2023-10-01T20:31:04Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [October 30, 2023, 6:48pm UTC](https://community.homey.app/t/homeyscript-timeline-wrangler/90714/6 "2023-10-30T18:48:32Z")

</div>

Not sure if it is ‘too much’, but I’d like to share my interactive timeline cleanup, which uses this [Telegram app](https://community.homey.app/t/app-pro-telegram/66622), to be able to select pre-defined phrases as answer, or by entering a phrase to match certain timeline entries.  
The timeline entries which contain the answer text , are to be deleted.

 ![Screenshot from 2023-10-30 19-33-24](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/d/c/dc7beae1baa814dcdac83f136206343cc92ee718.png)

- Impression of the Telegram question + answer system, via app settings:  

- The flow asks a question via Telegram, and it comes with selectable answers  
 ![Screenshot from 2023-10-30 19-35-29](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/4/d/4d3ee7fdba4d6ff2fc9519c5a8c7b901f0d6e7c2.png)

- And the same for a free-to-answer question:  
 ![Screenshot from 2023-10-30 19-41-43](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/4/6/46a44eef639033dad50d3b3175e3e0788e39e718.png)

- Feedback  
 ![Screenshot from 2023-10-30 19-36-29](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/a/4/a4c7b644409d09faf1d534ae1c67a2e923b813e3.png)

- Code used in the Homeyscript flow cards:

> **Click me**
>
> ```auto
> // DEL timeline msgs ALL matching | Argument = searchphrase - Timeline_Wrangler v1.3p
> 
> // ==================== User Settings ====================
> const deleteNotifications = true; // If "true": actually removes notifications, if "false": display only
> const daysOld = 0; // Amount of days that need to have past before a notification is deemed old
> const hoursOld = 0; // Amount of hours that need to have past before a notification is deemed old
> const maximumNotifications = 250; // The maximum amount of notifications that stay, anything more will get deleted
> const containsText = args[0]; // Only deletes notifications that contains the specified text, Empty ( '' ) = not used
> const showCategories = false; // If "true" it will show all timeline categories you have and had in the past
> const useCategory = true; // If "true" it will only remove from the specified categories below
> 
> // Separate categories by comma ( , )
> const categories = [
> 'homey:manager:flow',
> 'homey:manager:presence',
> 'homey:manager:apps',
> 'homey:manager:updates',
> 'homey:manager:zigbee',
> 'homey:manager:energy',
> 'homey:manager:backup',
> 'homey:manager:users',
> 'homey:app:com.athom.flowchecker',
> 'homey:manager:security',
> 'homey:app:nl.qluster-it.DeviceCapabilities',
> 'homey:app:net.i-dev.betterlogic',
> 'homey:app:com.spkes.telegramNotifications',
> 'homey:manager:safety',
> 'homey:app:nl.nielsdeklerk.log',
> 'homey:manager:experiments',
> ];
> 
> // ==================== Don't change anything below here ====================
> const removedNotifications = [];
> const notificationCategories = [];
> let amountNotifications = 0;
> let amountKept = 0;
> 
> if (showCategories) {
> await Homey.notifications.getOwners()
> .then(owners => {
> log(Object.keys(owners).length, 'Timeline Categorie(s):');
> Object.keys(owners).forEach(key => {
> if (owners[key].uriObj.hasOwnProperty('name')) {
> log('-', owners[key].uriObj.name, (owners[key].uriObj.name.length <= 5) ? '\t\t\t|' : (owners[key].uriObj.name.length <= 12) ? '\t\t|' : '\t|', '\'' + key + '\'');
> }
> else {
> log('- Unkown Name', '\t\t|', '\'' + key + '\'');
> }
> });
> log('\n');
> })
> .catch(err => {
> log(err);
> return 'Getting categories failed';
> });
> }
> 
> await Homey.notifications.getNotifications()
> .then(notifications => {
> amountNotifications = Object.keys(notifications).length;
> log(amountNotifications, 'Timeline Notification(s):');
> 
> Object.keys(notifications).forEach(key => {
> const currentDate = new Date();
> const dateCreated = new Date(notifications[key].dateCreated);
> const timeDifference = (currentDate - dateCreated) / 1000;
> const daysDifference = Math.floor(timeDifference / 86400);
> const hours = Math.floor((timeDifference % 86400) / 3600);
> const hoursDifference = Math.floor(timeDifference / 3600);
> const minutes = Math.floor((timeDifference % 3600) / 60);
> 
> if (
> ((containsText.length > 0 && notifications[key].excerpt.toLowerCase().includes(containsText.toLowerCase()))
> && (daysDifference >= daysOld && hoursDifference >= hoursOld))
> || maximumNotifications < amountKept
> ) {
> log('- Removing:', notifications[key].id, ' |', notifications[key].ownerUri, '|', notifications[key].excerpt, '(' + notifications[key].dateCreated + ' - ' + daysDifference + ' day(s), ' + hours + ' hour(s) and ' + minutes + ' minute(s) old)');
> 
> if (deleteNotifications) {
> Homey.notifications.deleteNotification({id: notifications[key].id});
> removedNotifications.push(notifications[key].id);
> }
> }
> else if (
> ((containsText.length === 0 && useCategory)
> && (categories.includes(notifications[key].ownerUri))
> && (daysDifference >= daysOld && hoursDifference >= hoursOld))
> || maximumNotifications < amountKept
> ) {
> log('- Removing:', notifications[key].id, ' |', notifications[key].ownerUri, '|', notifications[key].excerpt, '(' + notifications[key].dateCreated + ' - ' + daysDifference + ' day(s), ' + hours + ' hour(s) and ' + minutes + ' minute(s) old)');
> 
> if (deleteNotifications) {
> Homey.notifications.deleteNotification({id: notifications[key].id});
> removedNotifications.push(notifications[key].id);
> }
> }
> else if (
> ((containsText.length === 0 && !useCategory)
> && (daysDifference >= daysOld && hoursDifference >= hoursOld))
> || maximumNotifications < amountKept
> ) {
> log('- Removing:', notifications[key].id, ' |', notifications[key].ownerUri, '|', notifications[key].excerpt, '(' + notifications[key].dateCreated + ' - ' + daysDifference + ' day(s), ' + hours + ' hour(s) and ' + minutes + ' minute(s) old)');
> 
> if (deleteNotifications) {
> Homey.notifications.deleteNotification({id: notifications[key].id});
> removedNotifications.push(notifications[key].id);
> }
> }
> else {
> log('- Keeping: ', notifications[key].id, ' |', notifications[key].ownerUri, '|', notifications[key].excerpt, '(' + notifications[key].dateCreated + ' - ' + daysDifference + ' day(s), ' + hours + ' hour(s) and ' + minutes + ' minute(s) old)');
> amountKept++;
> }
> });
> })
> .catch(err => {
> log(err);
> return 'Script failed';
> });
> 
> if (removedNotifications.length > 0) {
> return 'Removed ' + removedNotifications.length + ' out of ' + amountNotifications + ' notification(s).\nUsed Argument: ' + args[0];
> }
> 
> return 'No notifications were (actually) removed. \nUsed Argument: ' + args[0];
> 
> ```

---

_[View the full topic](https://community.homey.app/t/homeyscript-timeline-wrangler/90714)._
