How to loop a function in automations

Hello everyone…

We have a stupid heatwave right now and my house is getting cooked…
So i made an automation, that turns the ac on when a certain temp is reached…
Unfortunately, the Mini split dont turn off when temp is reached…they just idle along with the fan…
So i was wondering how i could have my thermometer watch the temp, and when it gets to warm, turn the ac on and when the temp falls below a certain temp, to shut it back off and repeat in case the temp rises again to the to hot limit.
Here my code with the just turn on when reached hot limit…

alias: AC_Keep_Bedroom_fresh
description: Turn on when room over 27C
triggers:
  - type: temperature
    device_id: 569bdd265a4b0227c03b6f6b8a166f74
    entity_id: sensor.ble_temperature_schlafzimmer_a4c13888d685
    domain: sensor
    for:
      hours: 0
      minutes: 0
      seconds: 30
    above: 26
    trigger: device
conditions:
  - condition: time
    after: "08:00:00"
    before: "21:00:00"
    weekday:
      - sun
      - sat
      - fri
      - thu
      - wed
      - tue
      - mon
  - condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.window_sensor_bedroom_door_left_open
        state: "off"
        attribute: device_class
        for:
          hours: 0
          minutes: 2
          seconds: 0
actions:
  - data:
      hvac_mode: cool
      temperature: 23
    target:
      device_id:
        - fee953f90f472fc2436487244b3ad1e2
    action: climate.set_temperature
  - data: {}
    target:
      device_id:
        - fee953f90f472fc2436487244b3ad1e2
    action: climate.turn_on
  - data:
      message: Bedroom AC had to be turned on due to excessive heat
      title: Keep Bedroom fresh
    action: notify.notificationgroup
mode: queued
max: 10

This is using the HASS thermostat that is coming with the midea clones…
I would be also happy to use a different thermostat when it does something like that…
Happy for any input and thx in advance already

Hello Darkyputz,

The super simple way is to make an almost identical automation, but this one turns it off when the temp is below 22 (or whatever) for 30 seconds…

You could get fancier, but as long as the 2 temps used for triggers are not too close where it might start short cycling the unit, then you are golden.

1 Like

Hello and thx for the fast reply…

I havent thought of that…i was so focused to have it in a single automation (for not losing oversight)…will try that one…thx

Start there, then tackle pulling it into a single automation after that is working.
The single automation would have a couple of triggers probably using trigger_id and then a choose statement to select which, but start simple. The code logic and maybe the code will be transferable if/when you want to try it. Just start simple, and work toward elegant…

HA automations are closer to event driven programming than procedural programming so that the core doesn’t get blocked waiting.

Many tasks do get solved in pairs of logic - trigger for ON (PIR motion), and trigger for OFF (no motion for 5mins) - HA core can track which automations want to know when something changes state, rather than run all of the code at once.

Tip Name paired ON/OFF automations the same with different suffixes so they sort together in lists,

More complex logic (even state machines) are possible with Helpers acting a bit like variables, and individual automations triggering on state changes, but I’ve never personally had to use the looping actions which include the usual suspects.

If this helps, :heart: this post!

This condition will never work… off is not a valid value for device_class.


FWIW, make sure you are including appropriate duration requirements in your triggers and/or conditions, short-cycling your AC’s compressor is a recipe for it dying early.

Thx for the hint…
i changed it (taking device class out)

Also duplicated the “ON” logic and created an additional “off” automation.
Works so far…still testing