Help for conditional span between due days

At the moment I have this automation:

- id: HEATER_OFF_20210107204600
  alias: TERMOSIFONI - Spegnimento
  trigger:
  - minutes: /1
    platform: time_pattern
  condition:
    condition: and
    conditions:
      - condition: or
        conditions:
          - condition: time
            after: '09:00:00'
            before: '16:00:00'
          - condition: time
            after: '22:00:00'
            before: '06:00:00'
      - condition: state
        entity_id: input_boolean.poweron_bypass_riscaldamento
        state: "off"
  action:
    - service: script.set_heater_off

It powers off the climate if an input boolean is off AND if current time is between 09-16 OR 22:00-06.

In effect my climate starts at 06:00, stop at 09:00, starts at 16 and end at 22.

It works very well, without no troubles with time spanning between 2 days (after 22 and before 06), but I would improve it, using 4 input datetime:

input_datetime:
  start_zone_1_heater: # 06:00
    has_date: false
    has_time: true
  end_zone_1_heater: # 09:00
    has_date: false
    has_time: true
  start_zone_2_heater: # 16:00
    has_date: false
    has_time: true
  end_zone_2_heater: # 22:00
    has_date: false
    has_time: true

I have trouble with the construction of the conditional value template, 'cause I have a bit of trouble with time spanning in 2 days.

I wrote this template

{{ ((states('sensor.time') >
     (states.input_datetime.end_zone_1_heater.attributes.timestamp | int | timestamp_custom('%H:%M', False)))
      and (states('sensor.time') <
     (states.input_datetime.start_zone_2_heater.attributes.timestamp | int | timestamp_custom('%H:%M', False))))
     or
     ((states('sensor.time') >
     (states.input_datetime.end_zone_2_heater.attributes.timestamp | int | timestamp_custom('%H:%M', False)))
      and (states('sensor.time') <
     (states.input_datetime.start_zone_1_heater.attributes.timestamp | int | timestamp_custom('%H:%M', False)))) }}

In effect, at 23:00 I think condition time cannot be evaluated at true, because 23 is greater than end_zone_2 (22), but it NOT smaller than 06:00.

So, my automation could be a bit of…

- id: HEATER_OFF_20210107204600
  alias: TERMOSIFONI - Spegnimento
  trigger:
  	- platform: homeassistant
      event: start
    - platform: template
      value_template: "{{ states('sensor.time') == state_attr('input_datetime.end_zone_1_heater', 'timestamp') | int | timestamp_custom('%H:%M', False) }}"
    - platform: template
      value_template: "{{ states('sensor.time') == state_attr('input_datetime.end_zone_2_heater', 'timestamp') | int | timestamp_custom('%H:%M', False) }}"
  condition:
    condition: and
    conditions:
      - condition: or
        conditions:
          - condition: template
            value_template: >
              {{ ((states('sensor.time') >
			   (states.input_datetime.end_zone_1_heater.attributes.timestamp | int | timestamp_custom('%H:%M', False)))
			    and (states('sensor.time') <
			   (states.input_datetime.start_zone_2_heater.attributes.timestamp | int | timestamp_custom('%H:%M', False))))
			    or
			   ((states('sensor.time') >
			   (states.input_datetime.end_zone_2_heater.attributes.timestamp | int | timestamp_custom('%H:%M', False)))
			    and (states('sensor.time') <
			   (states.input_datetime.start_zone_1_heater.attributes.timestamp | int | timestamp_custom('%H:%M', False)))) }}
      - condition: state
        entity_id: input_boolean.poweron_bypass_riscaldamento
        state: "off"
  action:
    - service: script.set_heater_off

So, at the end, my final question is… Can my automation evaluted as true if I restart HomeAssistant at 23:14, for example?

I’m chaning my approach and I want use the before and after condition, but I cannot use a string generated from input_datetime.

These are my approaches

- id: HEATER_OFF_20210107204600
  alias: TERMOSIFONI - Spegnimento
  trigger:
  - minutes: /1
    platform: time_pattern
  condition:
    condition: and
    conditions:
      - condition: or
        conditions:
          - condition: time
            after: '09:00:00'
            #after: "'{{ strptime(states('input_datetime.end_zone_1_heater'), '%H%M') }}'"
            before: '16:00:00'
            #before: "'{{ strptime(states('input_datetime.start_zone_2_heater'), '%H%M') }}'"
          - condition: time
            after: '22:00:00'
            #after: "'{{ strptime(states('input_datetime.end_zone_2_heater'), '%H%M') }}'"
            before: '06:00:00'
            #before: "'{{ strptime(states('input_datetime.start_zone_1_heater'), '%H%M') }}'"
            #before: '{{ state_attr('input_datetime.start_zone_1_heater', 'timestamp') | int | timestamp_custom('%H:%M:%S', False) }}'
      - condition: state
        entity_id: input_boolean.poweron_bypass_riscaldamento
        state: "off"
  action:
    - service: script.set_heater_off

But I got several errors, when I enable them

while parsing a block mapping in "/config/packages/heater.yaml", line 142, column 13 expected <block end>, but found '<scalar>' in "/config/packages/heater.yaml", line 147, column 37

Or

Invalid config for [automation]: Invalid time specified: '{{ state_attr('input_datetime.start_zone_1_heater', 'timestamp') | int | timestamp_custom('%H:%M:%S', False) }}' for dictionary value @ data['condition'][0]['conditions'][0]['conditions'][1]['before']. Got None. (See ?, line ?).

How can I get that strings from the input_datetime?

Thank you to all

Maybe I missed some important point but the example you posted seems like a complicated way to control a heater according to a schedule.

For example, this turns on a heater switch at 06:00 and 16:00 and turns it off at 09:00 and 22:00 (two heating periods: 6-9 and 16-22).

- alias: Heater Control
  trigger:
  - platform: time
    at:
    - 06:00:00
    - 09:00:00
    - 16:00:00
    - 22:00:00
  action:
  - choose:
    - conditions: '{{ trigger.now.hour == 6 or trigger.now.hour == 16 }}'
      sequence:
      - service: switch.turn_on
        entity_id: switch.heater
    default:
    - service: switch.turn_off
      entity_id: switch.heater

I really appreciate your help and your point of view, but I prefer use my logic/automation (because I have so many other automation linked to)…

Do you know how can I use the string from input_datetime instead of hardcoded?

You can’t use a template with a Time Condition’s after and before options. You can only use a hard-coded time string or an input_datetime.

Good luck!

Instead, following your link… seems really easy :astonished:

condition:
  condition: time
  after: input_datetime.house_silent_hours_start
  before: input_datetime.house_silent_hours_end

That’s what I said:

but you can’t use a template.

Better for me… Using directly the input_datetime without the template… Sorry, I didn’t read very well your answer :frowning: