Want to switch my motion detection "switch" from ON to OFF at 9 PM then back OFF at 7AM

Hi there,

I am using a wall tablet with Fully Kiosk, and want to switch the tablet motion detection entity switch from ON to OFF during the night between 9PM and 7AM, then back ON at 7AM.
How should I do that … ?

In the automation, I can not find a “from time … to time …”, but only a start time
I could create a 10 hours timer helper, but then wondering how to use it. It would be creating an automation with even start 9PM, action would be to set the switch OFF, but how to integrate the helper then the switch back on ?

Many Thanks !!

You can use two automations one for ON and one for OFF, triggered by the time.
You can also combine these in a single automation using a Choose.
Trigger time-off, set id=off and trigger time-on, set id=on
Then for the action do a choose based on the id to set the switch OFF or ON

You need to add the correct action but somethinglike this;

description: ""
mode: single
triggers:
  - trigger: time
    at: "07:00:00"
    id: switch-on
  - trigger: time
    at: "22:00:00"
    id: switch-off
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - switch-on
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
      - conditions:
          - condition: trigger
            id:
              - switch-off
        sequence:
          - action: switch.turn_off
            metadata: {}
            data: {}

As simple as this:

alias: example
triggers:
  - id: "on"
    trigger: time
    at: "07:00:00"
  - id: "off"
    trigger: time
    at: "22:00:00"
conditions: []
actions:
  - action: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.your_switch
2 Likes

MErci !!
ChatGPT m’a proposé ceci qui semble fonctionner … mais je me demande, si HA redémarre entre 21h00 et 7h00, si le trigger de 7h00 va encore fonctionner ?
Qu’en pensez vous ?

alias: Motion detection night schedule
description: OFF à 21h00, ON à 07h00
triggers:
  - trigger: time
    at: "20:00:00"
    id: night_off
  - trigger: time
    at: "07:00:00"
    id: morning_on
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: night_off
        sequence:
          - action: switch.turn_off
            target:
              entity_id: switch.lenovo_tab_m10_plus_3rd_gen_motion_detection
      - conditions:
          - condition: trigger
            id: morning_on
        sequence:
          - action: switch.turn_on
            target:
              entity_id: switch.lenovo_tab_m10_plus_3rd_gen_motion_detection
mode: single

If Home Assistant is offline at 07:00:00 or 22:00:00 then the first example I posted will fail to turn the switch on or off because the automation’s Time Trigger will not be triggered.

The following version is more robust because it not only triggers at 07:00:00 and 22:00:00 but also whenever Home Assistant starts. It determines if the switch should be turned on or off based on whether the current hour is inside or outside of the time range (07:00:00 - 22:00:00).

alias: example
triggers:
  - trigger: time
    at:
      - "09:00:00"
      - "22:00:00"
  - trigger: homeassistant
    event: start
conditions: []
actions:
  - action: switch.turn_{{ iif(9 <= now().hour < 22, 'on', 'off') }}
    target:
      entity_id: switch.lenovo_tab_m10_plus_3rd_gen_motion_detection

NOTE

The iif is not a spelling error; it’s an Immediate If.

But here you also do not check the state of home
Asssistnt right ?
What if HA is down around 9pm, betweet the range 9pm - 7am, or around 7am ?

Home Assistant’s automations cannot do anything when Home Assistant is down.

If Home Assistant is down, the second automation example I posted will trigger at the first possible opportunity and that is when Home Assistant starts running again.

trigger: homeassistant
event: start

Most of my automations are designed this way; in addition to their specific triggers, they also trigger on startup and determine what should be turned on or off. It ensures everything is set to its correct state after a power failure.

1 Like