Duration of Condition in Automation

Gentlemen, plz help to set the duration of Condition in sequence in my automation.
Basic scenario - if Humidity Sensor stays below/above particular figure during a set time (I need to point 1 minute, for example), Action should be executed (OFF/ON correspondingly).
Here is my working example, where all conditions are set, but I don’t see any option for the duration.

alias: 'Towel Dryer:  HostBath Shelly ON / OFF'
description: 'Turns ON Towel Dryer if humidity is high '
trigger:
  - platform: state
    entity_id: sensor.sh_khoz_su_vlazhnost
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.sh_khoz_su_vlazhnost
            above: '67'
        sequence:
          - service: switch.turn_on
            data: {}
            entity_id: switch.sh_khoz_su
      - conditions:
          - condition: numeric_state
            entity_id: sensor.sh_khoz_su_vlazhnost
            below: '57'
        sequence:
          - service: switch.turn_off
            data: {}
            entity_id: switch.sh_khoz_su
    default: []
mode: single

I would use a template condition which I believe allows the duration to be set:

action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ states('sensor.sh_khoz_su_vlazhnost') | int > 67 }}"
            for:
              minutes: 1
        sequence:
          - service: switch.turn_on
            data: {}
            entity_id: switch.sh_khoz_su
      - conditions:
          - condition: template
            value_template: "{{ states('sensor.sh_khoz_su_vlazhnost') | int < 57 }}"
            for:
              minutes: 1
        sequence:
          - service: switch.turn_off
            data: {}
            entity_id: switch.sh_khoz_su

Thanks for your proposal, but “for:” condition somehow is not treated as correct in case I use it in action.

Your suggested variant results in:

Message malformed: extra keys not allowed @ data['action'][0]['choose'][0]['conditions'][0]['for']

I think you can only use ‘for’ in a trigger as otherwise you’d need to have a full database history on all entities.
If you can’t move the ‘for’ bits into a trigger, I’d create a binary sensor for each condition or combination in a choose.
Please educate me if I’m wrong
:+1:

I think the best option is to return to 2 automations (ON and OFF) with corresponding condition options.
I used that before, but then decided to utilize one automation instead of separate two.

I have noticed a strong preference to combine automations, this is only necessary when it makes sense to do so.
Most times, because it’s easier to maintain, better to read and you understand it better - stick with the two.

Don’t get me wrong, it’s good to experiment, but keep backups of anything you are playing with so you can revert.

That’s my $0.02 anyway

well, that sucks.

I could have sworn I’d seen a post (maybe by pnbruckner who had done some updating of some things a while back) that said that the for: had been added to all templates. It must have been in relation to just the template trigger.

Well, guys, optimization of automation is a sort of sporting event :slight_smile:
It’s like any programming - you feel you did the perfect job when the code is concise!
Thanks for your participation, I am reverting to 2 automations.

This should meet your requirements:

alias: 'Towel Dryer:  HostBath Shelly ON / OFF'
description: 'Turns ON/OFF Towel Dryer depending on humidity level '
trigger:
  - platform: state
    entity_id: sensor.sh_khoz_su_vlazhnost
condition: []
action:
  - variables:
      humidity: '{{ trigger.to_state.state | int }}'
  - condition: template
    value_template: '{{ humidity > 67 or humidity < 57 }}'
  - service: "switch.turn_{{ 'on' if humidity > 67 else 'off' }}"
    entity_id: switch.sh_khoz_su
mode: single
  • The condition within the action confirms the humidity is either greater than 67 or less than 57. If it’s not then the action stops and does not proceed to the service call.
  • The service call turns the switch on if humidity is above 67 otherwise it turns the switch off.

@123,

Thanks!
But where is time “1 minute” considered?
I was about to set timing: automation is intended for towel dryer and the fact that you take away towel periodically should not switch on/off the switch instantly.

Oops! That’s my mistake; I simply converted your original automation without taking into consideration the requested 1-minute enhancement. :man_facepalming:

Try this version:

alias: 'Towel Dryer:  HostBath Shelly ON / OFF'
description: 'Turns ON Towel Dryer if humidity is high '
trigger:
  - platform: numeric_state
    entity_id: sensor.sh_khoz_su_vlazhnost
    above: 67
    for: '00:01:00'
  - platform: numeric_state
    entity_id: sensor.sh_khoz_su_vlazhnost
    below: 57
    for: '00:01:00'
condition: []
action:
  - service: "switch.turn_{{ 'on' if trigger.to_state.state | int > 67 else 'off' }}"
    entity_id: switch.sh_khoz_su
mode: single

@123

Thanks! Looks cool!
Does code else 'off' provide switch.turn_off for condition below: 57? Or should I denote explicitly smth like else if trigger.to_state.state | int < 57 ?
It looks neat, but at first glance not very understandable (if it switches off the device or not in case Humidity is between 57 & 67%)

Sorry, I got the point.
Else is executed along with trigger that is valid in case of < 57.
Wonderful! Thank you!

1 Like