Set Binary State Based on Time and Input Triggers

I am trying to set a binary value (to use for indication and automation) for when my home is in “night-mode”. I guess I have to use either a binary_sensor or input_boolean. There are three ways I wish to be able to change the state of this binary:

  1. Triggered by times (set by user in UI). So if now() is after a certain time in the evening it goes to “True/On” and after a time in the morning it goes to “False/Off”.

  2. Toggled from the UI by clicking the entity or similar.

  3. Toggled by a physical button.

I also want to make sure the state is correct based on the defined time interval when starting/rebooting Home Assistant. This one I managed by creating a binary_sensor like this:

- trigger:

    - platform: homeassistant
      event: start
  
  binary_sensor:
  
    - name: "House Mode Night"
      icon: mdi:home
      state: >-
        {% set start = today_at(states('input_datetime.night_time_start')) %}
        {% set end = today_at(states('input_datetime.night_time_end')) + timedelta( days = 1) %}
        {{start < now() < end}}

However I am not sure how change the state afterwards based on the other triggers. For instance, I cannot toggle a binary_sensor from the UI and I cannot create an automation to toggle the state either.

Does anyone have any suggestions on how I might approach this?

image

Create an Input Boolean.

  1. Create an automation that turns on/off the Input Boolean at the desired times as “set by user in UI”. Include a startup trigger to set the Input Boolean’s state as per the desired timeframe.
  2. The Input Boolean can be “toggled from the UI”.
  3. Create another automation (or simply enhance the first one) that turns on/off the Input Boolean based on the “physical button”.

Let me know if you need my help to compose the automation.

Thanks for the tip. I’m not sure how the startup though state would work though. Setting a trigger is fine, but how can I check the timeframe (which i guess would be a condition) for just that trigger in the automation?