Howto get list of switches on for 15 mins, by using filter?

Hello,

I’d like to get a list of switches and lights which are on for 15 mins, using filters. I got to this far:

{% set on_devices = states
| selectattr('domain', 'in', ['switch', 'light'])
| selectattr('state', '==', 'on')
| selectattr('last_changed', '<', now())
| join('\n', attribute='name') %}

But it does not serve the purpose yet. The first filter selected switch and light entities. The second filter selected only ‘on’ entities. The third filter was trying to implement ‘on for 30 mins’ but I don’t know how to make: last_changed < now() - 30 mins. Can s.o advice?

timedelta()

{% set on_devices = states
| selectattr('domain', 'in', ['switch', 'light'])
| selectattr('state', '==', 'on')
| selectattr('last_changed', '<', now() - timedelta(minutes=30))
| join('\n', attribute='name') %}

Thank you. It works like a charm!

Templating in HA is liks gold mining. It’s hardly to find what you need. But luckily we have a great community.

1 Like