Timer switch - need help with datetime component

Hello,
I’m trying to build a sort of timed switch/relay (Zeitschaltuhr in german) depending on some sensors.
The time should be easily changeable in the frontend inside of HA, therefor my idea was to use the rather new datetime component.
Now my question is how to turn on/off the switch, if the current time (from HA) goes past one of those trigger times? I’ll probably have to use lambdas in the time component - maybe someone with more experience in this could help me.
Thanks

my code snippet:

time:
  - platform: homeassistant
    id: ha_time

datetime:
  - platform: template
    id: time_daylamp_on
    type: time
    name: "Heizlampe an"
    optimistic: yes
    initial_value: "09:00:00"
    restore_value: true
  - platform: template
    id: time_daylamp_off
    type: time
    name: "Heizlampe aus"
    optimistic: yes
    initial_value: "15:00:00"
    restore_value: true

I think you just use the on_time trigger:

time:
  - platform: homeassistant
    id: ha_time

datetime:
  - platform: template
    id: time_daylamp_on
    type: time
    name: "Heizlampe an"
    optimistic: yes
    initial_value: "09:00:00"
    restore_value: true
    time_id: ha_time
    on_time:
      then:
        - switch.turn_on: daylamp

  - platform: template
    id: time_daylamp_off
    type: time
    name: "Heizlampe aus"
    optimistic: yes
    initial_value: "15:00:00"
    restore_value: true
    time_id: ha_time
    on_time:
      then:
        - switch.turn_off: daylamp

This might probably be correct, but how do I refer/point to the value/timestamp stored in the datetime component?
My problem is mainly to find a working syntax

Edit: I saw you used on_time in datetime - this is not possible, its just possible in time

Then either something is broken or the docs are wrong:

image

This is my working code so far if someone want to take a look…
but I don’t want to recompile it if I need to change the time

# VARIABLES #################################
datetime:
  - platform: template
    id: time_lamp_on
    type: time
    name: "Lampe an"
    optimistic: yes
    initial_value: "09:00:00"
    restore_value: true
  - platform: template
    id: time_lamp_off
    type: time
    name: "Lampe aus"
    optimistic: yes
    initial_value: "15:00:00"
    restore_value: true

number:
  - platform: template
    name: "T_soll Tag"
    id: temp_thr_day
    optimistic: true
    restore_value: true
    initial_value: 30
    min_value: 20
    max_value: 40
    step: 1

# SENSOR ##################################
dallas:
  - pin: GPIO27
sensor:
  - platform: dallas
    address: 0xec63bd451f64ff28
    name: "Temperatur Tag"
    id: temp_day
    on_value: 
      then:
        - lambda: |-
            if (id(temp_day).state > id(temp_thr_day).state) {
              id(lamp).turn_off();
            }
        - lambda: |-
            if (id(temp_day).state < id(temp_thr_day).state && id(lamp_time).state) {
              id(lamp).turn_on();
            }        

time:
  - platform: homeassistant
    id: ha_time
    on_time:  # 9 to 15 o'clock
      - cron: '0 0 9 * * *'
        then: 
          if:
            condition:
              - switch.is_on: heating_mode
            then:
              - switch.turn_on: lamp_time
      - cron: '0 0 15 * * *'
        then:
          - switch.turn_off: lamp_time

switch:
  - platform: template
    name: "Heizbetrieb"
    id: heating_mode
    optimistic: true
    restore_mode: RESTORE_DEFAULT_ON
  - platform: template  # helper
    name: "Heizzeit Heizlampe"
    id: lamp_time
    #internal: True
    optimistic: true
    restore_mode: RESTORE_DEFAULT_OFF
    on_turn_on: 
      then:
        - lambda: |-
              if (id(temp_day).state < id(temp_thr_day).state) {
                id(lamp).turn_on();
              }
    on_turn_off: 
      then:
        - switch.turn_off: lamp

# OUTPUT #############################
  - platform: gpio
    pin: GPIO32
    name: "Heizlampe"
    id: lamp
    restore_mode: RESTORE_DEFAULT_OFF

I missed it on the first look, but your datetime entries are missing time_id… I’ll correct it in my post above.

this is in my case automatically set to ha_time because I only have one time component

I just tested it… compile fails if time_id isn’t set explicitly when using on_time. With explicit time_id it compiles and functions.

It works, thanks mate

But for my liking the documentation should be edited in this case, because the on_time mention is not really listed as an automation trigger at datetime (and also no mention that therefore time_id must be set)

Time_id is a required configuration setting and its explicitly stated as such in the documentation.

time_id (Required, ID): The ID of the time entity. Automatically set to the ID of a time component if only a single one is defined