Automation and time

Hi, I have been doing some automations and my programing knowledge is lacking, I will share an example of an automation I’m done that is working, but that I’m not happy with.

alias: Climate timer
description: ''
trigger:
  - platform: time
    at: '22:00'
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: climate.molnet
            state: heating
            attribute: hvac_action
        sequence:
          - service: climate.set_hvac_mode
            target:
              entity_id: climate.molnet
            data:
              hvac_mode: 'off'
    default: []
  - delay:
      hours: 11
      minutes: 0
      seconds: 0
      milliseconds: 0
  - service: climate.set_hvac_mode
    target:
      entity_id: climate.molnet
    data:
      hvac_mode: heat
mode: single

What this does is to turn of the heater in my bedroom at 22:00 and then turn it back on at 09:00.

The problem I have with this, is that I want it to turn on at 09:00, and not 11:00 hours after it was turned off.

Before this automation, I had two, one for turning it off at 22:00 and one for turning it on at 09:00, that seemed stupid, to have two automations to control one object, so I came up with this new one, but I have not been able to set a firm time and have to now settle with this 11h timer, can anyone help me on how to solve this in a more elegant way?

alias: Climate timer
description: ''
trigger:
  - id: 'off'
    platform: time
    at: '22:00'
  - id: 'heat'
    platform: time
    at: '09:00'
condition: []
action:
  - service: climate.set_hvac_mode
    target:
      entity_id: climate.molnet
    data:
      hvac_mode: '{{ trigger.id }}'
mode: single

The main difference between this example and yours is that it doesn’t bother to check if the HVAC system is in the process of heating before turning it off (it simply turns it off regardless if the HVAC system is heating, idle, etc). I’m not sure why you needed that condition but if you still do then I can show you how to add it.

1 Like

Wow, this is new for me, I’ll try to learn this, but I guess this is exactly what I wanted to learn, thank you!

I had the check so that I would have a “condition”, an ugly way to solve my problem :slight_smile:

1 Like

I don’t know if you are aware of it or not but it’s best to avoid using a long delay in any automation. The reason is that the moment you execute Reload Automations, or restart Home Assistant, the delay is cancelled.

This behavior is not limited to delay and also applies to wait_template, a State Trigger employing for, wait_for_trigger, etc. Basically, anything that makes an automation “busy” doing its action is cancelled. That means your original automation would never get to turn on the thermostat if its 9-hour delay was cancelled.

If you use the Automation Editor, the moment you modify/create an automation and click the Save button, Home Assistant automatically executes Reload Automations.

Yes, I have run into that problem a few times, that is why I did not like my solution and wanted help to make a proper one.

This “trigger” based approach, is new for me, I need to look into it, I did a similar solution for an automation where I required a notification with an “action” in it, e.g. that the notification sent to my phone could activate or deactivate a switch, there I used triggers. But your solution was exactly what I was looking for but could not solve, I thought that there must be a away to trigger an automation on specific “times”, but did not find how to do it, untill now :slight_smile: