Automation Condition Help - Check if binary sensor was "on" at any point today

Hi, I’m hoping to get some help adding a condition to an automation to run if a binary sensor was “on” at any point in the last 8 hours.

Example automation: If the washing machine was on today ( < 8 hours ago), and the rain gauge senses rain (or is > 0mm), send a notification to get the washing off the line.

I’m fine with the notification and the rain part, but I’m struggling to workout how to check if the binary sensor was “on” at any point on the same day.

Thanks in advance!

One solution: assuming you have a boolean running/idle sensor for your washing machine, create a 1 day History stats count sensor for your washing machine:

The numeric condition for your automation would be that the count sensor is above zero.

I use this to create an entity from the following template to tell me if my mailbox has been opened. It resets after 3 hours.

- platform: template
  sensors:
    mail:
      friendly_name: Mail
      device_class: motion
      delay_off:
        minutes: 120
      value_template: >-
        {{ is_state('binary_sensor.mailbox_vibration', 'on') }}

Yes I’ve got a binary sensor tracking the state of the washing machine.

History Stats sounds like exactly what I need. Ive been searching for a few days! Thanks, ill have a read and see how it goes.

Thanks, thats a good idea, I didn’t think about creating another sensor. That seems like it may be the simplest way to achieve what I want!

Be careful with that approach. It may not survive a restart (I haven’t tested it). Where as History stats definitely will.

It doesn’t. :slight_smile:
survive a restart…

Thanks, I just got it working and so far so good!

Great help, thanks!

Make sure you are using the history stats for the day, and not the last 24 hours.

e.g. Today:

start: "{{ now().replace(hour=0, minute=0, second=0) }}"
end: "{{ now() }}"

Last 24 hours:

start: "{{ now() }}"
duration:
  hours: 24
1 Like

I see the time returned is not in true time/hours. e.g. if the washing machine was in the “on” state for 30 minutes, it will return 0.5 hrs. Or if it was 1hr 15 minutes, it will return 1.25 hrs. Is there a way to return this as HH:MM format?

If you want something different you will have to create a template sensor.

1 Like