Hi! I have searched the internet for two days and two nights, to no avail.
I have an input boolean “input_boolean.3d” that is triggered to “on” from node red whenever my 3d-printer starts printing, and subsequently turned to “off” whenever my printer finishes.
I would like to track the total time of the current cycle the printer has been active, and reset it to zero when it’s turned off, so that it only counts time each time it’s on, from zero. I have only managed to get it to track total time using history.
{{ (now() - states['input_boolean.3d']['last_changed'])
if is_state('input_boolean.3d', 'on') else "0"|as_timedelta }}
The different types of brackets are correct. I’d recommend not starting an entity ID with a digit (e.g. 3d) as you may find problems later on. For example, you might read other posts and try to use something like:
{{ states.input_boolean.3d.state }}
but that will fail with an entity ID that starts with a digit.
{% set s = ((now() - states['input_boolean.YOUR_ID']['last_changed'])
if is_state('input_boolean.YOUR_ID', 'on')
else "0"|as_timedelta).total_seconds() %}
{% set h, s = s//3600, s%3600 %}
{% set m, s = s//60, s%60 %}
{{ "%02d:%02d" % (h, m) }}