Condition numeric_state above value for X minutes? Template instead?

Is there a way to program a condition to be true only when something like the following occurs please?

      - condition: numeric_state
        entity_id: sensor.plug_zigbee13_power
        above: 10
        for: "00:05:00"  

Numeric state doesn’t support “for”, and the “state” condition doesn’t support “above” or “below”.

I can then only assume this should be done via a template, but I can’t figure out how to integrate the “above 10 watts for longer than 5 mins” part of the code. Tried searching the forums and google but had no luck so far.

      - condition: template
        value_template: >
          {{ (states('sensor.plug_zigbee16_energy') | float > 10)  }}

The general plan is for it to look like the yaml below. The reason behind this is I’d like an automation that triggers when the tumble dryer has finished based on energy usage. However when the dryer finishes, it rotates the drum every one minute to stop the clothes creasing which keeps triggering the automation below. I therefore need a condition that checks if the power has been over 10W for at least the past 5 mins.

    trigger:
      - platform: state
        entity_id: sensor.plug_zigbee13_power
        to: '0'
        for: "00:00:20"
    condition:
      # power has been over 10W for more than 5 mins
      - condition: numeric_state
        entity_id: sensor.plug_zigbee13_power
        above: 10
        for: "00:05:00"    

I believe you should do it the other way around.
So use above 10 watt for 5 minutes as the trigger, and set a boolean dryer to on.

Then the next automation your automation above but the condition is that the boolean dryer is on.
Switch it to off and do the rest of the actions.

without checking the code logic, you can create a binary_sensor with a delay (on or off) to change state based on your preferences.

      - unique_id: driveway_buiten_motion_sensor_timed
        name: Driveway buiten motion sensor timed
        state: >
          {{is_state('binary_sensor.driveway_buiten_sensor_motion','on')}}
        delay_off:
          minutes: 2

or use even a template for the delay:

      - unique_id: parking_trash_buiten_sensor_motion_timed
        name: Parking trash buiten motion sensor timed
        state: >
          {{is_state('binary_sensor.parking_trash_buiten_sensor_motion','on')}}
        delay_off: >
          {{states('input_number.presence_timer')|int}}
        device_class: motion

You can then use that binary as trigger.

Hellis is right, you should check your logic. because when you say

is the definition of a trigger (occur) and not condition. Maybe it’s not meant like that, just saying you need to sort your triggers (a momentary state change) and conditions (a state at that time of trigger)

I was thinking something like this:

trigger:
  - platform: numeric_state
    entity_id: sensor.plug_zigbee13_power
    id: start
    for:
      minutes: 5
    above: '10'

  - platform: state
    entity_id: sensor.plug_zigbee13_power
    id: end
    to: '0'
    for:
      seconds: 20
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: start
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.dryer


      - conditions:
          - condition: trigger
            id: end
          - condition: state
            entity_id: input_boolean.dryer
            state: 'on'
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.dryer
          - service: tts.cloud_say
            data:
              entity_id: media_player.player
              message: Dryer is done.
    default: []

aha, great idea, thanks!

I’ve set my automations like this for now and will see how it works out:

  - alias: tumble_started
    id: tumble_started
    trigger:
      - platform: numeric_state
        entity_id: sensor.plug_zigbee13_power
        above: 10
        for: "00:00:30"
    action:
      - service: var.set
        data:
          entity_id:
            - var.tumble_started
          value_template: >
            {{ states('sensor.plug_zigbee13_energy') }}
      - service: input_boolean.turn_on
        entity_id: 
          - input_boolean.tumble_on
      - service: notify.signal_justme
        data:
          message: 'Tumble started'

  - alias: tumble_finished_boolean
    id: tumble_finished_boolean
    trigger:
      - platform: state
        entity_id: sensor.plug_zigbee13_power
        to: '0'
        for: "00:00:20"
    action:
      - service: input_boolean.turn_off
        entity_id: 
          - input_boolean.tumble_on

  - alias: tumble_finished
    id: tumble_finished
    trigger:
      - platform: state
        entity_id: sensor.plug_zigbee13_power
        to: '0'
        for: "00:00:45"
    condition:
      - condition: state
        entity_id: input_boolean.tumble_on
        state: 'off'      
    action:    
      - service: var.update
        data:
          entity_id: var.tumble_energy_difference
      - delay:
          seconds: 1
      - service: var.update
        data:
          entity_id: var.tumble_cost
      - service: notify.signal_justme
        data:
          message: "Tumble finsihed. Cost is £{{ states('var.tumble_cost') }}."

Just for reference, this is the power usage graph that I’m working with:

I like how you’ve combined the triggers. I’ll re-work mine to use the same method. Thank you!

I like this solution too, thank you @Mariusthvdb!

Here is the result of combining both your suggestions:

automation:
  - alias: tumble_automation
    id: tumble_automation
    trigger:
      - platform: state
        id: start
        entity_id: binary_sensor.tumble_on_off
        from: 'off'
        to: 'on'
        for: "00:00:30"
      - platform: state
        id: end
        entity_id: binary_sensor.tumble_on_off
        from: 'on'
        to: 'off'
    action:
      - choose:
          - conditions:
              - condition: trigger
                id: start
            sequence:
              - service: var.set
                data:
                  entity_id:
                    - var.tumble_started
                  value_template: >
                    {{ states('sensor.plug_zigbee13_energy') }}
              - service: notify.signal_justme
                data:
                  message: 'Tumble started'
          - conditions:
              - condition: trigger
                id: end
            sequence:
              - service: var.update
                data:
                  entity_id: var.tumble_energy_difference
              - delay:
                  seconds: 1
              - service: var.update
                data:
                  entity_id: var.tumble_cost
              - service: notify.signal_ha
                data:
                  message: "Tumble finsihed. Cost is £{{ states('var.tumble_cost') }}."

template:
  binary_sensor:
    - name: tumble_on_off
      delay_off:
        seconds: 25
      state: "{{ states('sensor.plug_zigbee13_power') | float(0) > 10.0 }}"

you now have a double delay?
One in the template binary_sensor, and one in the trigger (for: “00:00:30”) ?

I may be wrong in my understanding, but I thought the binary_sensor delay is for on → off, and the trigger delay is for off → on?

I haven’t been able to test this in anger yet (waiting for wife to wash something… )

You can just set the state of the entity to simulate the washing.

Right. Missed that … sorry :blush: