Postgres lag() function has huge impact on query
It looks like almost all the time is spent just reading the data.  I don't think the window function really has anything to do with the poor performance.  The timestamp selection would probably be better written as this:
insert_time&gt;='2020-10-01' and insert_time &lt; '2020-11-01'

especially if you had an index it could use, probably on (smart_device_id, insert_time).
Finally, clustering your table so that all the data which will be needed together is found together should help a lot.  Maybe use partitioning on smart_device_id so that it stays clustered.