Time scheduled on/off automation?

Hi there, I have some on/off interval codes for fans. I would like to split this code as two time zone.
But I’m a bit struggling to solve. Here is the code:

- id: 'Fanotomation G1'
  alias: Fanon G1
  trigger:
    platform: time
    minutes: '/30'
    seconds: 00
  action:
    - service: switch.turn_on
      entity_id: switch.fanon
    - delay:
        seconds: 60
    - service: switch.turn_off
      entity_id: switch.fanon

I would like to use code above for all week between 7.00 AM to 7.00 PM
and I would like to use code below all week between 7.00 PM to 7:00 AM

- id: 'Fanotomation G1'
  alias: Fanon G1
  trigger:
    platform: time
    minutes: '/10'
    seconds: 00
  action:
    - service: switch.turn_on
      entity_id: switch.fanon
    - delay:
        seconds: 60
    - service: switch.turn_off
      entity_id: switch.fanon

how can I combine these two as I want ?

Im a little confused, are you trying to disable the automation or just add a condition ?

In the future please follow the instructions at the top of the page to properly format your YAML code.

I think you can do it like this:

  - alias: Fanon G1
    trigger:
      platform: time
      minutes: '/10'
      seconds: 0
    condition:
      - condition: time
        weekday: [mon, tue, wed, thu, fri]
      - condition: template
        value_template: >
          {{ now().hour < 7 or now().hour >= 19 or trigger.now.minute % 30 == 0 }}
    action:
      - service: switch.turn_on
        entity_id: switch.fanon
      - delay:
          seconds: 60
      - service: switch.turn_off
        entity_id: switch.fanon

I don’t want disable anything. The code that I sent run as loop whole day. I would like to run that code as two different intervals in two different time zone (code 1- 7.00 AM to 7.00 PM and code 2- 7.00 PM to 7.00 AM). I just want to add time condition.

Ok, that’s not how I understood what you were asking for. You said “weekdays”, which usually means Monday through Friday.

If you want to just change the interval based on time of day (and run the automation on all days), then simply remove the first condition from the suggestion I made above.

I don’t want any modification of code just add time condition for all week.
here is sample but a bit complicated for me.

That is changing the code. :slight_smile:

If you want to run the actions all week, and you want them to run every 30 min between 7:00 AM and 7:00 PM, and every 10 min between 7:00 PM and 7:00 AM, that’s what my suggestion does, once the weekday condition is removed, as I said:

  - alias: Fanon G1
    trigger:
      platform: time
      minutes: '/10'
      seconds: 0
    condition:
      condition: template
      value_template: >
        {{ now().hour < 7 or now().hour >= 19 or trigger.now.minute % 30 == 0 }}
    action:
      - service: switch.turn_on
        entity_id: switch.fanon
      - delay:
          seconds: 60
      - service: switch.turn_off
        entity_id: switch.fanon

This triggers every 10 minutes, but the condition only lets the actions actually run (every 10 minutes) if the current time is between 7 PM and 7 AM. Otherwise it only lets them run if the minutes past the hour is 00 or 30 (i.e., every 30 minutes from 7 AM to 7 PM.) Isn’t that what you said you wanted? Have you given this a try?

1 Like

I’ll try the code. I’m gonna feedback soon.

I just pasted it in to my config and it worked fine. What error messages where you getting ?

here is error code;

Invalid config for [automation]: [condition] is an invalid option for [automation]. Check: automation->trigger->0->condition. (See /config/configuration.yaml, line 233). Please check the docs at https://home-assistant.io/components/automation/

I use these codes in automations.yaml file. Is that “condition” tag usable in this file ?

You can’t use a condition inside a trigger.

If you share your automation, we can help you work out what’s wrong.

I checked the code and give a try again. I miscoded the indents. There is no error in script. I’ll feedback soon.

The code works perfeclty !!! Thanks pnbruckner here is the on/off of history. Fanon line is modified code others are normal.

1 Like

hello there, I have changed the code of “% 30 == 0” to “% 20 == 0” but there is no change in the minutes except the fans run at once per hour !!!. What I’m doing wrong ? I would like to change the minutes as 20 for 7.00 AM to 7.00 PM.

Hmm, I don’t know. I think that should have worked. Are you sure you didn’t change something else? Maybe post the automation as it is now.

here it is,
automations.yaml (3.0 KB)

Yes, you changed something else. You changed the trigger to only fire every 15 minutes, but you changed the condition to test for every 20 minutes. They’ll only “agree” every 60 minutes. Change the trigger back to every 10 minutes. If that’s not what you want, then you may need to go back to the drawing board.

That’s what I want:

FAN

sometimes I would like to change numbers (as minute).

Ok, I’m confused. If you want it to run every 30 minutes during the day, and every 15 minutes during the night, then I understand why you changed the trigger to every 15 minutes, but I don’t understand why you changed the condition to % 20 == 0 (i.e., every 20 minutes.)

If I understand what you want now, then leave the trigger as every 15 minutes, and put the condition back to % 30 == 0 (i.e., every 30 minutes.)