blueprint:
  name: On-Off schedule with state persistence
  description: '# On-Off schedule with state persistence


    A simple on-off schedule, with the addition of state persistence across disruptive
    events, making sure the target device is always in the expected state.


    📕 Full documentation regarding this blueprint is available [here](https://epmatt.github.io/awesome-ha-blueprints/docs/blueprints/automation/on_off_schedule_state_persistence).


    🚀 This blueprint is part of the **[Awesome HA Blueprints](https://epmatt.github.io/awesome-ha-blueprints)
    project**.


    ℹ️ Version 2021.10.26

    '
  source_url: https://github.com/EPMatt/awesome-ha-blueprints/blob/main/blueprints/automation/on_off_schedule_state_persistence/on_off_schedule_state_persistence.yaml
  domain: automation
  input:
    automation_target:
      name: (Required) Automation target
      description: The target which the automation will turn on and off based on the
        provided schedule.
      selector:
        target: {}
    on_time:
      name: (Required) On Time
      description: Time when the target should be placed in the on state.
      selector:
        time: {}
    off_time:
      name: (Required) Off Time
      description: Time when the target should be placed in the off state.
      selector:
        time: {}
    custom_trigger_event:
      name: (Optional) Custom Trigger Event
      description: A custom event which can trigger the state check (eg. a powercut
        event reported by external integrations).
      default: ''
      selector:
        text: {}
    trigger_at_homeassistant_startup:
      name: (Optional) Trigger at Home Assistant startup
      description: Trigger the target state check and enforcement at Home Assistant
        startup.
      default: false
      selector:
        boolean: {}
variables:
  off_time: !input 'off_time'
  on_time: !input 'on_time'
  trigger_at_homeassistant_startup: !input 'trigger_at_homeassistant_startup'
  time_fmt: '%H:%M:%S'
  first_event: '{{ on_time if strptime(on_time,time_fmt).time() < strptime(off_time,time_fmt).time()
    else off_time }}'
  second_event: '{{ on_time if strptime(on_time,time_fmt).time() >= strptime(off_time,time_fmt).time()
    else off_time }}'
mode: single
max_exceeded: silent
trigger:
- platform: time
  at:
  - !input 'on_time'
  - !input 'off_time'
- platform: homeassistant
  event: start
- platform: event
  event_type: !input 'custom_trigger_event'
condition:
- condition: template
  value_template: '{{ trigger.platform!="homeassistant" or trigger_at_homeassistant_startup
    }}'
action:
- choose:
  - conditions:
    - condition: template
      value_template: '{{ now().time() >= strptime(first_event,time_fmt).time() and
        now().time() < strptime(second_event,time_fmt).time() }}'
    sequence:
    - service: homeassistant.{{ "turn_on" if first_event == on_time else "turn_off"}}
      target: !input 'automation_target'
  default:
  - service: homeassistant.{{ "turn_on" if second_event == on_time else "turn_off"}}
    target: !input 'automation_target'
