Can someone help me understand how to automate a switch timer

Ok so I’ve been scratching my head for a while I cant seem to understand how to automate a timer or maybe scheduler is a better term for my sonoff switch . I want to be able to set a time for the switch to turn off at .

so far I have

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

input_datetime:
  switch_timer:
    name: Switch Timer
    has_date: false
    has_time: true
    
input_boolean:
  switch_timer_toggle:
    name: Switch Timer Toggle
    icon: mdi:toggle-switch

Screenshot%20from%202019-04-16%2020-10-03

I know I can set an automation that triggers on a change of time to input_datetime and in turn switch the sonoff switch off but I want to be able to toggle the timer off should I decide that after changing the input_datetime and triggering the automation that I no longer require the timer to turn off the switch which is why I added an input_boolean toggle. But for the life of me I cant figure how to implement this .

Also how can I get the automation to check the state of the switch to see if its off already and not trigger at the time should I turn the switch off manually via the toggle. Would this setup create a lot of polling ?

That’s a lot of requirements, not all of which I think you may really need.

Let’s start with the basics. E.g., turn the switch off at the time set on the input_datetime if the input_boolean is on. That’s easy:

- alias: Turn off switch at specified time if enabled
  trigger:
    platform: template
    value_template: >
      {{ states('input_datetime.switch_timer').rsplit(':',1)[0] ==
         states('sensor.time') }}
  condition:
    condition: state
    entity_id: input_boolean.switch_timer_toggle
    state: 'on'
  action:
    service: switch.turn_off
    entity_id: switch.power_socket
2 Likes

thanks for your reply ,

thanks for the value template I was struggling a bit with this part I understand the the first part is( input datetime and its name) and the last part is my( time sensor) but what does the rsplit bit do ?

I thought about adding a condition to check if the input boolean was on as this would stop my wife from changing the time accidentally and triggering an automation but I’m unable to disable the automation once its running is there a way to do this ?

cool. another tool in the tool box. :slightly_smiling_face:

1 Like

The states() function returns the entity’s state string, which is a Python string type. rsplit() is one of the methods available for the Python string type. Basically it splits the string around the character given as the first parameter, starting from the right. The second parameter says how many times to split. Since it’s one, it will only split the string once, around the rightmost colon. Then the [0] says take the first part (which is the leftmost string before the last colon.)

This is necessary because whoever wrote the input_datetime in the first place thought it would be a good idea to include seconds, even though you can’t specify seconds. Which also makes it so you can’t directly compare it to the state string of sensor.time. So basically I’m taking the “HH:MM” part of the input_datetime and comparing it to sensor.time that is already formatted as “HH:MM”.

The condition is only tested when the trigger “fires”, which in this case is when the expression evaluates to true. This can happen when either sensor.time changes (and then they match), or, as you say, when input_datetime changes, and they match. But is that really an issue? I suspect you don’t really need the input_boolean and the corresponding automation condition.

BTW, the automation is “running” when the action(s) are running, which happens once the trigger fires when the condition(s) is/are true. And in this case it will run for milliseconds only, because I’m sure it doesn’t take long to actually turn off the switch.

1 Like

thank you for the rsplit explanation it was written so well I understood all of it .

Ok I’m understanding template triggers a bit more now, so I probably should clarify that I don’t what the timer to turn off the switch every day at a set time, most of the time the switch needs to be on and occasionally I want to set a time that it turns off and then it reset so to speak and then when I need it again I can change the time (if needed) and it will turn the switch off again at that time .

I understand now that in a template trigger when either the time sensor entity changes or the input_datetime entity changes the expression is checked and when they match and evaluate to true the automation triggers so if the condition for the input_boolean toggle is not true /on it wont trigger the action .

I think I will need the input boolean toggle to stop this triggering the same time every day / when the automation is not required,
is this correct ?

To enable and disable the timer simply include the automation as an entity in the card. It has a switch to turn the automation on (enabled) and off (disabled).

For example, look at my sunrise automation. The switch below the dividing line enables and disables this automation:

It’s simply the automation entity itself in the card config:

entities:
  - entity: light.all_bedroom_lights
  - entity: light.bedside_lamp
  - entity: light.ceiling_light
  - entity: light.lifx_wardrobe
  - type: divider
  - entity: automation.master_bedroom_sunrise_simulation  ### <- automation enable /disable
  - entity: input_boolean.mstr_bed_sunrise_sim_ignore
  - entity: input_boolean.mstr_bed_sunrise_sim_workday
  - entity: input_datetime.mstr_bed_sunrise_sim_time
show_header_toggle: false
title: Master Bedroom
type: entities
1 Like

It’s slightly more complicated than that. The expression is evaluate when HA first starts and will “trigger” if it is true (even though neither of the entities changed.) If it evaluates to false at that time then it won’t trigger then. Once it does trigger (either at the start, or when one of the entities changes that causes it to become true), then it won’t trigger again until one of the entities changes that causes the evaluation to become false, and then another entity change causes it to evaluate to true.

So, basically, trigger at start if true at that time. Then trigger whenever an entity changes that causes the expression to go from evaluating as false to evaluating as true. (Otherwise it might trigger over and over again as long as it stays true, which is not what you’d want.)

Then you probably don’t need the input_boolean. You can use a method that takes into account @tom_l’s suggestion. Basically have an automation that is triggered by the input_datetime changing. The action of that automation will turn on the automation that we’ve been talking about. Then add another step to the original automation that turns itself off.

Thanks for the explanation.
However, I’m not quite sure I understand it right.
Namely, I had a feeling that HA detects (or we can explicitly declare) entities that influence the sensor’s value.
And then HA re-evaluates value_template when any of those entities change their state.
The sensor’s state then being updated (but according to the docs, there is no change in last_changed, for example, if a new state is the same as the previous one).
And yes, there is a special case when HA evaluates value_template on startup to determine the sensor’s state.

Does is sound correct-ish? :wink:

You sir are a legend , I cant believe it was this simple it worked exactly as it should , I’m not sure why this isn’t mentioned in the docs to show that an automation can be enabled or disabled.

@pnbruckner

I think your write ups are perfect for beginner understanding I think this should be in the docs as well as it isn’t very clear for noobs like me , how the automation works broken down into its basic parts.

Thanks for your help everyone .

2 Likes

Correct. But even if just an attribute changes, that will cause a state change, which will cause the value_template to be re-evaluated.