Mqtt automation after 30 seconds

i have a motion detector that is not working like i was expected and I did a bit of automation
Indeed somehow it detects motion every 5 seconds, but goes to the off status after 90 seconds :confused:

So my plan is to store the timestamp of the last motion it received and when the current time - stored time > 30, turn off the light

basically this is what i did :
if motion detected, publish the current timestamp to an mqtt topic
and i added a sensor (test) that has the value of the mqtt topic

but i didnā€™t found a way to automate the end of the process :ā€™(

I tried something like this

  - above: '30'
    entity_id: sensor.test
    platform: numeric_state
    value_template: '{{ now().strftime("%s")| int - states(sensor.test) | int  }}'
  condition: []
  - entity_id: light.lumiere_salle_de_bain_du_bas
    service: light.turn_off

any clue what iā€™m missing ?

Thanks a lot in advance :wink: