Difference of two timestamps to convert to float-value

Hi, i have asimple task - i thought in the beginning - : i have an underfloor heating system and measure the time, when it turns on and off. So i have two timestamps that i want to calculate the time difference from and then to convert to a float value.

All trials with as_timestamp did fail

this is the YAML:
sensor: - name: pwm_wz_on_time state: "{{ (trigger.to_state.last_changed - trigger.from_state.last_changed) }}"

which works perfectly but gives a timestamp (or similar) as HH.MM:ss

State output in Developer Tools look like that:

0:03:00.039128

Any ideas from the community ??

That’s a timedelta, if you want seconds, you have to get the seconds out.

{{ (trigger.to_state.last_changed - trigger.from_state.last_changed).total_seconds() }}

so small a hint but great result. WORKS Perfect. thanks a lot…

I’m facing a similar problem, but I wasn’t able to solve it yet.

I’ve got a sensor.e2m_tracker_last_seen with a timestamp that is normally triggered every 30 seconds until the device is away from the home.zone. I want to get a sensor wich gives me the seconds since the last trigger. I’ve created a Sensor with a template

template:
  - sensor:
      - name: "tracker_test"
        unit_of_measurement: "Days"
        state: > 
            {{ (as_timestamp(now()) - as_timestamp(states('sensor.e2m_tracker_last_seen'))  | default(0)) | timestamp_custom('%S', true) | int }}

This sensor gives me the seconds, but if the Tracker is away it remains on the last value. So I can’t get the value of seconds since the last trigger. It’s always under 40 sec.

sensor.e2m_tracker_last_seen is a timestamp sensor.

How can I fit the template of @petro into my sensor?

Thanks for any help