Nested conditions in on/off automation

Eh ?
The switch ‘should’ already be ‘off’ as triggered at ‘23:00’ ??

Note to others: As Taras says above (arriving home, outside the 19:00-23:00 will cause a trigger to fire causing either an ‘off’ command to a device that is already ‘off’ - Unless the user is hopping across the boundary line every minute, this is not a problem (and even then, as you were ‘away’, not a lot should be happening at home to require a couple of bytes of your precious bandwidth :rofl: )

I tend to agree with the binary sensor because it makes the automation simple. But it requires some advanced templating. Either way, people can help you with that. To hit this approach, start by integrating sensor.time.

sensor:
  - platform: time_date
    display_options:
      - 'time'

This sensor updates every minute of the day and has an entity_id of sensor.time.

Next, create a binary_sensor.

binary_sensor:
- platform: template
  sensors:
    unit_2_helper:
      friendly_name: Unit 2 Automation Helper
      entity_id: sensor.time
      value_template: >
        {% set is_home = is_state('device_tracker.moto_g_6', 'home') %}
        {{ is_home and 19 <= now().hour < 23 }}

Now this will create a binary_sensor that is on when you are home and between the hours of 19 and 23. Lets break down the template:

This line simply sets is_home to true or false based on device_tracker.moto_g_6 being home or not.

        {% set is_home = is_state('device_tracker.moto_g_6', 'home') %}

This line returns 'on' or 'off' to the binary_sensor. It will be 'on' when is_home is True and when the current hour is between 19 and 23 (including 19 and 23).

        {{ is_home and 19 <= now().hour < 23 }}

Now that we have our binary_sensor.unit_2_helper, we can make the automations.

- alias: Turn On Unit 2
  trigger:
  - platform: state
    entity_id: binary_sensor.unit_2_helper
    to: 'on'
  action:
  - service: switch.turn_on
    entity_id: switch.on_off_plug_in_unit_2
- alias: Turn Off Unit 2
  trigger:
  - platform: state
    entity_id: binary_sensor.unit_2_helper
    to: 'off'
  action:
  - service: switch.turn_off
    entity_id: switch.on_off_plug_in_unit_2

And if you want to get your toes wet, here’s 1 automation that does it all (but uses templates).

- alias: Turn On/Off Unit 2
  trigger:
  - platform: state
    entity_id: binary_sensor.unit_2_helper
  action:
  - service_template: switch.turn_{{ trigger.to_state.state }}
    entity_id: switch.on_off_plug_in_unit_2

Look at what happens when the device_tracker changes state to home but the current time is not within the desired range. Your service_template calls either turn_on or turn_off and, this situation, will choose to call turn_off. So, as per my original point, it will call the turn_off service outside of the time range (even though the switch is already off). A condition can suppress that.

Long story short, there’s more than one way to achieve the desired goal. No matter which one you use, you have to ensure it covers all bases. All techniques discussed provide the user with sufficient latitude to screw up.

1 Like

I can’t help but wonder why this automation is even needed.

A rechargeable device controls its own charging needs. Plus it’s a toothbrush battery; its energy consumption is a rounding error compared to, say, a refrigerator. Especially if one focuses exclusively on just the miniscule amount of energy to ‘top up’ the charged battery outside the 4-hour window.

The real irony is that the automated switch used to control the toothbrush, runs 24x7 and probably consumes on the order of 2 watts. So although the toothbrush isn’t consuming energy (to keep the battery ‘topped up’) the switch does, all day long. :man_shrugging:

1 Like

To the OP (once you finally check back in… :wink:)…

I would recommend going with the “two automation solution” - one for turn on and one for turn off.

Then once you get a bit more comfortable with HA then you can take the deep dive into templating. At that point you can revisit some of your automations and revise them if desired.

However, I doubt you will want to mess with them later if they still work. By that time you’ll have so much other stuff in HA taking up your time (and money!) trying to get new stuff running that you won’t have time to mess with the old stuff that still works! :laughing:

2 Likes

Eh !
Won’t this be true right upto 23:59:59 ?

1 Like

ah yeah, just remove the = on the 23 :wink:

Good catch and more proof that all techniques discussed provide sufficient latitude to make logical errors.

Hey ! Nearly the whole gang is here (finity and Petro having joined) there’s only about 50 or more to come and ALL the heavyweights will be on one thread
:rofl:

Just to make it clear, I’m at the lightweight end of the scale !

Self-isolation, unemployment/retirement, boredom, ‘Job Jar’ avoidance, etc all lead to here.

Is this a typical amount of feedback a noob can expect in this community? This is amazing! It might take me until the weekend before I can properly take a look at all the proposed code and try to understand it.

As for the motivation, don’t get me started lol. Whenever a house burns down and makes it onto the (local) news they have some ‘expert’ on who will typically issue a warning about the dangers of charging batteries when not at home or overnight and my wife 100% subscribes to this fear mongering. It probably doesn’t help that she saw her neighbour’s house burn down as a child, so she just has an irrational fear about anything that might cause a fire in the house. And we never remember to plug back in the toothbrush when we get home. All this drove me to this project, my very first home automation project.

Check if the automated switch(es) you use in your home have been tested and approved by a third-party standards organization (UL, ETL, CSA, etc). Most have not and only have CE self-certification. It’s highly likely that your rechargeable toothbrush has third-party certification whereas the switch controlling it doesn’t. Amusing irony, but maybe not for your cautious wife.

Given the degree of caution employed here, I assume you have at least one chemical fire extinguisher in your home, in case there’s a battery fire while you are present. Li-ion batteries burn like hell.

So just to be sure - you are not using sonoff’s for this duty ? (there are numerous threads about how hot they get in operation)

Self certification only applies to assemblies of fully tested components that have been utilised in an approved manner with industry accepted techniques and are adequately protected from it’s expected environment (typically an MCC or ‘Control Panel’ where the cost of full 'type approval would be prohibitive (and possibly destructive) for a ‘one-off’ assembly) (and this normally covers “shall not cause interference or be affected by interference” etc. ). (the individual components need to be fully certified (‘certified safe’ e.g. IP4x, exposed surface contact temperatures, non flammable or fire retardant materials etc. ) and ‘Generally’ if it looks ‘wrong’ - it definitely is wrong.
A rechargeable toothbrush would not fall into this catagory (Though 1. It has been known for some manufacturers to ‘fake’ Approval Marks 2. There is another CE mark (looks VERY similar, but the spacing is different) This apparently stands for ‘Chinese Engineered’ or some such - and I have absolutely no idea what comfort (if any) should be derived from that).

Yeah, I believe this is pretty standard.

Not every question gets this kind of attention but, especially if you ask in a coherent fashion, I think this thread is more or less typical.