Ltek
April 26, 2025, 11:18pm
1
I have an Input Select helper where I want to record/track the time it was in its PRIOR state.
Example Entity :
Current State = ‘Home’
Prior State = ‘Away’
I need to record the duration from when Current State = Away
…so I can automate something based on the time it was in the prior state.
thx for the help!
template:
- trigger:
- platform: state
to:
- 'Home'
from:
- 'Away'
entity_id: input_select.example
sensor:
- name: Previous State duration
state: |
{{ (trigger.to_state.last_changed - trigger.from_state.last_changed).total_seconds() }}
Note that similar templates can be used directly in automations with state based triggers. This is most often used for conditional logic.
triggers:
- trigger: state
to: 'Home'
from: 'Away'
entity_id: input_select.example
conditions:
- alias: Check if was "Away" for at least an hour
condition: template
value_template: |
{{ (trigger.to_state.last_changed - trigger.from_state.last_changed)
>= as_timedelta('01:00:00') }}
There are also two integrations that might be of some use…
History Stats
MeasureIt custom integration
… but I don’t think they can do exactly what you are asking.
1 Like
Ltek
April 27, 2025, 2:57pm
4
thank you! I didnt realize HA stored from/to time stamps. I’ll test/refine the automation setup and post back so it may help others.
Ltek
April 28, 2025, 1:20pm
5
[HOW TO] Complete Automation for Tracking Time Duration of an Entity State
– Helper –
input_number.house_mode_last_duration
– Automation : updates Helper value –
alias: Last House Mode Last Duration
triggers:
- entity_id:
- input_select.house_mode
from: null
to: null
trigger: state
conditions: []
actions:
- target:
entity_id: input_number.house_mode_last_duration
data:
value: >
{{ (trigger.to_state.last_changed -
trigger.from_state.last_changed).total_seconds() }}
action: input_number.set_value
– Automation : Trigger Needed Action, this also uses another helper as a condition, that is just how I use it –
alias: House Mode changed - Last Duration Check
triggers:
- entity_id: person.name
to: home
trigger: state
conditions:
- condition: numeric_state
entity_id: input_number.house_mode_last_duration
above: 300
- condition: state
entity_id: input_select.house_mode_last
state: Local
actions:
- target:
entity_id: automation.notify_returning_home_local_name
action: automation.trigger
data:
skip_condition: true
– Simple Dashboard Card –
type: custom:button-card
entity: input_number.house_mode_last_duration
show_icon: true
show_name: true
show_state: true
layout: horizontal
name: Last House Mode Duration
tap_action:
action: more-info
state_display: |
[[[
var total = parseInt(entity.state);
var days = Math.floor(total / 86400);
var hours = Math.floor((total % 86400) / 3600);
var minutes = Math.floor((total % 3600) / 60);
var seconds = total % 60;
var parts = [];
if (days > 0) parts.push(days + 'd');
if (hours > 0 || days > 0) parts.push(hours + 'h');
if (minutes > 0 || hours > 0 || days > 0) parts.push(minutes + 'm');
parts.push(seconds + 's');
return parts.join(' ');
]]]
styles:
card:
- border: null
- box-shadow: null
- background: null
- padding: 6px 12px
- margin: 4px
- font-size: 14px
- font-family: Monotype, sans-serif
- cursor: pointer
grid:
- grid-template-areas: "'i n s'"
- grid-template-columns: 20px 1fr auto
- align-items: null
- column-gap: 18px
icon:
- width: 20px
- color: |
[[[
if (entity.state < 3600) return "lightgreen";
if (entity.state < 86400) return "orange";
return "red";
]]]
name:
- justify-self: start
- font-weight: null
- font-size: 14px
- color: null
state:
- justify-self: end
- font-family: Monotype, sans-serif
- font-size: 14px
- color: |
[[[
if (entity.state < 3600) return "lightgreen";
if (entity.state < 86400) return "orange";
return "red";
]]]
Ltek:
[SOLUTION]
That is not how you mark a solution. Please read the pinned post if you don’t know how it’s done.
Also, if anything, you should mark @Didgeridrew ’s post with the proper solution tag since he’s the one who helped you solve the issue.