Automation questions based on multipurpose sensor

Hi guys,

I am new in the smart automation and I just started with the following hardware:
ST Hub V3
Multipurpose sensor
Motion Sensor
Harmony Hub

The setup on the automation that I have - I have installed on the entrance door the Multipurpose sensor as when I am entering the house, the TV will power on and the entrance lights will power on.

Now the problem that I am facing in the automation is the following:
If for example I leave from the house and before I leave I will power off everything, when I open the front door, the automation will start as described above but in fact I am leaving the house and everything will power on…

Also, I am interested that the lights just to power on after sunset and not before even if this automation is triggered, is this possible?

Can you share some opinions on how can be resolved