How to create a code, that measures for how long a device has been working, which triggers an action?

Hey everybody,

I recently created a counter, that tells me how often a device has been turned on (everything above 5.5W is considered as turned on). What I am trying to do now is to create a code, that increments another counter. In order for that to happen, the device has to be turned on ( bigger than 5.5 Watts) for more than 8 seconds and if that is the case, than the other counter gets incremented by one (initial = 0).

- id: '1673870444936'
  alias: New Automation
  description: ''
  trigger:
  - type: power
    platform: device
    device_id: cada4c50e7044fea3238a48e8f8bf81e
    entity_id: sensor.shellyplug_s_c8c9a3b8d590_power
    domain: sensor
    above: 5.5
    below: 100
    for: 
      hours: 0
      minutes: 0
      seconds: 8
  condition: []
  action:
  - service: counter.increment
    data: {}
    target:
      entity_id: counter.zahler_wasserspender
  mode: single

This is the code I used for the first counter, however I am not sure what I should do now. Should I create a timer or implement the device entity again, but this time just inserting the duration?
Thank you for your time in advance!

Cheers

The automation you show already does this. I.e., it only increments the counter if the power has been above 5.5 for at least 8 seconds.

1 Like

First of all, thanks for replying. The problem is, that it isn’t working at all. This was the reason I wanted to make a public post, since I might have been doing something wrong, which I am not aware of. If you say, that the code is right, then there has to be some other problem, I hope I can figure it out by myself. Thank you.