Skip to content

Instantly share code, notes, and snippets.

@FrankTub
Last active August 29, 2025 04:23
Show Gist options
  • Select an option

  • Save FrankTub/38f2d1ad6cc054f775a4f7b8bee06db2 to your computer and use it in GitHub Desktop.

Select an option

Save FrankTub/38f2d1ad6cc054f775a4f7b8bee06db2 to your computer and use it in GitHub Desktop.
Binary sensor to input boolean
blueprint:
name: Binary sensor → input_boolean with day/night rules
description: >
Toggle an input_boolean on/off based on a binary sensor with separate day/night delays
and cutoff times.
domain: automation
input:
required_inputs:
name: "Required inputs"
icon: mdi:list-box-outline
collapsed: false
input:
target_boolean:
name: (Target) input_boolean
description: The input_boolean to control through the automation
selector:
entity:
domain: input_boolean
trigger_sensor:
name: (Input) Binary sensor
description: The binary sensor that changes states that should trigger the state change of the target input_boolean
selector:
entity:
domain:
- input_boolean
- binary_sensor
- switch
configuration_settings:
name: "Configuration settings"
description: In this part of the automation you can configure when day and night periods start and end and what the respective delay periods for day and night should be. It is also possible to inverse the binary sensor input trigger with respect to the target input boolean
icon: mdi:tools
collapsed: true
input:
time_day_start:
name: Day start time
description: Everything between day start time and day end time is considered day time. Outside of it is considered night time.
default: "07:00:00"
selector:
time:
time_day_end:
name: Day end time
description: Everything between day start time and day end time is considered day time. Outside of it is considered night time.
default: "20:00:00"
selector:
time:
wait_on_day:
name: Wait before ON (daytime)
description: During the day the time before the target is turned on, if you use the inverse state behavior this means this is the time before the input binary sensor is turned off!
default:
hours: 0
minutes: 15
seconds: 0
selector:
duration:
wait_off_day:
name: Wait before OFF (daytime)
description: During the day the time before the target is turned off, if you use the inverse state behavior this means this is the time before the input binary sensor is turned on!
default:
hours: 0
minutes: 3
seconds: 0
selector:
duration:
wait_on_night:
name: Wait before ON (nighttime)
description: During the night the time before the target is turned on, if you use the inverse state behavior this means this is the time before the input binary sensor is turned off!
default:
hours: 0
minutes: 3
seconds: 0
selector:
duration:
wait_off_night:
name: Wait before OFF (nighttime)
description: During the night the time before the target is turned off, if you use the inverse state behavior this means this is the time before the input binary sensor is turned on!
default:
hours: 1
minutes: 0
seconds: 0
selector:
duration:
inverse_state_behavior:
name: Inverse state behavior
description: |
If enabled, the binary sensor ON will turn the boolean OFF and vice versa.
default: false
selector:
boolean:
trigger_variables:
trigger_sensor: !input trigger_sensor
target_boolean: !input target_boolean
time_day_start: !input time_day_start
time_day_end: !input time_day_end
wait_on_day: !input wait_on_day
wait_off_day: !input wait_off_day
wait_on_night: !input wait_on_night
wait_off_night: !input wait_off_night
inverse_state_behavior: !input inverse_state_behavior
triggers:
# --- Day ON ---
- trigger: state
entity_id: !input trigger_sensor
from: "off"
to: "on"
for: "{{ wait_on_day if not inverse_state_behavior else wait_off_day }}"
id: "source_turned_on_day"
# --- Day OFF ---
- trigger: state
entity_id: !input trigger_sensor
from: "on"
to: "off"
for: "{{ wait_off_day if not inverse_state_behavior else wait_on_day }}"
id: "source_turned_off_day"
# --- Night ON ---
- trigger: state
entity_id: !input trigger_sensor
from: "off"
to: "on"
for: "{{ wait_on_night if not inverse_state_behavior else wait_off_night }}"
id: "source_turned_on_night"
# --- Night OFF ---
- trigger: state
entity_id: !input trigger_sensor
from: "on"
to: "off"
for: "{{ wait_off_night if not inverse_state_behavior else wait_on_night }}"
id: "source_turned_off_night"
# --- Day/night transition triggers ---
- trigger: time
at: !input time_day_start
id: day_start
- trigger: time
at: !input time_day_end
id: day_end
actions:
- variables:
# Effective sensor state depending on inverse_state_behavior
trigger_variable_eligible_to_turn_target_on: "{{ states(trigger_sensor) in ['on', 'true'] if not inverse_state_behavior else states(trigger_sensor) in ['off','false'] }}"
trigger_variable_eligible_to_turn_target_off: "{{ states(trigger_sensor) in ['off', 'false'] if not inverse_state_behavior else states(trigger_sensor) in ['on','true'] }}"
- choose:
# --- ON branch ---
- conditions:
- "{{ states(target_boolean) in ['false', 'off'] }}"
- "{{ trigger_variable_eligible_to_turn_target_on }}"
- or:
- and:
- or:
- "{{ trigger.id == 'source_turned_on_day' and not inverse_state_behavior }}"
- "{{ trigger.id == 'source_turned_off_day' and inverse_state_behavior }}"
- "{{ (now() | as_local) >= (today_at(time_day_start) | as_local) and (now() | as_local) < (today_at(time_day_end) | as_local) }}"
- and:
- or:
- "{{ trigger.id == 'source_turned_on_night' and not inverse_state_behavior }}"
- "{{ trigger.id == 'source_turned_off_night' and inverse_state_behavior }}"
- "{{ (now() | as_local) >= (today_at(time_day_end) | as_local) or (now() | as_local) < (today_at(time_day_start) | as_local) }}"
- and:
- "{{ trigger.id == 'day_start' }}"
- >
{% set w = iif(not inverse_state_behavior, wait_on_day, wait_off_day) %}
{{ (as_timestamp(now()) - as_timestamp(states[trigger_sensor].last_changed))
>= ((w.hours|int(0))*3600 + (w.minutes|int(0))*60 + (w.seconds|int(0))) }}
- and:
- "{{ trigger.id == 'day_end' }}"
- >
{% set w = iif(not inverse_state_behavior, wait_on_night, wait_off_night) %}
{{ (as_timestamp(now()) - as_timestamp(states[trigger_sensor].last_changed))
>= ((w.hours|int(0))*3600 + (w.minutes|int(0))*60 + (w.seconds|int(0))) }}
sequence:
- service: input_boolean.turn_on
target:
entity_id: !input target_boolean
# --- OFF branch ---
- conditions:
- "{{ states(target_boolean) in ['true', 'on'] }}"
- "{{ trigger_variable_eligible_to_turn_target_off }}"
- or:
- and:
- or:
- "{{ trigger.id == 'source_turned_off_day' and not inverse_state_behavior }}"
- "{{ trigger.id == 'source_turned_on_day' and inverse_state_behavior }}"
- "{{ (now() | as_local) >= (today_at(time_day_start) | as_local) and (now() | as_local) < (today_at(time_day_end) | as_local) }}"
- and:
- or:
- "{{ trigger.id == 'source_turned_off_night' and not inverse_state_behavior }}"
- "{{ trigger.id == 'source_turned_on_night' and inverse_state_behavior }}"
- "{{ (now() | as_local) >= (today_at(time_day_end) | as_local) or (now() | as_local) < (today_at(time_day_start) | as_local) }}"
- and:
- "{{ trigger.id == 'day_start' }}"
- >
{% set w = iif(not inverse_state_behavior, wait_off_day, wait_on_day) %}
{{ (as_timestamp(now()) - as_timestamp(states[trigger_sensor].last_changed))
>= ((w.hours|int(0))*3600 + (w.minutes|int(0))*60 + (w.seconds|int(0))) }}
- and:
- "{{ trigger.id == 'day_end' }}"
- >
{% set w = iif(not inverse_state_behavior, wait_off_night, wait_on_night) %}
{{ (as_timestamp(now()) - as_timestamp(states[trigger_sensor].last_changed))
>= ((w.hours|int(0))*3600 + (w.minutes|int(0))*60 + (w.seconds|int(0))) }}
sequence:
- service: input_boolean.turn_off
target:
entity_id: !input target_boolean
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment