Automation old-fashion timer

Hello,

sorry for that “maybe stupid question” about time trigger.

I have a automation which starts at 9am.
If something happend like restart of the system later or anything similar, this automation does not get triggered again, because it is maybe allready 9.30am.

What would be the right way to go to get a automation that works like a old fashioned timer-switch and get a “switched-on-time-window” e.g. from 9-12?

thanks in advance

You could have a template trigger.

{{ today_at("9:00") < now() < today_at("12:00")}}

Not sure if it will work if this is directly inside an automation, but if it is in a binary sensor then it will for sure work since they will be unavailable at startup

thank you, I will have a try.

What is your automation supposed to be doing during the time window?

Actually it is a general question, not depending at the usecase.
But in this case I have a automation to switch a heater on at 9am and a secound automation that switches it off at 5pm.

I found the trigger: time_pattern. Maybe I could as well repeat the trigger e.g. every "x"min in a specified timeframe.

something like this:

alias: heater timer on at 9am
 description: ""
 triggers:
   - trigger: time
     at: "09:00:00"
   - trigger: time_pattern
     hours: "9,10,11,12,13,14,15,16"
     minutes: "15"
 conditions: []
 actions:
   - type: turn_on
     device_id: xxx
     entity_id: xxx
     domain: switch
 mode: single

don’t know if this would work.

The 9:00 is superfluous with the time pattern, but it will work. So long as you don’t want to be able to manually turn off am the heater without it turning itself back on.

oh yes, good point. the turning off agument is fundamental.
so this solution does not work for me.

edit:
and the automation does not save: invalid time patter value for hours

Or just as I said.
Make the template I posted a template binary sensor and it will turn on at 9.
If you restart at 8:59 and it comes back online 9:15 then it will be unavailable until the template has been evaluated and then turn on.
Should be just a few seconds.

Then use the action switch.turn_on, don’t use device.

  - action: "switch.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: switch.porttelefon
    data: {}

This will cause errors in the log if you reload the configuration when the binary sensor becomes unavailable but that is not an issue.

okay, thanks again.
I have to try. never made a template binary sensor before.

@Hellis81
do I need the template extension for this? or if not where do I put the script?

Create a helper.
Settings → devices and integration → helpers → create new helper

1 Like

You could add an HA startup trigger. If that is triggered, it is after 9am, and the switch is off, then turn it on.

  - trigger: homeassistant
    event: start
1 Like

okay, so far this works, but how can I activate and deactivate this trigger?

sounds good as well.
where do I have to set this trigger then? in the same automation, right?

edit:
:nerd_face: okay,I see

edit2:
just realised that this would not work like expected.
if I have this additional startup command, the automation would start at any time. also out of the range and I have the exact same time problem like before.

You must change the times in the template.

okay, thanks.

why is it so “complicated” to realise such a “easy and fundamental” timer switch?

is this only because the logics from analog to digital are so far away from each other?

alias: heater timer on at 9am
description: ""
mode: single
triggers:
  - trigger: time
    at: "09:00:00"
  - trigger: homeassistant
    event: start
conditions: []
actions:
  - if:
      - condition: and
        conditions:
          - condition: time
            after: "08:59:59"
          - condition: state
            entity_id: switch.xxx
            state:
              - "off"
    then:
      - action: switch.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: switch.xxx

The and is not needed, a list of multiple conditions will always be and, unless they are below an or.

Also note that this wil ALWAYS turn it on on a reboot after 9:00.

Thanks. And, actually, it could be put in the conditions of the automation instead of an if statement.

alias: heater timer on at 9am
description: ""
mode: single
triggers:
  - trigger: time
    at: "09:00:00"
  - trigger: homeassistant
    event: start
conditions:
  - condition: time
    after: "08:59:59"
  - condition: state
    entity_id: switch.xxx
    state:
      - "off"
actions:
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: switch.xxx
1 Like