HELP - How to Turn on switch at adjustable time, turn off switch at adjustable time?

Tags: #<Tag:0x00007f7393777320> #<Tag:0x00007f7393777208> #<Tag:0x00007f73937770f0> #<Tag:0x00007f7393776e98> #<Tag:0x00007f7393776d08>

Hi all,

I’m trying to control a switch using adjustable on/off times, with several options:
for example,

I’d like to be able to:

— Choose a time to turn on an entity (example: 2pm (14:00))
— Choose a time to turn off the entity (example: turn off at 6pm (18:00))
— Option to NOT turn off at chosen time (example: ignore 6pm turn off and keep running)

I’ve seen a few examples where people have used Input_number to customise the time, etc, but I couldn’t quite figure it out, nor is it the most user friendly option - open to suggestions.

Any assistance would be greatly appreciated.
Thank you in advance

Paul

You might get some real good examples looking at Garden irrigation topics like this one:

There’s quite a few people who built great, elaborate setups including definition of input variables and interfaces that you can probably just copy from a package, cut it down, and use it for your switch.

1 Like

Or if you want to use an addon there are a couple of scheduler options now.

Or

3 Likes

So not sure if this gets you closer but maybe it will spark some ideas.

I use the input_datetime to set the trigger time and then I automate the setting of the input_datetime each day based on what is going on.

For example I have a good morning announcement to remind my kid when to get dressed for the day. So I created a input_datetime for it:

input_datetime:
  skylar_morning_report:
    name: Skylar Dressed Announcement
    has_date: false
    has_time: true

This time changes each day based on whether he has school so that hopefully he will sleep in on the weekdays he doesn’t have school. So I have an automation that runs each day to set the time of this input_datetime:

automation:
 - id: set_skylar_morning_report_time
    alias: set skylar morning report time
    trigger:
    - platform: time
      at: '05:50:00'
    action:
    - service: input_datetime.set_datetime
      entity_id: input_datetime.skylar_morning_report
      data_template:
        time: >
          {% if states.calendar.skylar_school.attributes.offset_reached == True %}
            06:15
          {% else %}
            07:58
          {% endif %}

That simply sets the time based on whether he has school. Just two options to choose from, but it could be as many as needed. The only catch is the set time automation has to happen when the calendar is going to show it is a school day.

Then the announcement is triggered at what ever time is set in that input_datetime and the announcement happens at different times without needing my input:

- id: skylar_morning_greeting
    alias: Skylar Morning Greeting
    initial_state: true
    trigger:
      platform: template
      value_template: "{{ states('sensor.time') == (state_attr('input_datetime.skylar_morning_report', 'timestamp') | int | timestamp_custom('%H:%M', False)) }}"
    condition:
    - condition: time
      weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    action:
    - service: script.ah_report
      data:
        call_interuption: 1
        call_skylar_dressed: 1
        call_school_today: 1
        call_skylar_events: 1
        call_clothes_suggestion: 1

You could easily set the start time and the end time to be adjustable either with an automation like I used or via the UI. I’ve tried to build my whole Home Assistant setup around the idea that I dont have to actually set these things myself or flips switches at all if I can help it. It supposed to be home “automation” and not home “remote control” right? lol

1 Like

A Big thank you for everyone’s contribution…

I found my solution using input_datetime

image

sensor:
  - platform: time_date
    display_options:
      - 'time'

input_boolean:
  spa_timer_on_enabled:
    name: "SPA ON timer"
    initial: off
    icon: mdi:hot-tub
  spa_timer_off_enabled:
    name: "SPA OFF timer"
    initial: off
    icon: mdi:hot-tub

input_datetime:
  turn_spa_on_time:
    name: "Turn SPA on at"
    has_time: true
    has_date: false
    initial: "14:00"
  turn_spa_off_time:
    name: "Turn SPA off at"
    has_time: true
    has_date: false
    initial: "16:00"


#TURN SPA ON AT SPECIFIC TIME
automation:
- id: '1587771123429'
  alias: Turn on SPA with Timer
  trigger:
    platform: template
    value_template: '{{ states(''sensor.time'') == (states.input_datetime.turn_spa_on_time.attributes.timestamp
      | int | timestamp_custom(''%H:%M'', False)) }}'
  condition:
  - condition: state
    entity_id: input_boolean.spa_timer_on_enabled
    state: 'on'
  action:
  - data: {}
    entity_id: input_boolean.spa_on_switch
    service: input_boolean.turn_on
    #TURN OFF SPA TIMER ON
  - delay: '2'
  - data: {}
    entity_id: input_boolean.spa_timer_on_enabled
    service: input_boolean.turn_off

#TURN SPA OFF AT SPECIFIC TIME
- id: '1587771123430'
  alias: Turn off SPA with Timer
  trigger:
    platform: template
    value_template: '{{ states(''sensor.time'') == (states.input_datetime.turn_spa_off_time.attributes.timestamp
      | int | timestamp_custom(''%H:%M'', False)) }}'
  condition:
  - condition: state
    entity_id: input_boolean.spa_timer_off_enabled
    state: 'on'
  action:
  - data: {}
    entity_id: input_boolean.spa_on_switch
    service: input_boolean.turn_off
    #TURN OFF SPA TIMER ON
  - delay: '2'
  - data: {}
    entity_id: input_boolean.spa_timer_on_enabled
    service: input_boolean.turn_off
    #TURN OFF SPA TIMER OFF
  - data: {}
    entity_id: input_boolean.spa_timer_off_enabled
    service: input_boolean.turn_off

This allows me to turn on / off the spa at a given 24hr time. It will also turn off the timers once actioned. I’ll play around with adding the day of the week in there next.

Again, thank you for everyone’s contributions, this really is a terrific community.