How execute action based on user time on ESPHOME?

Hi all,
I am try a simple automation to execute a action in a time defined from the user:

I have defined 2 datetime as below:

datetime:
  - platform: template
    id: my_open_time
    type: time
    name: "Open Time"
    optimistic: yes
    initial_value: "07:00:00"
    restore_value: true
    icon: "mdi:calendar-alert"
  
  - platform: template
    id: my_close_time
    type: time
    name: "Close Time"
    optimistic: yes
    initial_value: "19:00:00"
    restore_value: true
    icon: "mdi:calendar-alert"

Based on it, I want execute a actions to OPEN and CLOSE my COVER time_based.

cover:
  - platform: time_based
    name: "General Cover"
    id: general_cover

    open_action:
      - output.turn_on: command_up
      - delay: 600ms
      - output.turn_off: command_up
    open_duration: 41s

    close_action:
      - output.turn_on: command_down
      - delay: 600ms
      - output.turn_off: command_down
    close_duration: 39s

    stop_action:
      - output.turn_on: command_stop
      - delay: 600ms
      - output.turn_off: command_stop

Any idea how can I do it?

BR

Hello @nickrout
I try it on my ways, using the ‘TIME’ component, but I do not know how to check the conditions.
Any way could you give a example?
BR

Conditions are dealt with in the page linked from here. I have you the automations page as you should really read ask those links.

You must setup some time source to have real time in ESP device.
Then create binary_sensor which evaluates as true when time is between close & open time.
In sensor automations call required cover actions to close/open it.

1 Like

Thanks all.
UPDATE AFTER IMPLEMENTATION (Last Update at 12-Sep-2024):

1º - Created a time object

Time:
  - platform: homeassistant
    id: homeassistant_time

2º Created the DATETIME object that the user can be set from UI.

datetime:
  - platform: template
    id: my_open_time
    type: time
    name: "Open Time"
    optimistic: yes
    initial_value: "07:00:00"
    restore_value: true
    icon: "mdi:calendar-alert"
    entity_category: config
  
  - platform: template
    id: my_close_time
    type: time
    name: "Close Time"
    optimistic: yes
    initial_value: "19:00:00"
    restore_value: true
    icon: "mdi:calendar-alert"
    entity_category: config

3º - Created a Binary Sensor to control the COVER object and also checl the operation mode object that the user can select.

binary_sensor:
  - platform: template
    name: "Cover Control Time"
    id: cover_control_time
    lambda: |-
      // Check if the operation mode is 'time_based'
      if (id(operation_mode).state != "time_based") {
      return false;  // Do not trigger the cover if mode is not 'time_based'
      }

      auto now = id(homeassistant_time).now();
      int current_time = now.hour * 60 + now.minute;  // Current time in minutes since midnight

      // Get the open time from datetime component
      auto open_time_obj = id(my_open_time).state_as_esptime();
      int open_time = (open_time_obj.hour * 60) + open_time_obj.minute;

      // Get the close time from datetime component
      auto close_time_obj = id(my_close_time).state_as_esptime();
      int close_time = (close_time_obj.hour * 60) + close_time_obj.minute;

      // Return true if the current time is between open_time and close_time
      return (current_time >= open_time && current_time < close_time);
 #----------------------------------------------------------------------------------
    # Automation to control the cover based on the time
    #----------------------------------------------------------------------------------
    on_press:
      then:
        - cover.open: general_cover
  
    on_release:
      then:
        - if:
            condition:
              lambda: 'return id(operation_mode).state == "time_based";'
            then: 
            - cover.close: general_cover

It was the solution implemented and running well.

Have a little doubt about this part:

IMHO need to use return {}; to keep state unchanged !
Current code will trigger change when state is true, but operartion_mode is not time_based.

@Masterzz YEs! I have implement a CONDITION for that! I am review the solution add it! Thanks a lot!