Automation with timeframe that starts before 00:00 and ends after

Hi,
I have an automation to turn off my heater within a certain timeframe.
When the timeframe stays before 00:00h it works perfectly, but when it crosses the 00:00h line it fails.
I now have 2 different automations; one from 22:00 till 23:59 and one from 00:00 till 05:00.
Is there a way to overcome this ?
My automation for the whole period (wich is not working) :

alias: 'Verwarming hk snachts uit'
initial_state: 'on'
trigger:
  - platform: homeassistant
    event: start

    # voorwaarden in het tijdvak van 22.00u tot 05.00u
  - platform: template
    value_template: "{{ states('sensor.time') == '22:00' }}"
  - platform: template
    value_template: "{{ states('sensor.time') == '05:00' }}"

# indien de instelling handmatig wordt veranderd wordt de automation opnieuw getriggerd na 30 seconden
  - platform: state
    entity_id: climate.huiskamer
    from: "off"
    for: 
      seconds: '30'

action:
  - service_template: >
      {% if '22:00' <= states('sensor.time') < '05:00' %}
        climate.turn_off
      {% else %}
        climate.turn_on
      {% endif %}
    entity_id: climate.huiskamer

Time conditions can span midnight, and you can use conditions in a choose action.

alias: 'Verwarming hk snachts uit'
initial_state: 'on'
trigger:
  - platform: homeassistant
    event: start
  - platform: time
    at: '22:00'
  - platform: time
    at: '05:00'
  - platform: state
    entity_id: climate.huiskamer
    from: "off"
    for: 
      seconds: '30'
action:
  - choose:
      - conditions:
          - condition: time
            after: '22:00'
            before: '05:00'
        sequence:
          - service: climate.turn_off
            entity_id: climate.huiskamer
    default:
      - service: climate.turn_on
        entity_id: climate.huiskamer

The default action will occur if nothing is chosen.

2 Likes

Hi Tom,
Thank you for your reply.
I never used the ‘choose’ option (it’s new to me as noob)
I’m gonna test the automation.
It looks much more simple and usable as I can set the defaultoption, not only to turn the climate on but, also to set the hvac mode.
If it works I will mark your reply as the solution. Thanks again!

1 Like

Thank you!
This is so much better.

1 Like