Best practices simple automation

Hello,

First of all, thanks to the community for this great tool and the vast information you provide.

I’m fairly new to HA. I’ve set up a simple automation for the heating of my kids room, but I’m unsure if there is a better way to do it. Currently I’ve two automations (on/off) to turn a switch on/off based on a temperature sensor as follows,

- id: Calefaccion_on
  alias: Calefaccion On
  trigger:
    - platform: time_pattern 
      minutes: '/15'
  condition:
    condition: and
    conditions:
      - condition: time
        after: '22:45:00'
        before: '07:30:00'
      - condition: numeric_state
        entity_id: sensor.temperature_1
        below: 21
  action:
    - service: switch.turn_on
      entity_id: switch.calefaccion_1

- id: Calefaccion_off
  alias: Calefaccion Off
  trigger:
    - platform: time_pattern 
      minutes: '/15'
  condition:
    condition: and
    conditions:
      - condition: time
        after: '23:00:00'
        before: '07:30:00'
      - condition: numeric_state
        entity_id: sensor.temperature_1
        above: 22
  action:
    - service: switch.turn_off
      entity_id: switch.calefaccion_1

So, it checks every 15 minutes whether the temperature is above or below the limit, during night time. To me it looks like a bit redundant. Would it be better just to use the numeric_state as the trigger? Is there a better way to do it?

Thank you.

Yes it would, but with a couple of extra triggers:

- id: Calefaccion_on
  alias: Calefaccion On
  trigger:
    - platform: numeric_state
      entity_id: sensor.temperature_1
      below: 21
   - platform: time
     at: '22:45:01'
  condition:
    - condition: time
      after: '22:45:00'
      before: '07:30:00'  
  action:
    - service: switch.turn_on
      entity_id: switch.calefaccion_1

Note the second time trigger in the on automation to ensure the heater turns on if it is already below 21° when the time window starts.

- id: Calefaccion_off
  alias: Calefaccion Off
  trigger:
    - platform: numeric_state
      entity_id: sensor.temperature_1
      above: 22
   - platform: time
     at: '07:29:58'
  condition:
    - condition: time
      after: '23:00:00'
      before: '07:30:00'
  action:
    - service: switch.turn_off
      entity_id: switch.calefaccion_1

Note the second time trigger in the off automation to ensure the heater turns off at 07:30, even if it is above 22°.

Thank you @tom_l. Does that mean that both triggers have to be true?

No, multiple triggers are OR.

When any of the automation’s triggers becomes true (trigger fires ), Home Assistant will validate the conditions, if any, and call the action.

Automation Trigger - Home Assistant

When your feeling brave take a look at the generic thermostat component in HA (https://www.home-assistant.io/integrations/generic_thermostat/)

It shows just like any thermostat control in your lovelace dashboard… you can also control it with google home speakers if you have that all set up too. Essentially it’d save you running the automation with a static temperature to work to

1 Like

How about

- id: Calefaccion_Control
  alias: Calefaccion Control
  trigger:
    - platform: numeric_state
      entity_id: sensor.temperature_1
      below: 21
    - platform: numeric_state
      entity_id: sensor.temperature_1
      above: 22
  condition:
    - condition: or
      conditions:
      - condition: time
        after: '23:00:00'
        before: '07:30:00'
      - condition: numeric_state
        above: 22
  action:
    - service: >-
        switch.turn_{{ 'on' if states('sensor.temperature_1') < 21 else 'off'}} 
      entity_id: switch.calefaccion_1
  • will switch on the heater during the night, if required
  • will switch off the heater anytime, if temperatur is above threshold.

Not if the the temperature is already below 21 when 11pm comes around. That is the reason for my extra triggers.

You are correct.

Much better to set up a generic thermostat, then have an automation set the target temp depending on time

3 Likes

Thank you all for your suggestions.
@tom_l, just to clarify, the first automation turns on the heater at the trigger time regardless the current temp, and if it is above threshold the second automation turns it off?

@samnewman86 Just for fun I’m trying your suggestion and it seems to work just fine.

I will try both and decide which one I keep as solution.
Thank you all!

Oh yeah. Crap. You need another condition to prevent that happening:

- id: Calefaccion_on
  alias: Calefaccion On
  trigger:
    - platform: numeric_state
      entity_id: sensor.temperature_1
      below: 21
   - platform: time
     at: '22:45:01'
  condition:
    condition: and
    conditions:
      - condition: time
        after: '22:45:00'
        before: '07:30:00'
      - condition: numeric_state
        entity_id: sensor.temperature_1
        below: 21
  action:
    - service: switch.turn_on
      entity_id: switch.calefaccion_1

TBH I reckon the scheduled generic thermostat is the more elegant solution. Nicer interface for adjusting the set-point too (thermostat card).

Also much safer.