Residencial Automations - Raspiberry Pi, Relé, PIR Sensor and Python

Hello people. I am building a home automation DIY with raspiberry pi 3+, relay board and PIR presence sensors. I made a simple schedule so that when the presence sensor detects a movement it would send a signal to trigger a relay that would light a lamp. It turns out that during testing with the system the sensor detected movement while there was no movement (empty environment). I tried to regulate the sensitivity and signal range on the sensor itself, but the problems persisted. At first I thought the problem was with sensors or GPIOs, but I’ve tested it with several GPIO sensors and ports and the problem still persisted.
Then I created a record of the signals that the PIR sensor was sending to GPIO and realized that they are random signal peaks. So my problem is: I need a logic that reads the sensor signals for a period of time in order to avoid signal spikes or be sure that it’s a real moviment and not a signal spikes(for example: If the sensor detects motion for more than 2 seconds it triggers a relay, or if the sensor detects 10 movement within 2 seconds interval it triggers a relay, otherwise it resets counting).
Below is one of the revisions of the project I was trying to implement, but it is not working at all.

import RPi.GPIO as GPIO
import time
GPIO.setwarnings (False)

GPIO.setup (7, GPIO.IN)
GPIO.setup (3, GPIO.OUT)
GPIO.setup (5, GPIO.OUT)

count = 0

while True:

while GPIO.input (7) == 1:

    count = count + 1
    time.sleep (0.1)
    print (count)
    if count> = 20:
        GPIO.setup (5.0)
        count = 0
        print ('Motion Detected!')
        GPIO.setup (5.1)
        print ('Movement Canceled')

I hope someone can give me a light to make it work. If anyone proposes a solution that works I can post to the community so everyone can do the same.

I thank you all for your attention and cooperation.