Show time (2 hours go, 3 days ago, etc) since sensor last updated

I wanted to show when a sensor was last updated (vibration sensor). I followed this previous forum post, but got errors when I used it in my configuration.yaml.

sensor:
  - platform: template
    sensors:
      dog_feed_time:
        friendly_name: "Last Dog Feeding"
        value_template: >
        {%- set time = (as_timestamp(now()) - as_timestamp(states.sensor.dog_feeding.last_changed)) | int  %}
        {%- set minutes = ((time % 3600) // 60) %}
        {%- set minutes = '{}minutes'.format(minutes) if minutes > 0 else '' %}
        {%- set hours = ((time % 86400) // 3600) %}
        {%- set hours = '{}hours '.format(hours) if hours > 0 else '' %}
        {%- set days = (time // 86400) %}
        {%- set days = '{}days '.format(days) if days > 0 else '' %}
        {{ 'Less than 1 minute' if time < 60 else days + hours + minutes }} ago

The error message appears at the top of the file editor plugin:


missed comma between flow collection entries at line 139, column 10:
            {%- set time = (as_timestamp(now( ... 
             ^

I haven’t been able to spot what it’s complaining about. Line 139 is where I start:

{%- set time = (as_timestamp(now()) - as_timestamp(states.sensor.dog_feeding.last_changed)) | int  %}

Template needs to be indented

sensor:
  - platform: template
    sensors:
      dog_feed_time:
        friendly_name: "Last Dog Feeding"
        value_template: >
          {%- set time = (as_timestamp(now()) - as_timestamp(states.sensor.dog_feeding.last_changed)) | int  %}
          {%- set minutes = ((time % 3600) // 60) %}
          {%- set minutes = '{}minutes'.format(minutes) if minutes > 0 else '' %}
          {%- set hours = ((time % 86400) // 3600) %}
          {%- set hours = '{}hours '.format(hours) if hours > 0 else '' %}
          {%- set days = (time // 86400) %}
          {%- set days = '{}days '.format(days) if days > 0 else '' %}
          {{ 'Less than 1 minute' if time < 60 else days + hours + minutes }} ago