Im trying to display the time since my motion sensor has detected motion and its not working

  - type: entity
    entity: binary_sensor.4_in_1_sensor_home_security_motion_detection
    content_info: last-changed
    icon_color: |
      {% if is_state("input_boolean.away_switch", "on") %}
        grey
      {% else %}
        green
      {% endif %}
    icon: mdi:motion-sensor
    tap_action:
      action: call-service
      service: input_boolean.toggle
      data: {}
      target:
        entity_id: input_boolean.away_switch
  - type: template
    entity: |-
      binary_sensor.4_in_1_sensor_home_security_motion_detection
      {% endif %}
    icon_color: |
      {% if is_state("input_boolean.away_switch", "on") %}
        grey
      {% else %}
        green
      {% endif %}
    icon: |
      {% if is_state("input_boolean.away_switch", "on") %}
        mdi:motion-sensor-off
      {% else %}
        mdi:motion-sensor
      {% endif %} 
    tap_action:
      action: call-service
      service: input_boolean.toggle
      data: {}
      target:
        entity_id: input_boolean.away_switch

As you can see, the first “Chip” shows the time since motion was detected, but I wanted the color to change based on the state of switch. I then tried to recreate the chip as a template, but I cannot get it to display the time since motion was detected. I then tried to create a “History Stat” sensor and that doesnt seem to work either. Seems like it should be fairly simple but I am doing something wrong. O.o

  - platform: history_stats
    name: Motion Detection Duration
    entity_id: binary_sensor.4_in_1_sensor_home_security_motion_detection
    state: off
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
    type: time

Your History Stats is going to total the time “off” for the day, not the duration since last off. You could do something like this:

{% if states('input_boolean.away_switch') == 'on' %}
  {% set duration = now() - states.input_boolean.away_switch.last_updated %}
  {% if duration < timedelta(hours = 1) %}
    red {# Less than an hour #}
  {% elif duration < timedelta(hours = 3) %}
    blue {# 1-3 hours #}
  {% else %}
    green {# Over 3 hours #}
  {% endif %}
{% else %}
  grey {# off #}
{% endif %}

However, last updated is also changed on a restart of HA, so in this case it will show you time since restart, not time since last off. If you wanted to cater for this you could store when it when “off” to “on” elsewhere and use that instead of last updated.

1 Like

This gets me a lot closer! Im actually trying to just display the amount of time since motion was detected by binary_sensor.4_in_1_sensor_home_security_motion_detection in seconds, mins, and hours. Can that be done?

I don’t believe the ‘entity’ mushroom chip supports templating in ‘content_info’. If you’re wanting to put it in the ‘template’ chip…

    content: >-
      {% if states('input_boolean.away_switch') == 'on' %}
        {{ now() - states.input_boolean.away_switch.last_updated }}
      {% endif %}

The format of this is like “1 day, 22:39:02.845954”. If you don’t like that you can use the properties on the duration to get a different format…

{% if states('input_boolean.away_switch') == 'on' %}
  {% set duration = now() - states.input_boolean.away_switch.last_updated %}
  {% set d = duration.days %}
  {% set h = duration.seconds//3600 %}
  {% set m = (duration.seconds//60)%60 %}
  {% set s = (duration.seconds%60%60) %}
  {% if d>0 %}{{d}}d{%- endif -%}
  {% if h>0 %}{{h}}h{%- endif -%}
  {% if m>0 %}{{m}}m{% endif -%}
  {% if s>0 %}{{s}}s{% endif -%}
{% endif %}

This would give you “1d22h39m2s”.

type: template
entity: binary_sensor.4_in_1_sensor_home_security_motion_detection
icon_color: |
  {% if is_state("input_boolean.away_switch", "on") %}
    grey
  {% else %}
    green
  {% endif %}
icon: |
  {% if is_state("input_boolean.away_switch", "on") %}
    mdi:motion-sensor-off
  {% else %}
    mdi:motion-sensor
  {% endif %} 
tap_action:
  action: call-service
  service: input_boolean.toggle
  data: {}
  target:
    entity_id: input_boolean.away_switch
content: 'I want to see the time since motion was detected by binary_sensor.4_in_1_sensor_home_security_motion_detection right here'

Ok, thank you for the help so far, I seem to have miscommunicated though. I have everything I wanted from the away switch handled, I just want to see time since motion was detected by the 4_in_1 sensor. I attempted to switch this into your code and it didnt work.

Do I need to create another input boolean to track this?

This is more like what I am trying to do, I think, but it doesnt work:

  - type: template
    entity: binary_sensor.4_in_1_sensor_home_security_motion_detection
    icon_color: |
      {% if is_state("input_boolean.away_switch", "on") %}
        grey
      {% else %}
        green
      {% endif %}
    icon: |
      {% if is_state("input_boolean.away_switch", "on") %}
        mdi:motion-sensor-off
      {% else %}
        mdi:motion-sensor
      {% endif %} 
    tap_action:
      action: call-service
      service: input_boolean.toggle
      data: {}
      target:
        entity_id: input_boolean.away_switch
    content: >-
      {% if
      states('binary_sensor.4_in_1_sensor_home_security_motion_detection')
      == 'off' %}
        {{ now() - states.binary_sensor.4_in_1_sensor_home_security_motion_detection }}
      {% endif %}

Ah, sorry for my poor reading skills. You’re missing the .last_updated part at the end:

{{ now() - states.binary_sensor.4_in_1_sensor_home_security_motion_detection.last_updated }}
1 Like

Its just outputting as code, not the desired number. Im not sure why? I must have copied your code wrong somewhere in there.

type: template
entity: binary_sensor.4_in_1_sensor_home_security_motion_detection
icon_color: |
  {% if is_state("input_boolean.away_switch", "on") %}
    grey
  {% else %}
    green
  {% endif %}
icon: |
  {% if is_state("input_boolean.away_switch", "on") %}
    mdi:motion-sensor-off
  {% else %}
    mdi:motion-sensor
  {% endif %} 
tap_action:
  action: call-service
  service: input_boolean.toggle
  data: {}
  target:
    entity_id: input_boolean.away_switch
content: >-
  {% if states('binary_sensor.4_in_1_sensor_home_security_motion_detection') ==
  'off' %}
    {{ now() - states.binary_sensor.4_in_1_sensor_home_security_motion_detection.last_updated }}
  {% endif %}

It does work for me (after changing to an entity I have). I’ve noticed that if you get anything wrong it shows the code instead of the template. Try pasting it into the Template tab of Developer Tools and see if you get an error.

I think the most likely problem is that your entity starts with a digit. This is not allowed in Javascript or Python (not sure which applies), so you have to access it differently. Instead of…

{{ now() - states.binary_sensor.4_in_1_sensor_home_security_motion_detection.last_updated }}

…try…

{{ now() - states.binary_sensor["4_in_1_sensor_home_security_motion_detection"].last_updated }}

This isn’t necessary for the “if” statement though.

Wow, that got us most of the way there! Thank you.

type: template
entity: binary_sensor.4_in_1_sensor_home_security_motion_detection
icon_color: |
  {% if is_state("input_boolean.away_switch", "on") %}
    grey
  {% else %}
    green
  {% endif %}
icon: |
  {% if is_state("input_boolean.away_switch", "on") %}
    mdi:motion-sensor-off
  {% else %}
    mdi:motion-sensor
  {% endif %} 
tap_action:
  action: call-service
  service: input_boolean.toggle
  data: {}
  target:
    entity_id: input_boolean.away_switch
content: >+
  {% if states('binary_sensor.4_in_1_sensor_home_security_motion_detection') ==
  'off' %}
    {% set duration = now() - states.binary_sensor["4_in_1_sensor_home_security_motion_detection"].last_updated %}
    {% set d = duration.days %}
    {% set h = duration.seconds//3600 %}
    {% set m = (duration.seconds//60)%60 %}
    {% set s = (duration.seconds%60%60) %}
    {% if d>0 %}{{d}}d {%- endif -%}
    {% if h>0 %}{{h}}h {%- endif -%}
    {% if m>0 %}{{m}}m {% endif -%}
    {% if s>0 %}{{s}}s {% endif -%} ago
  {% endif %}

It sometimes outputs a little strangely when the count of seconds is low, but its working.