Help with Yaml code for PIR Occupation

I am looking for some help resolving what should be a basic automation. I have a Tuya Zigbee Motion detector 809WZT which automatically changes state from Detected to Clear in approximately 60 seconds and a Sonoff ZBMiniL2 relay attached to the bathroom lights.
I want to acheive an automation that turns on the lights when occupancy is detected and remains on whilst occupancy is detected but turns off after 3 minutes if occupancy is not detected.
This was my original Yaml produced by using the inbuilt blueprint

alias: Bathroom Lights On
description: ""
triggers:
  - type: occupied
    device_id: d59c8588c114dce804f413049d024d3a
    entity_id: 108364784b86112474088a686516c96c
    domain: binary_sensor
    metadata:
      secondary: false
    trigger: device
conditions: []
actions:
  - type: turn_on
    device_id: 0c6e1bd79db5e318cee1aba462b54f0d
    entity_id: 921d8b547f88b0bc8cf1a43bb38d97e3
    domain: switch
mode: single

This turns the lights on but not off again after a set time.
I searched these forums and the internet and found some examples and ended up with this Yaml file

alias: Bathroom Lights Occupancy
triggers:
  - entity_id: binary_sensor.bathroom_pir_occupancy
    to: "on"
    trigger: state
  - entity_id: binary_sensor.bathroom_pir_occupancy
    to: "off"
    for: "00:03:00"
    trigger: state
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.bathroom_pir_occupancy
            state: "on"
        sequence:
          - target:
              entity_id: switch.bathroom_lights
            action: light.turn_on
      - conditions:
          - condition: state
            entity_id: binary_sensor.bathroom_pir_occupancy
            state: "off"
        sequence:
          - target:
              entity_id: switch.bathroom_lights
            action: light.turn_off
mode: restart

I had to change the entity IDs to match my devices (as flagged by Spook) but it doesn’t work in that even though the PIR identifies occupancy, it now does not trigger the switch. If it assists here is the Traces log

this:
  entity_id: automation.bathroom_lights_on
  state: 'on'
  attributes:
    id: '1768217802807'
    last_triggered: '2026-01-12T13:01:10.888434+00:00'
    mode: restart
    current: 0
    friendly_name: Bathroom Lights Occupancy
  last_changed: '2026-01-12T12:30:22.730335+00:00'
  last_reported: '2026-01-12T13:01:10.897718+00:00'
  last_updated: '2026-01-12T13:01:10.897718+00:00'
  context:
    id: 01KES4QKB7G0S5EECZNGMKMX96
    parent_id: 01KES4QKAZ9B1Z0D359AETZHRR
    user_id: null
trigger:
  id: '1'
  idx: '1'
  alias: null
  platform: state
  entity_id: binary_sensor.bathroom_pir_occupancy
  from_state:
    entity_id: binary_sensor.bathroom_pir_occupancy
    state: 'on'
    attributes:
      device_class: occupancy
      friendly_name: Bathroom PIR Occupancy
    last_changed: '2026-01-12T13:01:10.879325+00:00'
    last_reported: '2026-01-12T13:01:10.879325+00:00'
    last_updated: '2026-01-12T13:01:10.879325+00:00'
    context:
      id: 01KES4QKAZ9B1Z0D359AETZHRR
      parent_id: null
      user_id: null
  to_state:
    entity_id: binary_sensor.bathroom_pir_occupancy
    state: 'off'
    attributes:
      device_class: occupancy
      friendly_name: Bathroom PIR Occupancy
    last_changed: '2026-01-12T13:02:12.154571+00:00'
    last_reported: '2026-01-12T13:02:12.154571+00:00'
    last_updated: '2026-01-12T13:02:12.154571+00:00'
    context:
      id: 01KES4SF5T092RDBA3PXCY2ZYH
      parent_id: null
      user_id: null
  for:
    __type: <class 'datetime.timedelta'>
    total_seconds: 180
  attribute: null
  description: state of binary_sensor.bathroom_pir_occupancy

Can anyone see what the issue is. Spook identified missing entities and I realised that the entity was not a light but a switch which I corrected but perhaps there are still errors?.

The Home Assistant Cookbook - Index has some good info regarding this. Specifically:

  1. Why and how to avoid device_ids in automations and scripts and
  2. Motion activated lights automation
1 Like

I’d set a timer when there is motion detected. Even if the timer is already runnng. Then turn the light off when the timer finishes.

Avoid using device IDs in your automations. If your device ever changes you will have to hunt down your automations and update them.

Many thanks @MaxK . I actually used this first before searching other solutions and coming her asking for help. I tried it again and it still didn’t work.

However after some head scratching I have now got it working. There is a line in the code:-
action: "light.turn_{{ trigger.to_state.state }}". The assumption being that to device is a light. However in my scenario its a switch.
I changed to
- action: "switch.turn_{{ trigger.to_state.state }}" and low and behold it now works.
Many thanks for your advice. As an aside how do people save their code snippets so they can use them again in the future without going through the pain. Is there anything built in to HA for this purpose or do people just use text files names with the automation?

Thanks @busman Yes found that out the hard way. These device IDs wer actually autocompleted by the HA Automations GUI when I tried the first automation so I continued to use them in other yaml code. I didn’t know where to find the device ID until I stumbled across it under the entity properties. Maybe its somewhere else more obvious but I am not sure where.
Anyhoo now working correctly
Many thanks for helping

I personally do a backup before making any significant modifications. I sometimes use a text file to temporarily save pieces of information. I know that Git and VS Code can be used to manage this too.

Yeah. That’s unfortunate and, frankly, I would consider that a bug. The UI puts device right at the top so of course people will pick that.

(Devices are logical containers for entities, but it’s entities that do all the work…)

As for automations/script, spend some time looking at the “Traces” (upper right of Automation window) and the “Changed variables” once inside the trace to see what data is available when the automation runs.

As for saving your previous work, if you are going to modify an automation you might first “Duplicate” it (three dots) and work in a new automation. Then disable the old one.

If (or once) you are familiar with the YAML, one thing I do is use Packages. There’s no UI, but I like to group related configuration into a single file.

Oh, and just FYI, the for: on a trigger doesn’t survive a HA restart. So, use a timer or datetime if it’s anything you consider a bit more critical.