Automation to turn off a plug at 5am unless I'm home then turn off at 10am

I’m a week old into home assistant and already had the battle of the tuyas last night so my brain has a bit of a ha hangover and I just can’t think of the correct logic or building blocks to make this work.
I feel like there is a simple way and my brain is just too fogged to see it haha.

I have a panel heater that turns on from sunset. I want it to turn off at 5am if I’m not detected at home or if I am detected at home to then turn off at 10am instead.

Is this possible? Thank you!!

Hi, I think you can maybe try something like this?

description: "Panel heater turn off"
mode: single
trigger:
  - platform: time
    at: "05:00:00"
condition: []
action:
  - if:
      - condition: zone
        entity_id: ##person.xxxx
        zone: ##zone.xxx
    then:
      - sequence:
          - delay:
              hours: 5
              minutes: 0
              seconds: 0
              milliseconds: 0
          - service: climate.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: ##climate.xxxx
    else:
      - service: climate.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: ##climate.xxxx
1 Like

You my friend are the real GC, thank you so so much <3

Be advised that employing a delay of several hours is never recommended. If Home Assistant is restarted while the delay is counting down, the automation is immediately terminated and never completes the balance of its actions.

If you’re interested in learning how to handle this situation in a manner that doesn’t use a delay and is unaffected by a restart, let me know.

Ooh yes please!!

Post the entity_id of the following entities:

  1. Your panel heater
  2. Whatever entity that indicates when you’re home

I’ve just edited it with this added code.

Should work I think? Guess I’ll find out tomorrow heh

alias: Wall Heater Hall OFF edit
description: Panel heater turn off
trigger:
  - platform: time
    at: "05:00:00"
condition: []
action:
  - if:
      - condition: zone
        entity_id: person.xxxxx
        zone: zone.home
    then:
      - sequence:
          - stop: Suz is home
    else:
      - type: turn_off
        device_id: 63c681566ef71bef9c3dbf653b5ef1cd
        entity_id: 971658e9799af33af255aa6912814d5a
        domain: switch
mode: single

Then I made another automation that checks at 10am if the heater is on, then turns it off

edit: I think this may be better in a layman’s way to run an automation without delay.

In the first version of your reply, you said these were your entity_ids:

  • device_tracker.suzy_s_iphone_2
  • switch.heater_socket

The following automation turns off the switch at 5:00 if you are not home and the heater is on. Otherwise it turns off the heater at 10:00 if it’s on.

alias: Wall Heater Hall
description: Control panel heater
trigger_variables:
  suzy: device_tracker.suzy_s_iphone_2
  heater: switch.heater_socket
trigger:
  - platform: time
    at: "05:00:00"
    variables:
      is_true: "{{ is_state(suzy, 'not_home') }}"
  - platform: time
    at: "10:00:00"
    variables:
      is_true: "{{ true }}"
condition:
  - condition: template
    value_template: "{{ is_true and is_state(heater, 'on') }}"
action:
  - service: switch.turn_off
    target:
      entity_id: "{{ heater }}"
mode: single
1 Like

Hi Taras,

that looks very clean, but I admit not having found this in the documentation, other than Automation Trigger - Home Assistant
(this does not explain the use of is_true, which is my particular interest now)

Personally I didnt use trigger_varables before, and have only set variables: inside the action blocks, or scripts for that matter

believe it or not, but I even called those trigger_variables in the anchor definition…

    action:
      - <<: &trigger_variables
          variables:
            name: >
              {{trigger.to_state.name}}
            state: >
              {{trigger.to_state.state}}
            id: >
              {{trigger.to_state.object_id}}

I do love the elegance of your suggestion above a lot, so like to read up on that.
Appreciate the link to the docs if you could please?

There’s nothing special about that variable’s name. You can replace it with “foo” and the automation will continue to work as designed.

The variable’s value is set (boolean true or false) when the trigger occurs. The automation’s condition requires that the variable’s value is true in order to execute the action.

1 Like

thank you, I will be able to adapt a few automations using this going forward.
nice.

The technique effectively makes the trigger and condition do the decision-making that would usually be done with a choose in the automation’s action.

Wow, thank you

I have much much to learn, I noted when swapping to visual editor that at the bottom it said some is not supported and to use the yaml editor and thats when I noticed some of the statements were missing.

Thank you for your help and I shall go google the new information <3

Here’s the page with info for any future searchers Templating - Home Assistant

When I view the example in Visual mode, the only thing it doesn’t display is the trigger variables section. It’s still present when viewed in YAML mode, just not in Visual mode.

I’m just trying to understand the order of things in the yaml code now.

trigger:
  - platform: time
    at: "05:00:00"
    variables:
      is_true: "{{ is_state(suzy, 'not_home') }}"
  - platform: time
    at: "10:00:00"
    variables:
      is_true: "{{ true }}"

I rewrote what I interpreted multiple times and it still came out as nonsense hah.
Going to attempt shorter and direct definitions this time

‘not_home’ is at 5 true not home turn off
time 10 ‘true’ (not home?) to turn off
condition is_true and heater on to turn off, - true again i’m not home and to switch off?

hopefully my comments still don’t read as nonsense eek

The automation uses two Time Triggers:

  1. The first Time Trigger is scheduled to trigger at 05:00:00. When it does, it uses a template to set the value of a variable named is_true. The variable’s value is set to true if, based on your device_tracker, you are not home.

  2. The second Time Trigger is scheduled to trigger at 10:00:00. When it does, it simply sets the value of the is_true variable to true.

The automation’s conditions checks for two things:

  1. If the value of the is_true variable is true.
  2. If the heater switch is on.

If both conditions are met then the automation proceeds to turn off the heater switch.


Here are some scenarios and how the automation handles them.

  1. It triggers at 5:00:00, you’re not home and the heater is on so it turns it off.
  2. It triggers at 5:00:00, you’re not home and the heater is off so it does nothing.
  3. It triggers at 5:00:00, you’re home and the heater is on so it does nothing.
  4. It triggers at 5:00:00, you’re home and the heater is off so it does nothing.
  5. It triggers at 10:00:00, you’re home, or not home, and the heater is on so it turns it off.
  6. It triggers at 10:00:00, you’re home, or not home, and the heater is off so it does nothing.
1 Like

Thank you for the examples and your time in this, I appreciate it!!

I see I was understanding it wrong, that the not home was not the state but the variable true was, which refers to it.

I have it!! Thank you <3

1 Like

Take that back nope it still swapped and failed for heater & suzy
i’m gonna put a dumb action in instead lol

It doesn’t matter if the trigger_variables section appears at the bottom. That’s just the Automation Editor’s preference for how it formats YAML.

However, what does matter are whatever changes it makes that produce “undefined errors”. Post the error messages.

Automation - and only one hah

alias: Wall Heater Hall Schedule OFF
description: Control panel heater
trigger:
  - platform: time
    at: "09:13:00"
    variables:
      is_true: "{{ is_state(suzy, 'not_home') }}"
  - platform: time
    at: "09:14:00"
    variables:
      is_true: "{{ true }}"
condition:
  - condition: template
    value_template: "{{ is_true and is_state(heater, 'on') }}"
action:
  - service: switch.turn_off
    target:
      entity_id: "{{ heater }}"
trigger_variables:
  suzy: device_tracker.suzy_s_iphone_2
  heater: switch.heater_socket
mode: single

First system log error

Logger: homeassistant.helpers.template
Source: helpers/template.py:2629
First occurred: 9:13:00 AM (1 occurrences)
Last logged: 9:13:00 AM

Template variable error: 'suzy' is undefined when rendering '{{ is_state(suzy, 'not_home') }}'

Second system log error

Logger: homeassistant
Source: helpers/template.py:605
First occurred: 9:13:08 AM (1 occurrences)
Last logged: 9:13:08 AM

Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 603, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2616, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.12/site-packages/jinja2/sandbox.py", line 394, in call
    return __context.call(__obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2856, in wrapper
    return func(hass, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1806, in is_state
    state_obj = _get_state(hass, entity_id)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1191, in _get_state
    return _get_template_state_from_state(hass, entity_id, hass.states.get(entity_id))
                                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2134, in get
    entity_id.lower()
    ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2648, in _fail_with_undefined_error
    return super()._fail_with_undefined_error(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'suzy' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/trigger.py", line 278, in async_with_vars
    run_variables.update(trigger_variables.async_render(hass, run_variables))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script_variables.py", line 63, in async_render
    rendered_variables[key] = template.render_complex(
                              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 240, in render_complex
    return value.async_render(variables, limited=limited, parse_result=parse_result)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 605, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'suzy' is undefined