Seconds since last state change - updated every second?

Hi there,

I got a switch for my water pump, and i’d like to monitor it on the Dashboard, showing the duration of the running pump. I managed to update it every minute using:

{% set t = states.sensor.time %}
{{ (as_timestamp(now()) - as_timestamp(states.switch.garage_pumpe.last_changed))|int }} seconds

Unfortunatelly, this does only update every minute - is there a way to get a bit more responsive feeling by updating this sensor every second?

Do you really need that? That will be a lot of data that will be written for, what I suppose, no reason.
I believe the workaround would be to create a counter that you increment with an automation, and another automation to reset it when the pump runs.
Then using the counter value create a sensor that makes it in HH:MM:SS format.

Where is excesssive data being written? I only want to use that in the dashboard, not as a sensor / templated sensor itself.

See this post, this could be a solution:

1 Like

In order to make it update every second I think you need to create a new sensor with the current seconds since pump run.
Perhaps I’m wrong.

^^ But update entity will not work since there is no entity to update.

automation:

  - alias: Second updater
    id: second_updater
    mode: restart
    trigger:
      platform: time_pattern
      seconds: '/1'
    condition:
      condition: state
      entity_id: input_boolean.second_updater
      state: 'on'
    action:
      service: homeassistant.update_entity
      entity_id:
        - sensor.second_updater

using that in test env on:

      - unique_id: second_updater
        name: Second updater
        state: >
          {{now().strftime('%-S')}}
        icon: mdi:clock

but you can use it on any sensor. Its not cheap though, and will take resources as others have stated. There’s a reason the sensors update per minute…

I don’t understand why either.
I monitor my Heating system using the history integration, which gives me a h:m:s output on the dashboard.

 - platform: history_stats
   name: Heating Run Time Today
   entity_id: switch.house_boiler
   state: "on"
   type: time
   start: "{{ now().replace(hour=0,minute=0,second=0) }}"
   end: "{{ now() }}"

Your posted code -

{% set t = states.sensor.time %}
{{ (as_timestamp(now()) - as_timestamp(states.switch.garage_pumpe.last_changed))|int }} seconds

Does not make sense to me, because you aren’t checking the state of the switch - just how long ago the state last changed. And you can do that directly in the dashboard by setting the secondary attribute to last changed?

But it doesn’t update every second as OP requested.

No I understand that, I was just saying I don’t understand the need to update it every second, especially given that it is only tracking the state change, but not when it last changed to a specific state.

I stripped down the code to the bare minimum of “displaying a timespan”, removed all that cluttering of displaying the time based on the state :slight_smile:

Does your switch have a current sensor also? Asking because I have water pump analytics that detect pump problems using electrical current.

Nope, its just a controllable power outlet, to which the pump is hooked up.