Automation - if else - is that possible in one automation?

No there isn’t.

I will have to play with it when I get home. I can move the time ahead but still after the sun is below and try again.

1 Like

Hey Mf,

So I came the issue a bit closer.
If I click my button while the sun is below the horizon and it is before 22 (10pm.), the the light toggles as it should.
When i click the button after 22 (10pm) and until 00:00, the scripts also fires as it should.

… but if I then do it after 00, it goes back and does the light toggle.

1 Like

Ah yes, well that’s because I’m a bit dim and forgot about after midnight, change the if’s to…

{% if now().hour < 22 and now().hour > 6 %}

Yes that makes sense! But at 6 o’clock it might still be dark and my daughter would still need to be able to turn on the script. So how can I change it from that and let it run - lets say until sunrise?
Don’t the service_template support that?

Just an FYI python and jinja have the ‘smarts’ to handle an if statement like this:

{% if 6 < now().hour < 22 %}
1 Like

Well, the fact that it’s still ‘below horizon’ is catered for by the condition, so just make it 9 instead of 6 :slight_smile:

@petro 's way is much neater, I don’t know why I never think of doing it like that. Probably because I find python to be the devil’s work :laughing:

:rofl: Jinja too.

1 Like

So that will make my automation end up like this:

- alias: Xiaomi
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d00014d0cc0
      click_type: single
  condition:
    condition: template
    value_template: "{{ is_state('sun.sun' , 'below_horizon') }}"
  action:
    service_template: >
      {% if 6 < now().hour < 22 %} homeassistant.toggle
      {% else %} homeassistant.turn_on {% endif %}
    data_template:
      entity_id: >
        {% if 6 < now().hour < 22 %} light.laura_ibox
        {% else %} script.natlys {% endif %}

You agree? With the simplified petro thing :slight_smile:?

1 Like

since you ask for it, Please indulge me my multi-line fetish:

  condition:
    condition: template
    value_template: >
      {{ is_state('sun.sun', 'below_horizon') }}

makes it easier on the eye, and easier not to make mistakes if you do need quotes inside your templates. This way you don’t need them around the template.

another one, simply to force yourself to always be correct, and check that easily: I always put the if then else and ends at the beginning of the line, so its obvious if one is missing:

    service_template: >
      {% if 6 < now().hour < 22 %} homeassistant.toggle
      {% else %} homeassistant.turn_on 
      {% endif %}
    data_template:
      entity_id: >
        {% if 6 < now().hour < 22 %} light.laura_ibox
        {% else %} script.natlys 
        {% endif %}

extra advantage is you can easily see which if, then else belongs to which, in case of nested conditions/templates.

just my 2c
cheers!

I agree :slight_smile:

Looks good and seems to work!
Thanks everyone so far :slight_smile:

1 Like

Hey guys,

I’m back here. I have something similiar to the above, so I though I would repost here.
What am I doing wrong. I want to fire different scripts depending on the time of the day.

I did check the scripts by firering them manually - they work, so it must be my automation:

- alias: '[Hobbyroom] Turn on Spot'
  trigger:
    platform: state
    entity_id: input_boolean.disable_motionsensor_hobbyroom_automation
    from: 'off'
    to: 'on'
  action:
    service_template: >
      {% if '21:15:00' < now().hour < '21:30:00' %} homeassistant.turn_on
      {% elif '21:30:00' < now().hour < '21:45:00' %} homeassistant.turn_on
      {% else %} homeassistant.turn_on
      {% endif %}
    data_template:
      entity_id: >
        {% if '21:15:00' < now().hour < '21:30:00' %} script.test1
        {% elif '21:30:00' < now().hour < '21:45:00' %} script.test2
        {% else %} script.test3
        {% endif %}

Heeeeeeeeeeelp :slight_smile:

  action:
    service_template: >
      {% if '21:15:00' < now().hour < '21:30:00' %} homeassistant.turn_on
      {% elif '21:30:00' < now().hour < '21:45:00' %} homeassistant.turn_on
      {% else %} homeassistant.turn_on
      {% endif %}

The only outcome of these if elseif else is homeassistant.turn_on
You should just call “service: homeassistant.turn_on”

    data_template:
      entity_id: >
        {% if '21:15:00' < now().hour < '21:30:00' %} script.test1
        {% elif '21:30:00' < now().hour < '21:45:00' %} script.test2
        {% else %} script.test3
        {% endif %}

now().hour is only checking hours. You are comparing it to ‘hours:minutes:seconds’.

I’ve recently got into the HA so I don’t know the solution off of my head.
If noone answers you, I will look up my automation when I get home and let you know.

For combining multiple time elements you need strftime:

now().strftime("%H:%M")

For a full list off formats that strftime supports look here: http://strftime.org/

Since I don’t think you’d need secondsyou’d end up with:

data_template:
  entity_id: >
    {% if '21:15' < now().strftime("%H:%M") < '21:30' %} script.test1
    {% elif '21:30' < now().strftime("%H:%M") < '21:45' %} script.test2
    {% else %} script.test3
    {% endif %}

So you think I could just use this:

- alias: '[Hobbyroom] Turn on Spot'
  trigger:
    platform: state
    entity_id: input_boolean.disable_motionsensor_hobbyroom_automation
    from: 'off'
    to: 'on'
  action:
  - service: homeassistant.turn on
    data_template:
      entity_id: >
        {% if '21:15:00' < now().hour < '21:30:00' %} script.test1
        {% elif '21:30:00' < now().hour < '21:45:00' %} script.test2
        {% else %} script.test3
        {% endif %}

Or am I completely off here?

now().hour is an integer. '21:15:00' and '21:30:00' are strings. You cannot do greater than or less than operations on strings and expect them to work. The string will only look at the first set of numbers or characters for your result, and strings order numbers like this 1, 10, 11, …, 2, 20, 21,… etc. Things get messy with strings. Never compare strings unless you understand the ramifications.

All these time values need to be converted to integers of some sort. So you could convert them to hours, minutes, or seconds. Doesn’t matter which one. Easiest would be hours.

Second, this is a script. You don’t need to call anything but the script. That can be done with a service_template.

- alias: '[Hobbyroom] Turn on Spot'
  trigger:
    platform: state
    entity_id: input_boolean.disable_motionsensor_hobbyroom_automation
    from: 'off'
    to: 'on'
  action:
  - service_template: >
      {% set time_now = now().hour + now().minutes / 60 + now().seconds / 3600 %}
      {% set t1 = 21 + 15 / 60 %}
      {% set t2 = 21 + 30 / 60 %}
      {% set t3 = 21 + 45 / 60 %}
      {% if t1 < time_now <= t2 %} script.test1
      {% elif t2 < time_now <= t3 %} script.test2
      {% else %} script.test3
      {% endif %}

Makes sense. Thanks for the inputs. I realized I didn’t need the minutes feature, but only during hours.
Next goal is to make it reating on the position of the sun, so I don’t have to change the time values when the day gets longer or shorter.

But here is what I have now and that is working. Might get back regarding the above wish, but I’m guessing I would just have to if_states and then chose the sun attributes and then a number, depending whether it should be below or above horizon.

- alias: '[Hobbyroom] Motion/Lux Turn On Spot'
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_xxxxxxxxxxxxxxxxxxxxxx
    from: 'off'
    to: 'on'
  condition:
    - condition: numeric_state
      entity_id: sensor.illumination_xxxxxxxxxxxxxxxxxxxx
      below: 70
  action:
  - service: homeassistant.turn_on
    data_template:
      entity_id: >
        {% if 6 <= now().hour < 8 %} script.hobbryroom_motion_morning
        {% elif 8 <= now().hour < 16 %} script.hobbryroom_motion_day
        {% elif 16 <= now().hour < 22 %} script.hobbryroom_motion_evening
        {% else %} script.hobbryroom_motion_night
        {% endif %}

Hello

Looking at the examples above, I don’t seem to understand what is wrong with this syntax I’m trying?

automation epaper:
  - alias: epaper
    trigger:
      platform: time_pattern
      minutes: '/5'
    action:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.dummy_epaper
          value:
            {% if now().hour < 20 %} 2
            {% else %} 1 {% endif %}

This is the error:

Error loading /home/homeassistant/.homeassistant/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token

thanks!

After value: add a >

1 Like

Ten years ago so easier…