Need Help on a time template

Hi I have a water heater at my home and I am using a template to track the time that the heater is on and off. I would also be able to track my android tv but unfortunately Android TV has states rather then just on and off.

{% set t = 0 if states('media_player.fire_tv_192_168_1_86') == ['idle', 'playing', 'paused', 'standby'] else now().timestamp() - states.media_player.fire_tv_192_168_1_86.last_changed.timestamp() %}
{{ t | timestamp_custom('%H:%M', false) }} 

The above template actually works but whenever TV changes states it resets time. For example if the TV changes from idle to playing the time resets to 00:00. How can I modify it to show one unique time during any state above and reset at the end when the tv passes to off state ?

To test against each individual item in a list instead of the whole list use in instead of ==.

{% set t = 0 if states('media_player.fire_tv_192_168_1_86') in ['idle', 'playing', 'paused', 'standby'] else (now() - states.media_player.fire_tv_192_168_1_86.last_changed).total_seconds() %}
{{ t | timestamp_custom('%H:%M', false) }}

No. Unfortunately using “in” does not prevent the timer to reset on state changes like from idle to playing. I need the timer to only reset when android tv is off.

Sorry, I think I misread the original post… The issue is that last_changed will reset on every state change as well as restart/reload.

Are you specifically trying to log the time the tv turns on or the number of hours it is on?

The latter would be best handled using the History Stats integration.

This will reset to 0 when off, but when do you want the time to be set?

{% set t = 0 if is_state('media_player.fire_tv_192_168_1_86', 'off') else (now() - states.media_player.fire_tv_192_168_1_86.last_changed).total_seconds() %}
{{ t | timestamp_custom('%H:%M', false) }}

At the moment it will set the time on any other state change than “to off”.

Oh, that could be a bit of a spanner in the works.

Maybe a triggered template sensor would be better, depending on when it should actually be set.

1 Like

I am sorry. Maybe I didn’t fully explain it. First of all, I want a timer to start as soon as the television switches from “off” to “on” state. This timer should also not be reset on any other state transitions. For example, when the TV is in the main menu, the state appears as idle. When a movie starts playing, the state is updated to playing. Here, I want this timer to continue without stopping during this transition. However, it is now resetting at state transitions. What I want is to reset only when the TV is turned off. With this I could see how many minutes or hours a device stays online.

History Stats is something else which is not suitable for my situation.

Let me give an example. I have a water heater. Sometimes I close it and forget to turn it back on. So when I go into the shower the cold water welcomes me. To prevent this I need a timer for the last state change. I am building a new dashboard and there will be a place for my devices. When the heater turns off, its picture will also change to another picture reminding me that it is turned off. Below that I want this template to show me the duration that it has been on “off” state. So that I could understand that the water is cold and I could open it back.

For the TV… set up a template binary sensor whose last_changed can better represent the tv switching into one of the “on” states.

template:
  - binary_sensor:
      - name: TV On
        state: >
          {{ is_state('media_player.fire_tv_192_168_1_86', ['idle', 'playing', 'paused', 'standby']) }}
        availability: >
          {{ not is_state('media_player.fire_tv_192_168_1_86', ['unknown', 'unavailable']) }}

Then your dashboard template for the amount of time the tv has been ‘on’ would be something like:

{% set t = 0 if is_state('binary_sensor.tv_on', ['off', 'unknown', 'unavailable']) 
else (now() - states.binary_sensor.tv_on.last_changed).total_seconds() %}
{{ t | timestamp_custom('%H:%M', false) }}

Well I have created a template switch instead. The switch turns on with the states like playing, idle, pause or standby. And it turns off when the TV is switched off. Then I created another 3 seperate templates to track the time since their last state change. This way I achieved what I wanted.

I placed it into a secret and swipeable menu. Now I can track all my device activities.