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.
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.
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.
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:
template:
- 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'
sensor:
- name: Pump Previous Runtime
state: |
{{ 0 if trigger.id == 'on' else
(trigger.to_state.last_changed - trigger.from_state.last_changed).total_seconds() }}
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.
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.