Insert time by weekdays in automation

Hello,

I need help from you again.
I have created an automation with which I execute a script by switch.
But this script should be executed automatically at certain times on certain days of the week.
Unfortunately I can not do this.

The script should be executed on the days Sunday to Thursday each 23:00.
And Saturday and Sunday 01:00 o’clock
should be executed.

Here is my automation:

alias: Alarmanlage zu Hause aktivieren über Fernbedienungen und Taster
description: ''
trigger:
  - device_id: 948d829e314157fae68c3862c1a8e7a4
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: button_1
  - device_id: 948d829e314157fae68c3862c1a8e7a4
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: button_3
  - platform: time
    at: '01:00'
condition: []
action:
  - service: script.turn_on
    target:
      entity_id:
        - script.alarmanlage_cobra_zu_hause_aktivieren_fur_alexa
mode: single

How do I get this done.
I thank you already now.

Kind regards Werner

Translated with DeepL Translate: The world's most accurate translator (free version)

Create a template sensor in configuration.yaml (or template include file)

template:
  - name: "execute"
    unique_id: "execute"
    state: >-
      {% set hr = now().hour %}
      {% set day = now().weekday() %}
      {% if (day in [0,1,2,3,6] and hr == 23) or (day in [5,6] and hr == 13) %}
         on
      {% else %} 
         off
      {% endif %}

add this as a condition if the time and date must be met or

condition:
  - condition: state
    entity_id: sensor.execute
    state: 'on'

you can also set this a a trigger

trigger:
  - platform: state
    entity_id: sensor.execute
    from: 'off'
    to: 'on'

Here it is setup in developer tools with the hr set to 14 to make it trigger on

For your automation

alias: Alarmanlage zu Hause aktivieren über Fernbedienungen und Taster
description: ''
trigger:
  - device_id: 948d829e314157fae68c3862c1a8e7a4
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: button_1
  - device_id: 948d829e314157fae68c3862c1a8e7a4
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: button_3
  - platform: state
    entity_id: sensor.execute
    from: 'off'
    to: 'on'
condition: []
action:
  - service: script.turn_on
    target:
      entity_id:
        - script.alarmanlage_cobra_zu_hause_aktivieren_fur_alexa
mode: single

EDIT: Added OP automation with sensor.execute.

1 Like

I think your at: is wrong

What time is 01:00

Try 01:00:00 real time bro

1 Like

Either one will work. If you use input_datetime’s they are held to the minute and work just fine. OP is asking for executing at multiple times on specific days. You could add all to the triggers for each of the specifications but the template sensor accomplishes it with a cleaner automation.

Regards

1 Like

Hello,

many, many thanks for the effort.
Will test it soon and get back to you.

Will there be a problem because I have this code already in my config…yaml:

# Datum und Uhrzeit    
sensor:
  - platform: template
    sensors:
      datum_zeit_time:
        friendly_name: ""
        value_template: >
          {% set days = ['Mo.', 'Di.', 'Mi.', 'Do.', 'Endl.Fr.', 'Sa.', 'So.'] %}
          {{ days[now().weekday()] }}
          {{ now().strftime('%d.%m. - %H:%M') }}  Uhr

Kind regards Werner

Will there be a problem because I have this code already in my config…yaml:

No