Automation/Script for turning on/off lights

I am trying to build an automation/script that can turn on/off my lights. I am using an ESP Human Presence device that I would like to turn on/off my lights when presence is detected or not.
This is the current script I have:

alias: Master Lights On/Off
sequence:
  - if:
      - type: is_occupied
        condition: device
        device_id: c972ad9670833a69ce359ea2a05f9f35
        entity_id: dd4166a965e0ba723d26674d34dc3d69
        domain: binary_sensor
      - condition: and
        conditions:
          - condition: time
            after: "17:00:00"
            before: "01:00:00"
    then:
      - service: light.turn_on
        metadata: {}
        data: {}
        target:
          entity_id:
            - light.laineys_bedroom_main_lights
    else:
      - if:
          - type: is_not_occupied
            condition: device
            device_id: c972ad9670833a69ce359ea2a05f9f35
            entity_id: dd4166a965e0ba723d26674d34dc3d69
            domain: binary_sensor
          - condition: and
            conditions:
              - condition: time
                after: "17:00:00"
                before: "01:00:00"
        then:
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.laineys_bedroom_main_lights
mode: parallel
icon: mdi:lightbulb-multiple-outline
max: 2

Is my idea achievable with this script?
Thank you.
Edit: I have it to do the automation/script at certain times only as you can see.

How are you going to run this script?

Why not write an automation that is triggered by your presence sensors instead?

Also have a read of this: Why and how to avoid device_ids in automations and scripts

Thanks for the reply.
I am new with scripts and automations, I have a very basic understanding of how to use them.
That is why I wasn’t sure if I needed an automation or a script. I have created 2 automations to do what I want 1. Turn on 2. Turn off. Can I do it all within the same automation? If so, can you send an example?

This is the “turn on” automation:

alias: Turn on Master bed lights
description: Turning on master bed lights with motion
trigger:
  - platform: state
    entity_id:
      - binary_sensor.humen_sensor_1m_3e6dc0_presence
    from: "off"
    to: "on"
    enabled: false
condition:
  - condition: time
    after: "17:00:00"
    before: "23:00:00"
    enabled: false
action:
  - service: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: light.master_bedroom_main_lights
    enabled: false
mode: single

Also, I have read the post you linked and thank you. It’s easier to use entities rather than device ids.

Thanks, that worked.