Automation switch on entity only between hours

Hi,
I am newbee to HA automation.

I would like to switch on an entity between 2:00 and 4:00 AM. The rest of the day it shall be switched off!

I saw examples with "condition: time; after; before. But I think I would need some trigger for this as well.

I could set two automations! One for switch “on” and one for switch “off”.
If this would be the solution - mmmh - fine! But I hope there is some better setup for my requirement.

Any help apreciated!

1 Like

Yes two automations with a time trigger in each, 2:00 for on and 4:00 for off is the easiest way. There are many ways to combine the two automations into one but it is best to start out simple.

Other ways you could do this in one automation:

  1. Use a schedule helper. Edit the schedule helper to cover the 2am to 4am time. Then:
trigger:
 - platform: state
   entity_id: schedule.my_switch_schedule
   not_to:
    - unavailable
    - unknown
action:
  - if: 
      - condition: state
        entity_id: schedule.my_switch_schedule
        state: 'on'
    then:
      - service: switch.turn_on
        target:
          entity_id: switch.my_switch
    else:
      - service: switch.turn_off
        target:
          entity_id: switch.my_switch
  1. Or using trigger ids, templates and time triggers:
trigger:
  - id: 'on'
    platform: time
    at: "02:00:00"
  - id: 'off'
    platform: time
    at: "04:00:00"
action:
  - service: "switch.turn_{{ trigger_id }}"
    target:
      entity_id: switch.my_switch

There are other ways as well but try to walk before you try to fly.

thanks for the examples.
I tried 2) but I get “Fehler: Template rendered invalid service: switch.turn_”

- id: '1720157995030'
  alias: Test
  description: ''
  trigger:
  - id: 'on'
    platform: time
    at: 09:07:00
  - id: 'off'
    platform: time
    at: 09:08:00
  action:
  - service: switch.turn_{{ trigger_id }}
    metadata: {}
    data: {}
    target:
      entity_id: switch.s_shelly02_switch_0
  mode: single

Typo. Should be:

  - service: switch.turn_{{ trigger.id }}
-----------------------------------^

…and it’s recommended to put your times in quotes:

    at: "09:07:00"

I have tried to add a schedule helper 1:1 from the example of the doc but failed:
At least, the helper does not show up in the webgui.

schedule:
  thermostat_schedule:
    name: "Thermostat schedule"
    monday:
      - from: "17:00:00"
        to: "21:00:00"
    tuesday:
      - from: "17:00:00"
        to: "21:00:00"
    wednesday:
      - from: "17:00:00"
        to: "21:00:00"
    thursday:
      - from: "17:00:00"
        to: "21:00:00"
    friday:
      - from: "17:00:00"
        to: "23:00:00"
    saturday:
      - from: "07:00:00"
        to: "10:00:00"
      - from: "16:00:00"
        to: "23:00:00"
    sunday:
      - from: "07:00:00"
        to: "21:00:00"

default_config:

exists in my configuration.yaml

what can I do the schedule to show up in the weggui?

You have to create it via the UI for it to show up:

what type of helper is it?
In my webgui e.g. “timetable” (I guess it could be timetable) is greyed out

Schedule in English: look for the same icon:

You should be able to see but not edit schedules created via YAML:

image

Have you fully restarted HA since adding the schedule to the YAML? Any errors in the logs?

OK

  1. “schedule:” still had to be in the yaml
  2. available after restart - not when reloading config only!

Now I can create a schedule and see it in the gui.
Thanks!

1 Like

You can reload config from now on — but you need a full restart after adding a new integration to your YAML for the first time.