Sleep Sensor automation, run once a day

Hey all,

I have a sleep sensor that triggers some automation when getting into bed. But the problem is, if I get out for a sec and back in, re-triggers.

I’ve seen people mention an input boolean, but I can’t see anything like that in the automation GUI?

Can someone please point me in the right direction?

Thanks all

I usually use this,

  condition:
    - condition: template
      value_template: "{{ ((as_timestamp(now()) | float) - (as_timestamp(state_attr('automation.notification_garage_open_when_away','last_triggered')) | default(0) | float)) > 600.0 }}"

Change the automation name to what yours is called and set the 600.0 at the end to however long you want to block execution in seconds.

I have also seen where people put the script/automation into single execution mode and then put a wait at the end for x seconds, this however doesnt work if you reboot/restart HA.

That’s perfect thank you, ill test it tonight.

So just to understand, that’s seconds?

"{{ ((as_timestamp(now()) | float) - (as_timestamp(state_attr('automation.notification_garage_open_when_away','last_triggered')) 

I get what that’s doing.

| default(0) | float)) > 600.0 }}"

But what’s that? And is that a pipe, like PowerShell?

The first part you listed is just taking the current time and minusing the last time the automation was run, which gives you in seconds how long since it last ran. You then compare the result to whatever value you want to exceed before allowing the automation to run again. E.g. if you only want to run once a day, you probably dont actually want 24 hours, or it might not run when you go to bed early, so perhaps say 18 hours, so therefore change 600.00 to 3600 (seconds in an hour) * 18 = 64800.0

The default part just returns 0 if no value is returned by the attribute last_triggered (e.g. it hasnt run before).

1 Like

Thanks for that!

I got the first part, I ended up going for 10 hours, seems to have worked last night, so again thanks :slight_smile:

I was more looking to find out how this works?

| default(0) | float)) > 600.0 }}"

If you have time to explain it, or point me to doco that explains it, would be great. But only if you have a sec :slight_smile:

Default(0) just means if the value is for whatever reason invalid, it will substitute it for 0. Not overly necessary, but if the automation has never been run it will fix it dropping an error. It is then cast as a float value.

Lastly it takes the whole left of the equation and checks it is greater than 600.0 seconds.

1 Like

Cool thanks for that, everything helps so I can learn how to do these things myself :slight_smile: