How to use history_stats to calculate duty cycle

Hello,
I am trying to calculate a duty cycle of my DIY thermostat (the share of time when heater is one in a cycle). The thermostat provides a MQTT sensor which is “on” and “off”.
To make it easier to debug now I am exprerimenting with mockup made of a lamp switch in HA and I am trying to use history_stats integration and looks like I do something wrong.

  1. I made a template sensor which keeps the time of off-on transition
template:
  - trigger:
      - platform: state
        entity_id: light.loft_staircase_light
        from: 'off'
        to: 'on'
    sensor:
      - name: "loftonts"
        # state: "{{ as_timestamp(states.light.loft_staircase_light.last_changed) }}" I've tried boith TS and formatted value
        state: "{{ states.light.loft_staircase_light.last_changed }}"

It holds correct time of lamp switched on (manually by button).
2) I made history_stats sensor where start value is fed from sensor above and end value is now() - but ratio is always 0% if I toggle the light on and off (say using 10 sec intervals).

sensor:
  - platform: history_stats
    name: HS Loft
    entity_id: light.loft_staircase_light
    state: "on"
    type: ratio
    start: "{{ states('sensor.loftonts') }}"
    end: "{{ now() }}"

I am quite new to HA and probably the approach is wrong but I cannot understand where is my mistake.

thanks in advance for any insights