Hello;
I have been racking my brain for the last few days trying to figure out how to get some PIR motion detectors to turn on switches according to two times set by input_datetime selectors on the front end. I am happy to say, I figured that out - and it was all in the ‘and/or’. stupid little detail, but all the difference.
Here is that working code, with the actual entity names changed just to make it more reader friendly.
// First evaluation:
// If the “disable” is less than the “enable”…
// True; (ie: off at 00:00, on at 09:00)…“current time” must be less than “disable” OR more than “on time”
// False; (ie: on at 09:00, off at 23:00)…“current time” must be less than “disable” AND more than “enable”
- alias: "Turn on string lights with movement"
trigger:
platform: state
entity_id:
- binary_sensor.left_pir_sensor
- binary_sensor.front_pir_sensor
- binary_sensor.right_pir_sensor
from: "off"
to: "on"
condition:
condition: and
conditions:
- condition: state
entity_id: switch.sonoff_1000c62377
state: 'off'
- condition: template
value_template: >
{% if states.input_datetime.disable.state < states.input_datetime.enable.state %}
{{ states.sensor.time.state < states.input_datetime.disable.state or states.sensor.time.state > states.input_datetime.enable.state }}
{% else %}
{{ states.sensor.time.state < states.input_datetime.disable.state and states.sensor.time.state > states.input_datetime.enable.state }}
{% endif %}
action:
- service: switch.turn_on
target:
entity_id: switch.sonoff_1000c62377
So, that all works good, lights turn on when they detect movement, but only between the hours specified.
What I would like to do is a similar service_template that turns on an input_boolean at the beginning of the “enable” time and turns off at “disable” time. I haven’t even started with the trigger for that yet, I am still trying to figure out how to nest multiple if statements into the existing, already complicated if statement… this time it would be a service_template in a different automation - but based on the previous template, something like this:
- service_template: >
{% if states.input_datetime.disable.state < states.input_datetime.enable.state %}
{{ states.sensor.time.state < states.input_datetime.disable.state or states.sensor.time.state > states.input_datetime.enable.state }}
{% if true %}
input_boolean.turn_on
{% else %}
input_boolean.turn_off
{% endif %}
{% else %}
{{ states.sensor.time.state < states.input_datetime.disable.state and states.sensor.time.state > states.input_datetime.enable.state }}
{% if true %}
input_boolean.turn_on
{% else %}
input_boolean.turn_off
{% endif %}
{% endif %}
entity_id: input_boolean.test_boolean
Any help would be appreciated. This thing is giving me a headache!