Persistent state ON of group.day.light (code string?)

Hi,
I use this code (it works good) to get list of “dynamic” entity

{% set ns = namespace(entities = []) %}
{% for s in expand('group.day_lights') %}
  {% if s.state == "on" %}
	{% set ns.entities = ns.entities + [ s ] %}
  {% endif %}
{% endfor %}
{{ ns.entities | map(attribute='entity_id') | list | join(', ') }}

How can I use it in the follow code?

{{ ( as_timestamp(now()) - as_timestamp(states.XXXXXXXXXXX.last_changed)) | int(0) > 7200 }}

In alternative, any other way to check al my lights in group.day.lights and turn OFF only the entity that are ON from 2 hours?

Thanks