Switch off after x minutes

So I have a couple of pumps on automation that switch on and off after a specified duration.

The switches are all Tuya devices and I am using LocalTuya. LocalTuya seems to be having some issues as some switches are constantly cycling between unavailable and available.

One of my pumps became unavailable a couple of seconds before it was supposed to switch off and it was on till the next pump automation time kicked in.

I want a master kill automation that switches the pump off if it’s on for beyond x minutes.

This is the automation I have created, any inputs are welcome

alias: 5x5 pump kill switch
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: 
    entity_id: switch.5x5_pump
    domain: switch
    for:
      hours: 0
      minutes: 3
      seconds: 0
condition: []
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.5x5_pump
mode: single

This automation waits 3 minutes after switching on the pump to switch it off again, even if the pump is no longer available.

alias: 5x5 pump kill switch
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.5x5_pump
    to: "on"
condition: []
action:
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.5x5_pump
mode: single

Thanks!

I’ll try this out.

Will this work in case the device is unavailable at the time it’s required to be switched off?

When a device become unavailable, you can’t switch it on or off, because it is… unavailable. Right? :thinking:

Yes you’re right!

I’m sorry, I meant to say, the device cycles every 30 secs between available and unavailable. In case the off call coincides with the unavailable time, will it reset the counter for 3 mins?

When the switch turned on, the automation wait for 3 mins to switch the switch off again.

What you can try to do is, when the device toggles between available and unavailable, add “unavailable” to the “to:” state.
Every time when the switch goes to “on” or “unavailable” the automation will start and will wait 3 mins before it switched off the switch.

Thus, in case the off call coincides with the unavailable time, it will restart the counter for 3 mins.

Do not forget to change mode: in “restart”

alias: 5x5 pump kill switch
description: ""
trigger:
  - platform: state
    entity_id: switch.5x5_pump
    to:
      - unavailable
      - 'on'
condition: []
action:
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.5x5_pump
mode: restart

I do not know if the automation will work, because I didn’t test it.

Thanks!

I’ll check it out!

You could also implement a wait, which waits for 1 minute if the pump is unavailable and continue if it gets on again.
Not sure about the on in the wait template, you have to look which state it has when it is available.

alias: 5x5 pump kill switch
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: 
    entity_id: switch.5x5_pump
    domain: switch
    for:
      hours: 0
      minutes: 3
      seconds: 0
condition: []
action:
  - wait_template: "{{ is_state('switch.5x5_pump', 'on') }}"
    timeout: '00:01:00'
    continue_on_timeout: 'false'

  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.5x5_pump
mode: queued

1 Like