Schedule switch

Hello Guys,

I need a automation that turn on one switch at 21pm and turn off at 6am, i have an example, but is with 2 automations and i want all in just one automation. this is a example of 2 automations:

`- alias: 'Luzes exteriores ON'
    trigger:
      platform: time
      at: '21:00:00'
    action:
      service: switch.turn_on
      entity_id: switch.escritorio_exterior

- alias: 'Luzes exteriores OFF'
  trigger:
    platform: time
    at: '06:00:00'
  action:
    service: switch.turn_off
    entity_id: switch.escritorio_exterior`

Anyone know how i can do this in just one automation?

Thank you

You need to format your code as indicated at the top of the page.

1 Like

as per @sjee above reg. code formatting.

With regards to the logic behind it all, 2 automations is the easiest.
If you want a single automation, you’ll have to have 2 triggers (one for each time)
and set some service_template that takes into account the trigger, extracts its value to make your condition and call the correct command.
It makes it difficult to troubleshoot when you have issues, so although I know it’s less text to author 1 single automation, it’s actually much faster to have 2 just like you’ve done.
My 2 cents…

1 Like

There is another option which is to use switch.toggle (as descibed in the docs). But I agree with @lolouk44 just stick to 2 automations, it’s way easier.

1 Like

Thank you, and sorry for initial format code, i’m a newbie here :slight_smile:

Okay, it was just to have less buttons on my General page, but’s OK. And i will try the switch.toogle.

There are pros and cons both ways, and mostly I think it’s a matter of preference. If you do want one automation, it’s pretty simple:

- alias: 'Luzes exteriores'
  trigger:
    - platform: time
      at: '21:00:00'
    - platform: time
      at: '06:00:00'
  action:
    service_template: "switch.turn_{{ 'on' if trigger.now.hour == 21 else 'off' }}"
    entity_id: switch.escritorio_exterior
3 Likes

yeahh great :+1::+1: and how can I turn on the light at 9:15 p.m.?

thank you!

:+1::+1: The light turn on at 21h, but doesn´t turn off at 6h :frowning:

To answer your first question, do you mean at 9:15 p.m. instead of 9:00 p.m.? If so, just change '21:00:00' to '21:15:00'.

Regarding it working at 21h but not at 6h, I guess I’m not sure how that is possible. Are there any errors? Are you sure the automation is still turned on at 6h? Could there be something else running at 6h trying to turn the switch on? I would think this would either work for both times or neither.

yes, i already change ‘21:00:00’ to ‘21:15:00’, but in this case, it isn´t necessary change?

how it can knows turn off at 6:00?

yes, turn on it´s OK, but turn Off no.

trigger.now is the datetime object that caused the automation to trigger. See details here. A datetime’s hour attribute is just that, the hour part of the time. So, trigger.now.hour will be 21 when the automation triggers at 21:15:00, and it will be 6 when the automation triggers at 06:00:00. And the template {{ 'on' if trigger.now.hour == 21 else 'off' }} evaluates to ‘on’ when trigger.now.hour is 21, and evaluates to ‘off’ when trigger.now.hour is not 21 (e.g., when it is 6.)

FWIW, I use this exact construct in one of my automations:

automation:
  - alias: Period of Day
    trigger:
      - platform: time
        at: '05:45:00'
      - platform: time
        at: '22:00:00'
    action:
      service_template: >
        {% if trigger.now.hour == 5 %}
          script.pod_morning
        {% else %}
          script.pod_night
        {% endif %}

which works just fine.

And just to be sure, I tested the service_template in the template editor:

{% set trigger = {'now': strptime('21:15:00','%H:%M:%S')} %}
switch.turn_{{ 'on' if trigger.now.hour == 21 else 'off' }}
{% set trigger = {'now': strptime('06:00:00','%H:%M:%S')} -%}
switch.turn_{{ 'on' if trigger.now.hour == 21 else 'off' }}

results in:

switch.turn_on
switch.turn_off

So, I’m at a loss as to why this isn’t working for you. If none of my previous questions help you determine why it isn’t working, then I guess you should just stick with the two automations that do work for you.

2 Likes

Hi all,
I just changed the first code segment to read as below. Makes editing a few seconds quicker by using the second to trigger the event and allows better switching for anything down to 1 mimute events.
Seems to work reliably for me so far… :slight_smile:

#######################################
#####  Timed events
        ###### Use the seconds to control the switch. "== 1 is ON, == 0 is OFF"
        - alias: 'Livingroom Lights on 07:05-08:30'
          trigger:
            - platform: time
              at: '07:05:01'
            - platform: time
              at: '08:30:00'
          action:
            service_template: "switch.turn_{{ 'on' if trigger.now.second == 1 else 'off' }}"
            entity_id: switch.lights

To follow up on my posts above (about the use of trigger.now), the behavior of that variable changed with HA release 0.81. See this post for more details.

Hi community,

i came across this thread while i was looking for exact the same automation.
So i felt very happy :+1:
But when i apply the section for this automation, the following error will be emitted:
2019-11-27 09:52:47 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: required key not provided @ data['trigger'][1]['platform']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 91). Please check the docs at https://home-assistant.io/integrations/automation/

My configuration looks as follows:

- alias: SomeLighting
  trigger:
    - platform: time
      at: "17:00:00"
    - patform: time
      at: "22:30:00"
  action:
    service_template: "switch.turn_{{ 'on' if trigger.now.hour == 17 else 'off' }}"
    data:
      entity_id: switch.somelighting

If i remove the second time trigger, the error will be gone. Running hass 0.102.1 on a pi within a virtualenv.
Hope anybody can help.

Your error is telling you where to look. It’s saying it cannot find ‘platform’. Looking at the code you posted, your platform for 22:30:00 is misspelled.

in regards to errors:

the last most item in the series of words data['trigger'][1]['platform'] is the missing key. In regards to yaml:

 - platform: time
   \      /  \  /
    ------    --
      |        |
     key     value

:woozy_face: What the heck! No, that didn’t just happen to me, did it? :joy:
Thank you.

I noticed all the examples used the “at” expression. At 9pm turn light on, and at 6am turn light off, but what happens if there is a reboot at 10pm?

Is there a way to set up an animation to turn switch on if time is after 9pm, and turn switch off if time is after 6am but before 9pm?

Only way would be to add a trigger to run every minute and add a condition that the time is within your specified range

Thanks for the suggestion. I found a similar work around to what you mentioned. I am using two automations, one to turn on and the other to turn off.

The turn on automation has two triggers (only one has to be met for the automation to run). One trigger is at start up, the other trigger is at 06:00:01 . Condition uses the after 6am before 9am parameters. This way at one second after 6am the trigger checks the conditions, notices it is after 6am so it turns on the switch.

At reboot (re start) it always checks the conditions. If within the window switch turns on, otherwise switch remains off.

The second automation to turn off the switch is much more basic. It just runs everyday at 9am and turns switch off.

I was able to set up those automations without writing any Yaml code. I just used the autmoations section in HA. For other switches (watering switches) seems I will have to do a bit of yaml coding since the UI for automations doesn’t support every other day option.

That would create an endless loop of triggers. Because it would be “true” every second during that time. You could stop that by making an input Boolean and use it as a condition. Has to be off to pass, and set it On in the action. Define it as off on restart that way on a restart the automation would pass.