How can I define the period above midnight?

Hello all,

I would like to see if my automation becomes active when I activate a virtual switch.
To do this, a light should then light up in the dashboard.

Switch: input_boolean.alletueren

Lamp or sensor: binary_sensor.alletueren
Automation: automation.haustur_hausturschloss

Here is my attempt to solve this using the following template.

/config/packages/kellerlampe.yaml

template:
  - binary_sensor:
     - name: "haustueren"    
       unique_id: haustueren
       icon: mdi:coach-lamp   
       state: "{{ is_state('input_boolean.haustueren','on') and now().hour <= 22 and  now().hour >= 7 }}"
  - binary_sensor:
     - name: "alletueren"    
       unique_id: alletueren
       icon: mdi:coach-lamp   
       state: "{{ is_state('input_boolean.alletueren','on') and now().hour <= 7 and  now().hour >= 22 }}"

Question 1
name: "alletueren

How can I define the period above midnight?
Period should go from 10pm to the next day 7am.

Question 2
How can I input minutes e.g. 7:35am?
state: “{{ is_state(‘input_boolean.alletueren’,‘on’) and now().hour <= 7:35:00 and now().hour >= 22 }}”
Unfortunately, this line does not work.

Sorry for my bad English. I am using deepl.

Thank you
Frank

Ad 1: An or condition should do it.

Ad 2: Do you already have a sensor.time?
If so, you could do

state: "{{ states('sensor.time') <= '07:35' or now().hour >= 22 }}"

midnight to 7:35

{{ today_at('00:00') <= now() <= today_at('7:35') }}

22 to midnight

{{ now() >= today_at('22:00') }}

@pedolsky Thank you very much for your help
My automation looks like this:

Trigger

platform: state
entity_id:
  - binary_sensor.kuchefenster_og
from: "off"
to: "on"
for:
  hours: 0
  minutes: 0
  seconds: 0
id: Küchefenster OG

Condition

condition: and
conditions:
  - condition: state
    entity_id: input_boolean.alletueren
    state: "on"
  - condition: time
    after: "22:00:00"
    before: "07:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun

Action

choose:
  - conditions:
      - condition: trigger
        id:
          - Küchefenster OG
    sequence:
      - service: notify.alexa_media
        data:
          target:
            - media_player.alexa_wohnzimmer
            - media_player.echo_plus_kuche
            - media_player.echo_dot_schlafzimmer
            - media_player.franks_echo_pop
            - media_player.alexa_tim
          data:
            type: announce
            method: speak
          message: Küchefenster O G wurde geöffnet.
          title: Küchefenster O G wurde geöffnet.
alias: Küchenfenster OG

Where exactly do I insert this line?

state: "{{ states('sensor.time') <= '07:35' or now().hour >= 22 }}"

Do I need to replace the term sensor.time with binary_sensor.alletueren?

@petro

To understand:
If I put this into the template, would it look like this?

  - binary_sensor:
     - name: "alletueren"    
       unique_id: alletueren
       icon: mdi:coach-lamp   
       state: "{{ is_state('input_boolean.alletueren','on') and {{ now() >= today_at('22:00') }}"
       state: "{{ is_state('input_boolean.alletueren','on') and {{ today_at('00:00') <= now() <= today_at('7:35') }}
  • Unfortunately I get an error message there
  • Or does that have to be processed into a state line?
  • I would be grateful if you could show me what the State line should look like.

I would be grateful if you could show me what the state line including my switch should look like.
I use the switch to switch off the automation when I want to open the door at night or the window in another room.

Many thanks for yours help
Frank

The examples referred to the binary sensors. You simply have to adjust the state templates.


- binary_sensor:
     - name: "alletueren"    
       unique_id: alletueren
       icon: mdi:coach-lamp   
       state: "{{ is_state('input_boolean.alletueren','on') and (now() <= today_at('7:35')  or now() >= today_at('22:00')) }}"


You then could replace the automation conditions with


conditions:
  - condition: state
    entity_id: binary_sensor.alletueren
    state: "on"

The choose action is not necessary if there’s only one step.

Thank you very much.
I would never have thought of that.
I thought that an ‘or’ link would fit, but I couldn’t find any instructions on the internet.
Thanks again, you helped me a lot, even on a Sunday when I was off work.
Frank