Retry to turn off switch after Automation if Switch Fails X times

Hi,
I am new and have done a little work on hassio.
Implementation so far:
Stage 1: Working Like a Charm:
I use NodeMCU with Relay board connected with Water Pump and created a switch to turn Pump ON and OFF with below code.

switch:
  - platform: mqtt
    name: 'Water Motor Top'
    state_topic: 'motor/top'
    command_topic: 'motor/top/set'
    payload_on: '1'
    payload_off: '0'
    retain: true
    icon: mdi:water-pump
    templates:
      rgb_color: "if (state === 'on') return [251, 210, 41]; else return [54. 95, 140];"

Note: Switch will change its state based on state_topic replyed by NodeMCU
Stage 2: Working so far:
I created a input_number and do automation that if switch turned on then turn it off after X minutes. I tested and it is working with below code.
configuration.yaml

input_number:
  water_motor_top_timeout_box:
    name: Water Motor Top Timeout (minutes)
    icon: mdi:alarm
    initial: 30
    min: 5
    max: 60
    step: 1
    mode: box

automations.yaml

  - alias: Motor Top On Timeout Auto
    trigger:
      platform: state
      entity_id: switch.water_motor_top
      from: 'off'
      to: 'on'
     # for:
      #  seconds: "{{ input_number.water_motor_top_timeout_box }}" # not working
      #  seconds: '10' # work with constant
    action:
      service: homeassistant.turn_on
      entity_id: script.water_motor_top_timer

scripts.yaml

  water_motor_top_timer:
    alias: Set Water Motor Top Timer
    sequence:
      # Cancel old timer
      - service: script.turn_off
        data:
          entity_id: script.turn_off_water_motor_top_timer
      # Start new Timer
      - service: script.turn_on
        data:
          entity_id: script.turn_off_water_motor_top_timer
  turn_off_water_motor_top_timer:
    alias: Turn off water motor top timer
    sequence:
      - delay: '00:{{ states.input_number.water_motor_top_timeout_box.state | int }}:00'
      - service: homeassistant.turn_off
        entity_id: switch.water_motor_top

Issue:
Now I was thinking what happens if this automation changes the state from ON to OFF but NodeMCU does not replay with “motor/top 0” topic. Home assistant will remain the Switch state ON because of no replay from NodeMCU in the response of automation. The motor pump will remain ON forever until the user goes to pump and manually turn it off or use hassio switch to turn it OFF manually.

Solution:
I want to automate if switch state remains unchanged even after timeout automation try again 3 times to turn pump OFF, if still the state is unchanged then Play some warning sound and display RED box with a warning in hassio gui. So User will see what happened.

How can I implement above solution, or experts suggest a better approach to this issue.
Thank you all.

Use a condition after the last service and check the state of the device you attempted to turn off.

  turn_off_water_motor_top_timer:
    alias: Turn off water motor top timer
    sequence:
      - delay: '00:{{ states.input_number.water_motor_top_timeout_box.state | int }}:00'
      - service: homeassistant.turn_off
        entity_id: switch.water_motor_top
      - condition: template
        value_template: "{{ is_state('switch.water_motor_top','on') }}"
       - service: homeassistant.turn_off
        entity_id: switch.water_motor_top
      - condition: template
        value_template: "{{ is_state('switch.water_motor_top','on') }}"
      - service: homeassistant.turn_off
        entity_id: switch.water_motor_top
      - condition: template
        value_template: "{{ is_state('switch.water_motor_top','on') }}"
      - service: homeassistant.turn_off
        entity_id: switch.water_motor_top

Add a delay in there if you like. This is probably the best way only because there is no “loop” ability in scripts currently. You can make a loop by daisy chaining 2 scripts and checking a counter, but it’s not worth it if you only want 3.

Also:

value_templates don’t work here because it’s not supported. I believe there is a request to make that work.

thank you so much for the answer. Its almost completed. I just bring up Google TTS to notify me about the situation.
Now only I want to show a RED Warning Box Popup to user on Hassio Webpage. How can I do this. Also can I send google tts audio to server page (means hassio.local:8123) webpage. So i can listen audio notification on my laptop chrome if I opend local server page or on a android smartphone browser.

Don’t know how to do that. May want to start a separate thread about it. Off the cuff, I don’t think HA can create pop ups like that.

1 Like

you can use this as a template for something similar to what you want. it doesn’t change the color but it will display a pop up box on every page of HA until you dismiss it.

Nice, gonna save this for later.

I think you need a comma after 54 not a full stop.

1 Like

nice catch… I will fix it… but strangely it is working fine…

great share thanks…