How many seconds was last run (watter-pump)

Hello, I Want measure how many seconds was last run of My water-pump, (above 100w means water pump running, 0W= water pump is off) I have tasmota plug with power consumption.

 {{ ((as_timestamp(now()) - as_timestamp(states.sensor.refoss_plug1_energy_power.last_changed)) / 60) | round(0) }}

But time still Increasing

The provided template will return the number of minutes since the state of the sensor changed. You should create a template binary sensor that is “on” when the power consumption is above your threshold. Then create a History Stats sensor to track the time the binary sensor is “on”.

Thank you, but I don’t want to count the time together, I just want to see how many seconds the device was in use. In my case, about 45 seconds. Time is reset every new run.

Still looking for help.

Maybe not the best coding in the world and could probably be made simpler it works and it is what I used to get the last running time for our sewage pump in seconds. Two stage operation. One automation records the time when the pump starts and store it to an input_number. The second automation the calculate the time when the pump stops.

- id: "pumpe_starter"
  alias: "Pumpe_starter"      
    - platform: state
      entity_id: switch.pumpealarm_switch_1
      from: "off"
      to: "on"  
    - service: input_datetime.set_datetime
      entity_id: input_datetime.pumpe_paa_tidpunkt
        datetime: '{{ (now().strftime("%s")|int(default=0) ) |timestamp_custom("%Y-%m-%d %H:%M:%S", false,default=0) }}'   
- id: "pumpetid"
  alias: "pumpetid"
    - platform: state
      entity_id: switch.pumpealarm_switch_1
      from: "on"
      to: "off"
    - service: input_number.set_value
        entity_id: "input_number.pumpetid"
        value: "{{ (now().timestamp()- as_timestamp(states.input_datetime.pumpe_paa_tidpunkt.state,0))| timestamp_custom('%s', false,default = 0)}}"      

Create a binary sensor so you have an “on”/“off” to trigger off of…
Once that is done, use that binary sensor in a trigger-based template sensor:

  - trigger:
      - platform: state
        to: 'off'
        from: 'on'
        entity_id: binary_sensor.pump_running
        id: 'off'
      - platform: state
        to: 'on'
        from: 'off'
        entity_id: binary_sensor.pump_running
        id: 'on'
      - name: Pump Previous Runtime
        state: |
          {{ 0 if == 'on' else 
          (trigger.to_state.last_changed - trigger.from_state.last_changed).total_seconds() }}

Thank you, but I’m I’m struggling how make it works. Your code copy and paste in template.yaml? The what?
Thank you for your patience

Step 1:

Did you create the binary sensor?

Step 2:
Paste the configuration from my earlier post into your configuration. Make sure the entity ID used as the trigger for Template sensor matches the entity you created in Step 1.

Depending on how you are merging your template.yaml file, you may need to remove the template: key… you have not shared that information, so I can’t tell you exactly what is necessary in your case. Please review Splitting the configuration for information on merging and the use of the !include tag.

Step 3:
Reload Template Entities or Restart Home Assistant.

1 Like

Thank you, for your effort.
Sensor now working, but time isn’t correct, sensor says 59 ~ 60s, but my measuring with stopwatch ist 30~31s.
So I made rule in tasmota plug with polling mqtt push when power goes above 100w and second with power goes to 0w, changed template sensor and now is time of sensor around 39s.
So one more time thanks you.