Help with automation - sonnoff switch

This is my first post so please forgive me if this is not the right forum for this.
I was hoping someone could look over the first automation that I created using the GUI and let me know if they see any problems with it or perhaps a more efficient way to create the same automation.

The switch controls towel warmers in our bathrooms.
I was wondering what the best way to handle the case if one of the off triggers doesn’t fire (to turn the switch off) for some reason, such as HA goes off line for a minute or two but then comes back online a few minutes later. Should I add some sort of automation that when HA starts it makes sure the Towel warmer switch gets turned off if its state is “on”?

Any help or comments/improvements on the below automation yaml would be appreciated:

alias: 'Towel Warmers automation '
description: Turn on and off towel warmers based on time and day.
trigger:
  - platform: time
    at: '05:30:00'
    id: tw_turn_on_weekday
  - platform: time
    at: '07:30:00'
    id: tw_turn_off_weekday
  - platform: time
    at: '08:00:00'
    id: tw_turn_on_weekend
  - platform: time
    at: '10:00:00'
    id: tw_turn_off_weekend
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: tw_turn_on_weekday
          - condition: time
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.1000539b1b
      - conditions:
          - condition: trigger
            id: tw_turn_off_weekday
          - condition: time
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.1000539b1b
      - conditions:
          - condition: trigger
            id: tw_turn_on_weekend
          - condition: time
            weekday:
              - sat
              - sun
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.1000539b1b
      - conditions:
          - condition: trigger
            id: tw_turn_off_weekend
          - condition: time
            weekday:
              - sat
              - sun
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.1000539b1b
    default: []
mode: single

What you could do is to use a template that returns true for say, five minutes instead of just at the time.

Pseudo code since I can’t test it now:

{{ now() >= today_at("05:30") and now() <= today_at("05:35") }}

I believe this is correct.

I understand your question and I had exactly the same for a long time. create a trigger like this:

trigger:
  - platform: time_pattern
    hours: '*'
    seconds: /5
    minutes: '*'
    id: time

It checks all triggers.
Here is my example for turning on the light at night

alias: ОСНОВНОЙ Свет коридор
description: ''
trigger:
  - platform: time_pattern
    hours: '*'
    seconds: /5
    minutes: '*'
    id: time
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: numeric_state
                entity_id: sun.sun
                attribute: elevation
                above: '-1'
              - condition: state
                entity_id: input_boolean.svet_koridor
                state: 'on'
              - condition: device
                type: is_on
                device_id: ab5947eb9a971465823844735d2f51a2
                entity_id: light.xiaomi_gateway_light
                domain: light
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.gateway_light_7811dcfac306
      - conditions:
          - condition: and
            conditions:
              - condition: numeric_state
                entity_id: sun.sun
                attribute: elevation
                below: '-2'
              - condition: state
                entity_id: input_boolean.svet_koridor
                state: 'on'
              - condition: device
                type: is_off
                device_id: ab5947eb9a971465823844735d2f51a2
                entity_id: light.xiaomi_gateway_light
                domain: light
        sequence:
          - type: turn_on
            device_id: ab5947eb9a971465823844735d2f51a2
            entity_id: light.xiaomi_gateway_light
            domain: light
            brightness_pct: 50
    default: []
mode: restart

It’s not in the right forum (but easily fixed). “Share your Projects!” is for sharing the (functional) project you have created. You have a question about an automation, not a project to share, so please take a moment to change the topic’s category to “Configuration”.

Regarding your question, you may be interested in reviewing this post which explains how to make an automation more robust (i.e. properly handle missed events).

1 Like

The following automation triggers at four scheduled times of the day and at startup.

When triggered, it computes two variables:

  • The variable t is the current time expressed as a tuple. For example, (7,30) is 07:30.
  • The variable mode is the switch’s desired mode based on whether the current day is a weekday or weekend and if the current time is within the appropriate time range.

The automation’s action simple sets the switch to the computed mode.

alias: 'Towel Warmers automation '
description: Turn on and off towel warmers based on time and day.
trigger:
  - platform: time
    at:
      - '05:30:00'
      - '07:30:00'
      - '08:00:00'
      - '10:00:00'
  - platform: homeassistant
    event: startup
condition: []
action:
  - variables:
      t: '({{ now().hour }},{{ now().minute }})'
      mode: >
        {% if 1 <= now().isoweekday() <= 5 %}
          {{ 'on' if (5,30) <= t < (7,30) else 'off' }}
        {% else %}
          {{ 'on' if (8,0) <= t < (10,0) else 'off' }}
        {% endif %}
  - service: 'switch.turn_{{ mode }}' 
    target:
      entity_id: switch.1000539b1b
mode: single

The template that computes mode works like this:

  • If the current day is between 1 and 5 inclusively, it’s a weekday, otherwise it’s a weekend.
  • If it’s a weekday, report on if the current time is equal or greater than 05:30 and less than 07:30, otherwise report off.
  • If it’s a weekend, report on if the current time is equal or greater than 08:00 and less than 10:00, otherwise report off.

EDIT

If you are uncomfortable with the practice of representing time as a tuple, it can be easily redesigned so that time is represented as a string.

1 Like

Thank you I will move this post

1 Like