Time controlled switching

Hello, I have been looking for an attractive smartHome System for a few years now, so openhab & Co are out of the question.
Now I have discovered Home Assistant and at first glance I like it quite a lot.
Unfortunately I don’t really get along with the set-up.
I have a WLAN socket here at home and would like to use it for time control.
The socket should come on every day at 10 pm and go out in the morning at 2:30 am.
The whole thing should then be shown on a map.
I would like to be able to see and change the two times on the map.
So that you can change the times quickly and easily.
It would be ideal if you could switch the automation on/off with a switch at the top right - but that is not so important.
Now I search for tips at Google, but fail directly after “trigger: date_time” …

Thank for posting this question. Made me take another look at the Automation Creator.

So if you go to Automation page you can build this out directly using the GUI. I posted the yaml made after I used GUI to make the automation based on turning ON and OFF my living room light. I havent tested and maybe there is another way but this seems like it should work.

previously i made these using 2 automation (1 for ON action and 1 for OFF) but after your question I try using the new “chose” function of automation and seems pretty easy

alias: 'turn living room light on at 10pm and off at 2:30am'
description: ''
trigger:
  - platform: time
    at: '22:00:00'
  - platform: time
    at: '2:30:00'
condition: []
action:
  - choose:
      - conditions:
          - condition: time
            after: '21:59:00'
            before: '22:01:00'
        sequence:
          - service: light.turn_on
            data: {}
            entity_id: light.living_room
      - conditions:
          - condition: time
            after: '02:29:00'
            before: '2:31:00'
        sequence:
          - service: light.turn_off
            data: {}
            entity_id: light.living_room
    default: []
mode: single

EDIT
the map can be done with picture elements card but I never use this

EDIT 2
you can add the automation to the frontend as a toggle or button to turn it ON/OFF
the automation page provide toggle also to turn it OFF/ON

1 Like
- alias: 'turn living room light on at 10pm and off at 2:30am'
  trigger:
  - platform: time
    at:
      - '22:00:00'
      - '02:30:00'
  action:
  - service: "light.turn_{{ 'on' if trigger.now.hour == 22 else 'off' }}"
    entity_id: light.living_room

By “map” do you mean the Lovelace user-interface (UI)?

You can use input_datetime to present the two times in the UI and permit the user to easily change them. Let me know if you need help to enhance the automation to use input_datetime.

Yes, i mean the lovelace UI.
And yes, i have really no idea how to implement input_datetime and the “map”.

that’s what my configuration.yaml looks like now

automation:
  - alias: 'test'
    trigger:
    - platform: time
      at:
        - input_datetime.timer_start
        - input_datetime.timer_stop
    action:
    - service: "switch.turn_{{ 'on' if (trigger.now.hour == input_datetime.timer_start.timestamp.strftime('%H')) and (trigger.now.minute == input_datetime.timer_start.timestamp.strftime('%M')) else 'off' }}"
      entity_id: switch.steckdose03x

thats my card

type: entities
entities:
  - entity: automation.test
  - entity: input_datetime.timer_start
  - entity: input_datetime.timer_stop

but it wont work.

Here’s the documentation for the Input Datetime integration. You will need to create two input_datetime entities (time only, don’t include date), one to represent the start time and the other for the finish time. You can define them in the configuration.yaml file or via the UI in Configuration > Helpers.

Here’s an example of how to use the two input_datetimes:

- alias: 'turn switch on at 10pm and off at 2:30am'
  trigger:
  - platform: time
    at:
      - input_datetime.start
      - input_datetime.finish
  action:
  - service: >
      switch.turn_{{ 'on' if trigger.now.hour == state_attr('input_datetime.start', 'hour') and
                            trigger.now.minute == state_attr('input_datetime.start', 'minute')
                         else 'off' }}
    entity_id: switch.steckdose03x

If you find the template to be too difficult to understand, it can also be written like this:

- alias: 'turn switch on at 10pm and off at 2:30am'
  trigger:
  - platform: time
    at:
      - input_datetime.start
      - input_datetime.finish
  action:
  - service: >
      {% if trigger.now.hour == state_attr('input_datetime.start', 'hour') and
            trigger.now.minute == state_attr('input_datetime.start', 'minute') %}
         switch.turn_on
      {%  else %}
         switch.turn_off
      {% endif %}
    entity_id: switch.steckdose03x

with your solution 1 i get this error ->

can not read an implicit mapping pair; a colon is missed at line 42, column 179:
… start’, ‘minute’)) else ‘off’ }}
^

Where and when is that error message produced? For example, when I run Check Configuration, no errors are produced (the automation has no syntax errors).

Are you receiving the error when you execute Check Configuration or, afterwards, when the automation is triggered?

a colon is missed at line 42, column 179

As far as I can tell, there are no missing colons in the first example. Which version of Home Assistant are you using?

Your solution number 2 works great, so there’s no need to worry about the error.
This variant works really well.
Thanks for that.

1 Like

You’re welcome! I’m glad to hear it is working well for you.

Please consider marking my post, the one containing the automations, with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has an accepted solution. It will also place a link below your first post that leads to the Solution post. All of this helps users find answers to similar questions.