How do I make a template to select TRUE from two temporary sensors?

I made a template for a binary sensor. I want him to check the value by two sensors false or true when at least one TRUE block outputs modeled by the first block and separately by the second. How would I do what would be true when one of the parameters of any TRUE block?

turtle:

# ручний шаблон для змін в ручному режимі з вєб інтерфейсу данних  по діапазонам часу      
    input_datetime:
    
      first_start:
        name: Початок першого режиму
        has_date: false
        has_time: true

      first_stop:
        name: Завершення  першого режиму
        has_date: false
        has_time: true

      second_start:
        name: Початок другого режиму
        has_date: false
        has_time: true

      second_stop:
        name: Завершення  другого режиму
        has_date: false
        has_time: true  
# додаєм бінарний сенсор для шаблону ручного вводу        
    template:
    
      - binary_sensor:
      
          - name: turtle_time
            unique_id: turtle_time
            state: >
                {% if states('input_datetime.first_start') < states('input_datetime.first_stop') %}
                {{ states('input_datetime.first_start') <= now().strftime('%H:%M:%S') <= states('input_datetime.first_stop') }}
                {% elif states('input_datetime.first_start') > states('input_datetime.first_stop') %}
                {{states('input_datetime.first_start') <= now().strftime('%H:%M:%S') <= '23:59:59'
                or '00:00:00' <= now().strftime('%H:%M:%S') <= states('input_datetime.first_stop')}}
                {% endif %}     
                
                {% if states('input_datetime.second_start') < states('input_datetime.second_stop') %}
                {{ states('input_datetime.second_start') <= now().strftime('%H:%M:%S') <= states('input_datetime.second_stop') }}
                {% elif states('input_datetime.second_start') > states('input_datetime.second_stop') %}
                {{states('input_datetime.second_start') <= now().strftime('%H:%M:%S') <= '23:59:59'
                or '00:00:00' <= now().strftime('%H:%M:%S') <= states('input_datetime.second_stop')}}
                {% endif %}
                  
            device_class: running



Use variables to make repetitive code more readable and to define what you need to compare.
Anything in expression delimiters {{ }} will be output. In a binary sensor the final output must be true or false, so you will usually only have one expression at the very end. Logic and assigning values to variables is handled in statements {% %}.

{%- set start_1 = today_at(states('input_datetime.first_start')) %}
{%- set stop_1 = today_at(states('input_datetime.first_stop')) %}
{%- set start_2 = today_at(states('input_datetime.second_start')) %}
{%- set stop_2 = today_at(states('input_datetime.second_stop')) %}

{%- set stop_1 = stop_1 if start_1 < stop_1 else stop_1 + timedelta(days=1) %}
  {%- set first = start_1 <= now() <= stop_1 %}

{%- set stop_2 = stop_2 if start_2 < stop_2 else stop_2 + timedelta(days=1) %}
  {%- set second = start_2 <= now() <= stop_2 %}

{{- first or second }}
1 Like

Super Thank you all works And where you can read about the settings "timedelta

In the Templating docs:

As stated, timedelta() is based on the Python datetime function. You can learn more about timedelta function and object in Python’s datetime documentation

1 Like