Help please complex template sensor - list of remote sensors that include date of today

Hi can someone help me please with the following template? Could be complex. I’m struggling for a couple of hours now.

I’ve got 6 sensors that tracks if and how a specific remote is used and knows 2 states:

  • “Not used since 01 Sep 24, 13:31 (23 hours ago).”
  • “02 Sep 24, 11:56 - Down”

The date, timestamp and command can change, based on how I use the specific remote.

I want to know which remote is used today so that I can use this info for other purposes.

Whatever I try I don’t get the used remotes of today in a list. One of things I tried:

          {% set today = now().strftime('%d %b %y') %}
          {% set sensors = [
            'sensor.room1_remote',
            'sensor.room2_remote'
          ] %}
          {% set result = [] %}
          {% for sensor in sensors %}
            {% set state = states(sensor) %}
            {% if state and today in state and not state.startswith('Not used since') %}
              {% set result = result + [sensor] %}
            {% endif %}
          {% endfor %}
          {{ result | join(', ') if result else 'No sensors with today\'s date' }}

But I always get the message that there is no sensors that meets the criteria?!

both sensors have a state as follows:

  • Room1 remote:
    Not used since 01 Sep 24, 13:31 (23 hours ago).

  • Room2 remote
    02 Sep 24, 11:56 - Down

so it should show me the latest sensor because its state include the date of today.
if the first sensor also includes the date of today but is not used, it should be excluded!

if it helps, this is how the “remote” sensors are setup:

template:
  - sensor:
      - name: Uptime relatively
        unique_id: uptime_relatively
        state: >-
          {{ states("sensor.uptime") | as_datetime | relative_time }}

      - name: "Room1 remote"
        unique_id: room1_remote
        state: >-
         {% set fired = state_attr('cover.room1', 'cmd_fired') %}
          {% if not fired | is_number %}
            Not used since {{ as_timestamp(states('sensor.uptime')) | timestamp_custom('%d %b %y, %H:%M') }} ({{ states('sensor.uptime_relatively') }} ago).
          {% else %}
            {{ fired | int | timestamp_custom('%d %b %y, %H:%M') }} - {{ state_attr('cover.room1','last_cmd') }}
          {% endif %}

any help is appreciated.

Certainly complicated. Have you tried use the developer tools / templates to debug?

Try this version in the Template Editor:

{% set sensors = ['sensor.room1_remote', 'sensor.room2_remote'] %}
{% set result = sensors | expand 
  | rejectattr('state', 'match', 'Not used since')
  | selectattr('state', 'search', now().strftime('%d %b %y'))
  | map(attribute='entity_id') | list %}
{{ result | join(', ') if result|count > 0 else 'No sensors with today\'s date' }}

I tested it on my system (using different sensors and a different date string) and it correctly reported all sensors containing the date string.

2 Likes

exactly what I was looking for. thanks again @123 !!

1 Like