It would be good to understand how you got to the point you are at now - did you use the MQTT device tracker?
If so, you will need to use an availability template, this can be used to do any logic and calculations you need to work out if presence is on or off…
Templating uses jinja2 - there is a link in the docs I linked above.
I suggest you have a read - have a try coding what you want to achieve, then if you have issues, post what you have tried, formatted correctly so we can read it using preformatted text tags - see point 11 in this post and also read the rest of it.