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.