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
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.
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âŚ
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 %}
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:
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âŚ