Using "for" in a condition

Hey community,

I am currently trying to check if any power is being drawn at all (charger) when activating a switch. If no power is drawn within 5 minutes, the switch should be turned off again.
While it is possible to set a for condition in triggers (power is below 0.5 W for 5 minutes), this does not work for conditions.

This is working:

alias: 'Smartwatch: Prüfe Ladung beim Einschalten'
description: ''
trigger:
  - platform: device
    type: turned_on
    device_id: 181e3c909c3abfd5c3b21ef64c0b2a88
    entity_id: switch.workingroom_smartwatchdocking
    domain: switch
condition:
  - condition: state
    entity_id: input_boolean.automation_smartwatch_docking
    state: 'on'
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - condition: numeric_state
    entity_id: sensor.workingroom_smartwatchdocking_power
    below: '0.5'
  - type: turn_off
    device_id: 181e3c909c3abfd5c3b21ef64c0b2a88
    entity_id: switch.workingroom_smartwatchdocking
    domain: switch
mode: single

But I need it like this:

alias: 'Smartwatch: Prüfe Ladung beim Einschalten'
description: ''
trigger:
  - platform: device
    type: turned_on
    device_id: 181e3c909c3abfd5c3b21ef64c0b2a88
    entity_id: switch.workingroom_smartwatchdocking
    domain: switch
condition:
  - condition: state
    entity_id: input_boolean.automation_smartwatch_docking
    state: 'on'
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - condition: numeric_state
    entity_id: sensor.workingroom_smartwatchdocking_power
    below: '0.5'
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - type: turn_off
    device_id: 181e3c909c3abfd5c3b21ef64c0b2a88
    entity_id: switch.workingroom_smartwatchdocking
    domain: switch
mode: single

Do you know other possibilities?

Thanks in advance!

This will work if the value doesnt keep changing, e.g. if you are testing for exactly 0.0W. If it keeps changing this wont work.

    - condition: template
      value_template: "{{ (states('sensor.workingroom_smartwatchdocking_power') | float < 0.5) and (as_timestamp(now(),0) | float(default=0) - (as_timestamp(states.sensor.workingroom_smartwatchdocking_power.last_changed,0) | float(default=0) )) > 300.0 }}"
1 Like

That is working, thank you!

Unfortunately, after implementing that, my other automation is not working anymore. That other automation should turn of the switch, when the device is fully charged.

alias: 'Smartwatch: Ausschalten wenn voll'
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.workingroom_smartwatchdocking_power
    for:
      hours: 0
      minutes: 5
      seconds: 0
    below: '0.5'
condition:
  - condition: device
    type: is_on
    device_id: 181e3c909c3abfd5c3b21ef64c0b2a88
    entity_id: switch.workingroom_smartwatchdocking
    domain: switch
  - condition: state
    entity_id: input_boolean.automation_smartwatch_docking
    state: 'on'
action:
  - type: turn_off
    device_id: 181e3c909c3abfd5c3b21ef64c0b2a88
    entity_id: switch.workingroom_smartwatchdocking
    domain: switch
mode: single

Do you have any idea, why this rule is not triggered anymore?

Have you checked the trace for the automation to see it it is triggered but the conditions fail, or if it is not triggered at all? And have you checked the history graphs to see of during the 5 mins the power never isis above or equal to0.5W? any fluctuations above might throw this off, and 0.5 W is not that much.