Wetting my nose with autmations: how to merge?

I am pretty new to automations in Home Assistant. I am a sw developer, and that seems to be making things more difficult actually :slight_smile:

I have a switch and a thermometer… I want the switch to turn on when the temperature is above 32°C, but only during between 8:30 - 21:00. Otherwise, the switch should be off.

I ended up creating three scripts.

One to turn on the switch above 32*C and betwen 8:30/21:00:

alias: Turn on switch
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.tm32_temperature_temperature
    for:
      hours: 0
      minutes: 5
      seconds: 0
    above: 32
conditions:
  - condition: time
    after: "08:30:00"
    before: "21:00:00"
actions:
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      device_id: 3dc45ca861d7fb6b1d8c09cfdb1db294
mode: single

Then one to turn off the switch if the temperature is below 30*C (2 degress hysteresys seems good):

alias: Turn off switch
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.tm32_temperature_temperature
    for:
      hours: 0
      minutes: 5
      seconds: 0
    below: 30
conditions: []
actions:
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      device_id: 3dc45ca861d7fb6b1d8c09cfdb1db294
mode: single

And finally, one to turn it off always after 21:00:

alias: turn off at night
description: ""
triggers:
  - trigger: time
    at: "21:00:00"
actions:
  - type: turn_off
    device_id: 3dc45ca861d7fb6b1d8c09cfdb1db294
    entity_id: 0e8f0b76e005503792b6d57fbefeb661
    domain: switch
mode: single

It seems to be working, but it feels like overkill. Three scripts for such a simple task?

What is a better way to in Home Assistant to address the issue? Can i mix the three scripts into one? Is there a better way?

Thank you guys.

I would use trigger ids as a condition with a choose action:

alias: Temperature Controlled Switch
description: "Turn switch on/off based on temperature and time constraints"
triggers:
  # Temperature goes above 32°C
  - trigger: numeric_state
    entity_id: sensor.tm32_temperature_temperature
    above: 32
    for:
      minutes: 5
    id: temp_high
  
  # Temperature goes below 30°C  
  - trigger: numeric_state
    entity_id: sensor.tm32_temperature_temperature
    below: 30
    for:
      minutes: 5
    id: temp_low
  
  # Time reaches 21:00 (turn off)
  - trigger: time
    at: "21:00:00"
    id: night_time
  
  # Time reaches 8:30 (evaluate if should turn on)
  - trigger: time
    at: "08:30:00"
    id: day_time

conditions: []

actions:
  - choose:
      # Case 1: Temperature high during allowed hours - turn ON
      - conditions:
          - condition: trigger
            id: temp_high
          - condition: time
            after: "08:30:00"
            before: "21:00:00"
        sequence:
          - action: switch.turn_on
            target:
              device_id: 3dc45ca861d7fb6b1d8c09cfdb1db294
      
      # Case 2: Temperature low OR night time - turn OFF
      - conditions:
          - or:
            - condition: trigger
              id: temp_low
            - condition: trigger
              id: night_time
        sequence:
          - action: switch.turn_off
            target:
              device_id: 3dc45ca861d7fb6b1d8c09cfdb1db294
      
      # Case 3: Morning time - check if should turn on
      - conditions:
          - condition: trigger
            id: day_time
          - condition: numeric_state
            entity_id: sensor.tm32_temperature_temperature
            above: 32
        sequence:
          - action: switch.turn_on
            target:
              device_id: 3dc45ca861d7fb6b1d8c09cfdb1db294

mode: single

By the way, I hate to be that guy, but this is what LLMs are really good for and I use them for… Praise HA for doing yaml right.

Brilliant!

thank you, i need to learn trigger id’s and the associated usage… I know that LLMs are good for this, but i prefer to learn it myself, i am a bit old fashioned :slight_smile:

1 Like

There’s another technique you might be interested in:

You’re right, though, it often doesn’t help to think of automations and scripts as software - they’re more like rules. When this happens, do that. A large collection of separate automations may seem “inelegant” but it can be much easier to maintain.

Or just use the generic thermostat and a simple timed automation to change the climate to on or off or any mode you wish to set.

Agree with generic thermostat.
All you need then is an automation like the one kotrfa posted with thermostat on and off at the times.

This is also a good suggestion!

i will stick with the automation with the triggers because i think it’s more explictive of the logic behind it, and the custom thermostat will still require an automation.

But the termostat has a lot of logic that will keep it running when the automation does not.
For instance a reload or reboot will make your only fan (I assume it’s a fan) stay on or off if the temperature crosses the threshold at the same time.

The thermostat will know it’s out of bounds and turn on/off.
And has a much easier interface to use if you wish to use it manually or set a new temperature range