Help with (another) template

I’m trying to create a card that shows when motion is detected (via a motion sensor in my office), and want to show when last updated as a secondary row, formatted relatively.

By default when triggered the sensor shows ‘0 seconds’, and I want to reformat ‘0 seconds’ to ‘Now’ and then after that show the relative time with the ’ ago’ string added (e.g. 10 minutes ago).

      - type: custom:mushroom-template-card
        entity: sensor.office_motion
        primary: |
          {% if is_state('sensor.office_motion', 'Detected') %}
            Motion Detected
          {% else %}
            None Detected
          {% endif %}
        secondary: |
          {% if is_state('relative_time(states.binary_sensor.office_sensor_occupancy.last_updated)', '0 seconds') %}
            Now
          {% else %}
            {{ (relative_time(states.binary_sensor.office_sensor_occupancy.last_updated)) }} ago
          {% endif %}
        icon: |
          {% if is_state('sensor.office_motion', 'Detected') %}
            mdi:human-handsup
          {% else %}
            mdi:human-male
          {% endif %}

I’ve been trying for quite a while but it still keeps showing ‘0 seconds ago’ when triggered instead of ‘Now’.

Any help would be greatly appreciated :slightly_smiling_face:

Change this:

 {% if is_state('relative_time(binary_sensor.office_sensor_occupancy.last_updated', '0 seconds') %}

To:

 {% if relative_time(state.binary_sensor.office_sensor_occupancy.last_updated) == '0 seconds') %}

is_state() is used to test the state of entities, not compare strings.

i.e.

You have is_state( [a time string], [a state])

When what it should be is is_state( [an entity_id to get the state from], [a state])

1 Like

That’s it! Thanks a lot for that :+1:t3: