Duration of a switch state - In seconds

Hello,

I want to automate my pool and need timers for the switches to record how long they were on and off.
I would then like to use these to control my automation with conditions.

This is working so far for example:

  - trigger:
      - platform: state
        entity_id: switch.pool_pump_switch
        from: 'on'
        to: 'off'
    sensor:
      - unique_id: pool_pump_time_off
        name: "test1 pool pump off"
        state: "{{trigger.to_state.last_changed}}"
        device_class: timestamp

However, I would need the values ​​in seconds since the switch was last in the on or off state.
Is there a way to do this?

Hi, cant test now, but you can try as below. BUT, just to mention I would prefer a history stat for this rather than a trigger, but i guess it is personal preference.

{{ now() | as_timestamp - states('sensor.pool_pump_time_off') | as_timestamp }}

This seems not to work.
Or maybe I make a mistake …

is the sensor name correct? I just guessed it. Verify in dev tools, states.

Then go to dev tools, template, post the result of this:

{{ states('sensor.pool_pump_time_off') }}
{{ states('sensor.pool_pump_time_off') | as_timestamp }}

Thats a good question.
If I can use the sensor with the unique id “pool_pump_time_off” from the example as “sensor.pool_pump_time_off”, than the name is correct.

If I put your example in dev tool, template I get:

ValueError: Template error: as_timestamp got invalid input ‘unknown’ when rendering template ‘{{ states(‘sensor.pool_pump_time_off’) }} {{ states(‘sensor.pool_pump_time_off’) | as_timestamp }}’ but no default was specified

go to dev tools, states and search for pool

it might be sensot.test1_pool_pump_off

There I can’t find the entity “sensor.test1_pool_pump_off”.
But if I add “test1_pool_pump_off” to a tile card I geht a time as result.

you dont give much info, I have asked twice what you see in dev tools, states.

if you switch that tile card to yaml mode, you should also see the exact name.

If I switch the tile to yaml i get for the sensor:

sensor.pool_pool_pump_time_off

And for:

{{ states(‘sensor.pool_pool_pump_time_off’) }}
{{ states(‘sensor.pool_pool_pump_time_off’) | as_timestamp }}

I get now in dev tool/template:

2025-05-13T11:51:12+00:00
1747137072.0

Result type: string

This template listens for the following state changed events:

  • Entity: sensor.pool_pool_pump_time_off

So now the following thing work:

If I put in dev tool/template

{{ states(‘sensor.test1_pool_pump_off’)}}
{{ states(‘sensor.test1_pool_pump_off’) | as_timestamp }}

{{ now() | as_timestamp - states(‘sensor.test1_pool_pump_off’) | as_timestamp }}

I get:
2025-05-13T12:41:37+00:00
1747140097.0

23.650042057037354

23.650042057037354 is the correct time in seconds. Perfect!

But if I now chnage:

  • trigger:
    • platform: state
      entity_id: switch.pool_pump_switch
      from: ‘on’
      to: ‘off’
      sensor:
    • unique_id: pool_pump_time_off
      name: “test1 pool pump off”
      state: “{{trigger.to_state.last_changed}}”
      device_class: timestamp

To:

  • trigger:
    • platform: state
      entity_id: switch.pool_pump_switch
      from: ‘on’
      to: ‘off’
      sensor:
    • unique_id: pool_pump_time_off
      name: “test1 pool pump off”
      state: “{{ now() | as_timestamp - states(‘sensor.test1_pool_pump_off’) | as_timestamp }}”
      device_class: timestamp

It is not working. :frowning:

Because you configured its device_class to be timestamp but the sensor’s state value is a value in seconds which doesn’t meet the requirements of a timestamp sensor (i.e. the value must be a datetime in ISO format, such as what you originally had trigger.to_state.last_changed).

If you want to express the value as a number in seconds, you will need to remove device_class: timestamp

I wont be able to assist further with the trigger sensor so hopefully the suggestion from taras can help you.

Just to repeat from my earlier post, I would have used a history stat and not a trigger sensor.

Then in my automation I can trigger when the history stat exdceed for example 3 hours and then turn pool off.

Could you maybe give me an example how this could work with the history stat?

Maybe I’m just adding more noise to this thread, but it’s possible that the information you need is already present in the state machine and therefore already available for use in an automation (and therefore you don’t need to create any new sensors to store anything).

It would be useful to understand what triggers you plan to use for your automation and what information you need about the pump’s history.

When your automation runs, you will be able to determine the current state of the pump (obviously) and also determine how long it has been in that state.

If you trigger on the state change of the pump, you know it will have been in the new state for 0 seconds, but you will also have trigger data to know how long it had been in the previous state.

Do you need more information than that?

  • Settings
  • devives
  • helpers
  • create helper
  • history stats
  • fill in a name as well as your switch entity (switch.pool_pump_switch)
  • state on
  • time
  • next
  • start: {{ now().replace(hour=0, minute=0, second=0) }}
  • end: {{ now() }}
  • submit
  • now you have to the total run time for today and it start over every day.
  • then create an automation to trigger when the history stat is > than your number, for example 3 for 3hours or 3.5 for 3andHalf hours

The history stat will give you total on time for the day even if it is split between various cycles on the same day.

1 Like

Wow, thank you very much.
This helps me am lot for this project and I have a good idea to implement it.

But are the two things possible with the history stat:

  • restart the measurement after every switch, that every cyle starts with 0
  • the meaurement is not only updateted, when the state of the switch is changed
    it should update for example every second, when the state is for example on

If you setup the history stat the way I proposed, then

  1. every day will start at 0, not every cycle. if you rather want to measure cycle, you can change the history stat template code to look at current cycle, or just use the trigger data as sugested by @mekaneck
  2. it will be a counter for the day. It will update continuously and not only when state is changed. Not every second, but at least every minute or two, and for me that is sufficient enough for automation purposes.

Thank you, this works exactly as you described it.
One more question. Is it possible to see the code that the gui has generated?
I think this would help me for better understanding working in yaml myself.

Not that I am aware of.
It might have created some json config options in the .storage folder, but I would not suggest going there. Rather read the docs and forum if you want to learn yaml.