Get the time share of a valid status

I ran into problems and not getting it done…
I try to get a nice gauge in my influx data explorer that shows me the share of time when my let’s call it “door” was open.
So, if the the door was open for 6h in the last 24h it should show me 25%.

My query so far:

startRange = v.timeRangeStart 
stopRange = v.timeRangeStop   

totalDuration = duration(v: int(v: stopRange) - int(v: startRange))  

from(bucket: "myBucket")
  |> range(start: startRange, stop: stopRange)
  |> filter(fn: (r) => r["_measurement"] == "hmip.doorsensor")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: 1m, fn: last)
  |> map(fn: (r) => ({_time: r._time, open: r._value == 1 }))  
  |> window(every: 1m)
  |> reduce(fn: (r, accumulator) => ({countOpen: if then accumulator.countOpen + 1 else accumulator.countOpen}), identity: {countOpen: 0})
  |> map(fn: (r) => ({_value: r.countOpen / float(v: totalDuration) * 100.0, durationRange: totalDuration}))  

It seems that is not counting up correctly.
I appreciate if somebody can help me with that.