Suggestion to Combine On/Off for Switches

Tags: #<Tag:0x00007fc3fe799a90>

Hello guys! Need your help.

  1. Automation to turn on and off a switch
  2. Able to anticipate system downtime (power cut loss, crashes)

Any idea to merge it into single automation?

Hello :slight_smile: Welcome to the community.

This is what I personally use- (suggestion made by Muttley here)

trigger:
  - platform: homeassistant
    event: start
  - platform: template
    value_template: "{{ states('sensor.time') == '07:00' }}"
  - platform: template
    value_template: "{{ states('sensor.time') == '17:45' }}"

condition: []

action:
  - service: >-
      {% if '07:00' <= states('sensor.time') < '17:45' %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    entity_id:
      - switch.light

Can you try to see if it works for you?

1 Like

I dont seem to have sensor.time?

Sorry, I totally forget that you need to create the sensor first. So, sensor.time can be created using Time & Date integration.

You will need to edit your configuration.yaml by adding the following YAML code-

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

You can do it without sensor.time, though it is good to have.

trigger:
  - platform: homeassistant
    event: start
  - platform: time
    at:  '07:00'
  - platform: time
    at: '17:45'
action:
  - service: >
      {% if '07:00' <= as_timestamp(now())|timestamp_custom('%H:%M') < '17:45' %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    entity_id:
      - switch.light
1 Like

Or template-less

trigger:
  - platform: homeassistant
    event: start
  - platform: time
    at: '07:00'
  - platform: time
    at: '17:45'
action:
  - choose:
      - conditions:
          - condition: time
            after: '07:00'
            before: '17:45'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.light
    default:
      - service: switch.turn_off
        target:
          entity_id: switch.light
mode: single
2 Likes

Haha template-less seems much easier to be understood.

Using your code, what will happen if at 17:30, suddenly power loss occurred and only comes back on at 20:00?

Seeing the action condition stated in your choose syntax, it is written (condition: time & after: ‘07:00’)- so after the system came back online, how can the automation choose the default ones? As 20:00 is also after 07:00?

Sorry if my question is confusing.

To follow the original automation you would have to add a before condition as well.

action:
  - choose:
      - conditions:
          - condition: time
            after: '07:00'
            before: '17:45'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.light
    default:
      - service: switch.turn_off
        target:
          entity_id: switch.light

So if it is inside that time window (7am to 5:45pm) the switch will turn on, otherwise off.

1 Like

@tom_l Oeps :flushed:One part of my brain was thinking two conditions and the other one plus a default… Time for a new cup of coffee!

@marcrogivue In that case the light will stay on till 20:00. Because on 20:00 the automation is triggered because of the HA start event. And then just checks the time and runs the turn off because it’s not between 7:00 and 17:45.

1 Like

Thanks for clearing it up.