Ensuring a device turns on

So I have a heat pump, which is controllable via mqtt which is nice. It also reports it’s state on the mqtt as well, which I’m able to read and I have an indicator on the dashboard showing whether it’s running or not

I have a time trigger to turn it in at 5:30am and off at 8am - and once in a blue moon it’ll fail for a reason I’m not sure I’ll ever be able to ascertain. The aircon widget thinks it’s going, the state indicator knows it’s not.

Is there a way to have a trigger or automation that ensures it’s on after I’ve asked it to be on?

There are a few ways round it.

  1. quick and dirty: issue the command twice
  2. Quick and less dirty: issue the command, wait a few sec, have a condition that checks the sensor status and if no the right status issue the command again
  3. something a bit more involved like a loop that will issue the command, wait, check the status rerun the loop if wrong status or exit the loop if correct status

Hope I’ve give you enough ideas to get creative :slight_smile:

Automations can have loops?!

Back to the documentation I guess :slight_smile:

he he no you can’t have a loop in an automation, hence me saying “something a bit more involved” :wink:
You can achieve a loop by having 2 scripts calling each other.
Here is an example that I use:

flash_aqara_light:
  alias: 'Flash Lights When Alarm Armed'
  sequence:
    - condition: template
      value_template: '{{ states("alarm_control_panel.alarm").split("_")[0] == "pending"}}'
    - service: light.turn_on
      data:
        entity_id: light.aqara_gateway_light
        brightness: 255
        rgb_color: [255,0,0]
    - delay:
        seconds: 0.5
    - service: light.turn_off
      entity_id: light.aqara_gateway_light
    - service: script.turn_on
      data:
        entity_id: script.flash_loop

flash_loop:
  alias: 'Flash loop'
  sequence:
    - delay:
        seconds: 2
    - service: script.turn_on
      data:
        entity_id: script.flash_aqara_light

That’s ingenious!

I wonder if you could call yourself - recurse until the condition is met. Like, check if the device is already on and if not attempt to turn on the device then wait a minute then call yourself.

Every time under normal conditions, the first check will always fail, causing the script to tell the device to turn on and call itself again. Then the check will succeed and the script unwinds.

If the device is already reporting itself as on, the script won’t even attempt to turn it on.

I’ll need two exit conditions. Either a retry count or a time exit condition, so it doesn’t run forever if the device won’t turn on for some reason.

that’s pretty much it. The key here is the condition in the first script. When that condition is false, the script stops.
So replace it with your sensor showing the device is not on so the one command is issued again

Welp I’m off to bed but I’ll report back in a couple days with my results :slight_smile:

You can have loops in automation. It’s called “repeat”. It’s available via UI and via YAML as well. Leaving this for someone who’s searching for this next :slight_smile: Here’s an example of my script for auto watering.

alias: Auto water
description: ""
triggers:
  - trigger: time_pattern
    minutes: "10"
    seconds: "0"
    hours: "22"
conditions: []
actions:
  - sequence:
      - repeat:
          sequence:
            - sequence:
                - type: turn_on
                  device_id: 40cccff92e94da698f713c2ef99c6f71
                  entity_id: ab837a3674057dedb8509cbd26b44249
                  domain: switch
                - delay:
                    hours: 0
                    minutes: 7
                    seconds: 0
                    milliseconds: 0
          until:
            - condition: device
              type: is_on
              device_id: 40cccff92e94da698f713c2ef99c6f71
              entity_id: ab837a3674057dedb8509cbd26b44249
              domain: switch
      - delay:
          hours: 0
          minutes: 0
          seconds: 10
          milliseconds: 0
      - repeat:
          sequence:
            - sequence:
                - type: turn_off
                  device_id: 40cccff92e94da698f713c2ef99c6f71
                  entity_id: ab837a3674057dedb8509cbd26b44249
                  domain: switch
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 30
                    milliseconds: 0
          until:
            - condition: device
              type: is_off
              device_id: 40cccff92e94da698f713c2ef99c6f71
              entity_id: ab837a3674057dedb8509cbd26b44249
              domain: switch
mode: single
1 Like

thanks for the reply, much appreciated! I’ll have a play and see if it works for me