Newbie: Having trouble writing a somewhat complex automation

Hello,
I am new to Home Assistant. I have created a few automations and they are working well. However I am having trouble writing a slightly more complex automation where I have a z-wave motion sensor trigger a z-wave receptacle to turn ON and a timer event to start. When the timer event finishes, I want it to trigger the z-wave receptacle off. It works as far as triggering the timer event to start running.

I am having trouble with the yaml for the timer finish routine to trigger the switch to the off position.

I’ve looked through the docs and can’t find any obvious errors in the yaml, but I can’t save it as it complains of some error in the yaml. I looked for the schema and I couldn’t spot the issue yet.

Can somebody help me out?
Thanks

Can’t tell you what’s wrong with your YAML if you don’t show us your YAML :slightly_smiling_face:.

1 Like

Can you copy-n-paste it, or tell us what steps you are taking or what screen you are on?
What documentation are you following?
[Any code and logs to be formatted nicely with </> so it is readable please.]

We can’t help without seeing the yaml file.

Open the automation, click on the three-dot menu then “Edit in YAML”. Then click on the “Copy to clipboard” icon.

In the forum click on </> and paste the yaml from the clipboard.


perhaps this:

description: Turns on switch on motion, turns off 5 minutes after motion stops
mode: restart

trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor
    to: "on"

action:
  - service: switch.turn_on
    target:
      entity_id: switch.your_switch

  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor
        to: "off"
        for: "00:05:00"

  - service: switch.turn_off
    target:
      entity_id: switch.your_switch

Are you using the UI or YAML? If you are using YAML, then you need an id: key. id: "this_is_my_automation"

The UI automatically generates a unique id for you.

Warning, if you make your automations in YAML they should be in a separate directory. In my configuration.yaml:

automation: !include gui/automations.yaml # this is only for GUI-Automations, do not touch this file!
automation manual: !include_dir_merge_list manual_automations/

Then put your YAML-generated automations into the config/manual_automations directory. In my case I have a file for each category. For example, all of my automations related to the garage are in config/manual_automations/garage.yaml

Last warning- the unique ID must be unique. When you restart or reload Home Assistant, you will get no warnings or errors if you have a duplicate id: key.

For a new user in 2026 that should really look like this, there’s almost zero references left in the docs to ‘platform’ or ‘services’.

id: five_minute_motion_lght
alias: Turn on switch light for 5 min. on motion
description: Turns on switch on motion, turns off 5 minutes after motion stops
mode: restart

triggers:
  - trigger: state
    entity_id: binary_sensor.motion_sensor
    to: "on"

actions:
  - action: switch.turn_on
    target:
      entity_id: switch.your_switch

  - wait_for_trigger:
      - trigger: state
        entity_id: binary_sensor.motion_sensor
        to: "off"
        for: "00:05:00"

  - action: switch.turn_off
    target:
      entity_id: switch.your_switch
1 Like

I am trying to understand a nuance. The wait_for_trigger section waits for either “off” of the sensor or for 5 minutes. Since, it is assumed, that the “off” is never coming, there is a 5 minute delay. Subsequently, in the action: switch.turn_off the switch is turned off (or off, again, if an “off” did arrive).

The assumption made is that the sensor does not send an “off”. I only have a few motion sensors and they send a “clear” when the internal timeout is reached.

Is it true that “ON”, ON, “on”, on, 1, “1”, “motion detected”, etc.(or “OFF”, OFF, “off”, off, 0, “0”, “clear”, etc.) are not treated the same in condition statements?

The wait_for_trigger behaves like any other trigger, in this case it will wait for the binary_sensor to be in an off state for 5 minutes before firing and executing the following action.

Apparent states like ‘On’, ‘1’, ‘motion detected’, ‘open’, ‘True’ ‘Off’, ‘0’, ‘clear’, ‘closed’, ‘False’ are front end formated results based on the entities device_class attribute. A binary sensor itself can only have a state of ‘on’, ‘off’, ‘unknown’, and ‘unavailable’. You can always see an entities actual state in the Dev tools>States table.

See the Binary Sensor Docs for how they ‘dispay’ dependent on their Device Class.

The wait for trigger could be a simple delay also.
It would give the same effect ± a few seconds for the cool down of the motion sensor.