Binary sensor with time conditions from input_datetime

I’m new to Home Assistant Automation and I’m trying to build fairly simple automation that will turn on the light when the motion sensor detects movement and when the time is between specific hours.

Here is what I have so far:


- alias: Turn on the light
  trigger:
    platform: state
    entity_id: binary_sensor.motion
    to: 'on'
  condition:
    - condition: time
      after: '22:00:00'
      before: '06:30:00'
  action:
    - service: homeassistant.turn_on
      entity_id: switch.light1
        
    
- alias: Turn off the light
  trigger:
    platform: state
    entity_id: binary_sensor.motion
    to: 'off'
    for:
      seconds: 20
  condition:
  - condition: time
    after: '22:00:00'
    before: '06:30:00'
  action:
    service: homeassistant.turn_off
    entity_id: switch.light1

This works great, but now I want to have two parameters, so I created two entries:

input_datetime:
  light_from_time:
    name: From
    has_date: false
    has_time: true
    icon: mdi:timer
  light_to_time:
    name: To
    has_date: false
    has_time: true
    icon: mdi:timer-off

But I have problem with connecting values from those inputs to my conditions.
How should I do that?
I’ve read Time range condition template and tried using some of the code from there, but all my tries failed - my automation stopped working.
My idea is to use a template condition, but I must create datetime from time (my inputs are only for time).

My idea is to have a working sensor only during the night, from 22:00 till 06:30.

I was trying to use now() nad compare it with states(‘input_datetime.light_from_time’) like so:

{%- if states('input_datetime.light_from_time')> now() > states('input_datetime.light_to_time') -%}
Works
{%- else -%}
Not
{%- endif -%}

but this gives me Unknown error rendering template, probably because states return string.

An input_datetime has an attribute named timestamp. If it just has time (and not date), then the timestamp is the number of seconds between midnight and the time. So you could add a condition like this:

  condition:
    condition: template
    value_template: >
      {% set n = now() %}
      {% set n = (n.hour*60+n.minute)*60+n.second %}
      {% set ts_from = state_attr('input_datetime.light_from_time', 'timestamp') %}
      {% set ts_to = state_attr('input_datetime.light_to_time', 'timestamp') %}
      {{ n > ts_from or n < ts_to }}
2 Likes

I’ve checked that in the template editor and it seems to work perfectly. Thank You for the reply!
Next time before I ask I must read docs twice.
I think this example would be very helpful in docs - many of us want to have the condition based on hour range.

3 Likes

Hi Misiu

I have the same issue as you. Would you mind sharing your code?

The code from pnbruckner works but I cannot figure out your code you posted.

Kind Regards
Quintin Sleeking

This is my code, it’s not optimal, but it works for me:

- alias: turn on the light
  trigger:
    platform: state
    entity_id: binary_sensor.ruch
    to: 'on'
  condition:
    -  condition: template
       value_template: >
        {% set n = now() %}
        {% set n = (n.hour*60+n.minute)*60+n.second %}
        {% set ts_from = state_attr('input_datetime.light_from_time', 'timestamp') %}
        {% set ts_to = state_attr('input_datetime.light_to_time', 'timestamp') %}
        {{ n > ts_from or n < ts_to }}
  action:
    - service: homeassistant.turn_on
      entity_id: switch.oswietlenie_korytarz
      
####
      
- alias: turn off the light
  trigger:
    platform: state
    entity_id: binary_sensor.ruch
    to: 'off'
    for:
      seconds: "{{ states('input_number.oswietlenie_czas')|int }}"
  action:
    service: homeassistant.turn_off
    entity_id: switch.oswietlenie_korytarz

hope this helps :slight_smile:
if someone has a better version please let me know.

2 Likes