How to query how long a switch was on in a given day?

I have a switch names switch.kitty_feeder_relay that controls a motor that drives a screw which feeds my little monsters. Given a specific date range (current day, yesterday, etc) how can I find how many seconds the relay was on? This sounds like it should be a simple query but I’m not finding that to be the case.

History Stats - Home Assistant (home-assistant.io)

2 Likes
- platform: history_stats
  name: feederontoday
  entity_id: switch.kitty_feeder_relay
  state: "on"
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0) }}"
  end: "{{ now() }}"

So if I create this sensor, and then turn on the switch manually for 5 seconds, and then turn it off, I should expect the sensor value to show me 5 seconds?

Indeed… there is no risk in trying things out too :slight_smile:
If you change type: time to type: count then it would count the number of times you switched.
As per the doc, you can choose your own period to cover

Hours, right?

Or: Need precise seconds of switch ON state from history statistics - #33 by mekanics

time… so seconds building up to …the rest

Darn, just checked and … it states HOURS in the integration

EDIT: which is odd as one can limit the duration of measurement to seconds-level
So possibly it measures in seconds (?) but only reports in hours, I have my pc-on timer showing parts of hours e.g. 1.01

Yeah, just wanted to clarify that the unit of measurement is hours and not seconds. I haven’t worked with history stats much but set up a couple for TV on/playing this evening so this just caught my eye.
I also realize that the real ask of ‘on in a given day’ may not need to be in seconds :slight_smile:
But…

That will work if the time is indeed registered with that detail.
I just tested it manually and (roughly)

  • 10s shows as 0.0
  • 20s shows as 0.01
    The latter is clearly wrong as 0.01 = 3600 / 100 = 36s so maybe it is tracking in more detail but not showing it
    EDIT: the calc / convert sensor above shows 36s as well, so there is nothing ‘hidden’

Other option is automation I guess…trigger when feeder is ‘toggled’ and then run a template that continues to measure while ‘on’, and store that data as seconds in a input helper (increment service)

1 Like

Final comment… for this particular issue measuring time might be too much work to implement and not worth the effort (?). Assuming that the pet-feeder runs a fixed open/close for (say) 5s then one can count the number of times and multiply by 5…just my 2cts

1 Like

Hence my staring this topic.

Look at the way this feeder works again. An AC motor powers a screw which sits at the bottom of a sealed bucket. Pet kibble is gravity fed into the screw. When the relay is turned on, the motor slowly turns, pushing the kibble out into a bowl where the monsters impatiently wait. Every second equals a unit of kibble. I have an automation that delivers a variable X seconds 6 times a day. Assuming X = 10, that’s 60 seconds. Sometimes I notice the bowl is empty and the monsters are pestering me so I say, “Ok Google, meow meow” and Google responds “Bon Appetite” and turns on the relay for X/2 seconds, in this example 5 seconds, so if it’s 11:30 pm and the last scheduled feeding has happened and I’ve Google-fed them once, the relay has been on for 65 seconds.

So given a period 12am to now, yesterday, 2 days ago, etc. how long in seconds has the relay been on?

With the help of @thefes … this approach should work

Now I myself also have a solution for another challenge with the same ‘second’ counter issue…hence I spent a bit more time on it then I would usually do.

alias: Testing
description: ""
trigger:
  - platform: state
    entity_id: input_boolean.test_toggle
    to: "on"
action:
  - wait_for_trigger:
      - platform: state
        entity_id: input_boolean.test_toggle
  - service: input_number.set_value
    data:
      entity_id: input_number.test_number
      value: >-
        {{ states('input_number.test_number') | int(0) +
        (wait.trigger.to_state.last_updated -
        trigger.to_state.last_updated).total_seconds() | int(0) }}
mode: single