Switch the Smart Plug every X days "on"

Hi Folks,

i have an Eve Smart Plug. On this Smart Plug my toothbrush charger is connected.
I would like to have an automation, that switch the Charger every 10th day at midnight “on” and 23 hours later “off”. So the toothbrush is charging for 23 hours, every 10th day.

In the Eve App i only can set a specific day (e.g. Sunday). But 7 days is not enough to charge the toothbrush. The battery lasts 10 days.

Can anyone tell me, how I can manage this automation?

(Sorry I’m a newbie to homeassistant with very bad coding skills :see_no_evil:)

Thanks :slight_smile:

Charge it every 7 days, but for a shorter period?

1 Like

This is an interesting scenario, I wonder what would be the best way to handle it. I guess a proper way would be somehow with a template trigger calculating the pattern (basically if current timestamp plus offset is divisible by the number of seconds in 10 days). But I’m not sure.

I’m also wondering, what’s the point? Those chargers are designed to be on all the time. It’s such a slow charge that it doesn’t really affect battery much. Also I don’t think you can save energy this way, because the smart plug or relay is likely going to use more energy when idle.

@ShadowFist :slight_smile: i love it. and frankly, i agree…
however there’s still merit to doing it just because you can! :stuck_out_tongue:

you can do almost all of this via the ui. need to drop down to template coding just a touch.

create a datetime helper (set it to today just to get started). set it for a date only. can do this purely through the ui.

create your automation (via the ui):

trigger use a template trigger and use this for your template:

{{ now() - states(input_datetime.date_last_charged) > timedelta(days=10) }}

on the action, charge your toothbrush however you do that (switch.turn_on ??) and turn it off whenever you want (after some delay?)

then in the actions also reset the date to the current date with this:

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.date_last_charged
data:
  date: "{{ now().date() }}"

finally in more support of @ShadowFist 's answer… li-ion batteries (which i’m gonna guess you have) have the highest lifetime if you don’t drain it all the way down nor if you also don’t keep topping it off. so if it can last for 10 days, you’re still probably best off charging it every 7 if you want to preserve the life of the battery…

1 Like

Thank you very much for all of your answers. I’ll think about it, to charge it every 7 days to protect the battery life.

Maybe I will play a little bit with your suggestions in the automations. :slight_smile:

A template, triggered once each day (at midnight) with a condition test before action.

{{now().strftime('%j')|int()%10==0}}

The %j returns the day in the year, %10 returns the remainder, which will be 0 only every 10 days, hence only ‘true’ every 10 days.

This works for any number n of days, starting at the nth day. Naturally it will not overlap the end of the year correctly, going from day 360 to day 10 of the new year.

To make this more rigorous we can use Unix timestamp, reduced to an integer number of days in the epoch.

{{ utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') | as_timestamp | int() // 86400 %10==0 }}

Using UTC time rather than local time will for many places result in a test at midnight using the day prior, but since we are looking for every 10 days, not a specific day, this should not matter.
The strftime format just gets the timestamp into something that as_timestamp will accept, then // does an integer truncate division to get days in the Unix Epoch.

Of course, the %10==0 can be changed to ==1 to ==9 if you want to synchronise to a given date (eg your birthday, or the day you bought the toothbrush), and the 10 can be changed to anything you like, so do “this” every “x” days!

Since you are interested in every 10 days, and since months are (almost) 30 days long, we can simply test just on the day of the month. To avoid short months like February, every (say) 5th, 15th, 25th of the month is going to be 10 days (or 11 or 8/9) apart

{{now().strftime('%-d')|int() in [5, 15, 25]}}

Returns true accordingly for the 5th, 15th and 25th of the month.

This is a useful way of setting up automations for bills and such that fall on the same day of each month. Naturally, such conditional logic can be extended to, for example, switch a routine from 3 times a month during the winter, to 5 times a month during the summer.