Allow filtering and sorting by last_changed and last_updated. Fix #44

This commit is contained in:
2019-12-10 00:00:17 +01:00
parent b377a20cf9
commit 9a03e251fa
5 changed files with 67 additions and 33 deletions

View File

@@ -120,6 +120,25 @@ export function entity_filter(hass, filter) {
return false;
break;
case 'last_changed':
{
const now = new Date().getTime();
const changed = new Date(entity.last_changed).getTime();
if(!match(value, (now-changed)/60000))
return false;
break;
}
case 'last_updated':
{
const now = new Date().getTime();
const updated = new Date(entity.last_updated).getTime();
if(!match(value, (now-updated)/60000))
return false;
break;
}
default:
return false;
}

View File

@@ -65,6 +65,19 @@ export function entity_sorter(hass, method) {
if(_b === undefined) return gt;
}
return compare(_a, _b);
case "last_changed":
method.numeric = true;
// Note A and B are swapped because you'd most likely want to sort by most recently changed first
return compare(
new Date(entityB.last_changed).getTime(),
new Date(entityA.last_changed).getTime()
);
case "last_updated":
method.numeric=true;
return compare(
new Date(entityB.last_changed).getTime(),
new Date(entityA.last_changed).getTime()
);
default:
return 0;
}