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

So I guess some of it makes sense :smiley:.
Then I can also learn how I can do an If else inside of an If, which I think would help me understand the benefits of this (if that’s something you will use in your chuck) :blush:

Yeah, not a problem. I’ll write out how I would do it and (hopefully!) detail how it works and hopefully that will help you understand what you’re reading :slight_smile:

OK, so the first thing I’ve done is simplify your flow chart, because lower down you’re actually asking questions that are duplicitous because they’re already filtered by the conditions above. I got it down to

    Single click when sun below horizon 
                 |
       Is it before 2200?  - - - - - - yes
                 |                      |
                No                    turn off light
                 |
            Run the script 

So, I used the single click as the trigger and a condition to stop the automation firing if sun is above horizon.

That way we only have to deal with is it before or after 2200 hours.

Then I used a service template and a data template so if it is before 2200 you get

service: homeassistant.turn_off
entity_id: light.LIGHT

But after you get

service: homeassistant.turn_on
entity_id: script.PATH_LIGHT_AND_OTHER

Which looks like…

trigger:
  platform: state 
  entity_id: XIAOMI_BUTTON
  to: SINGLE_CLICK
condition:
  condition: template
  value_template: "{{ is_state('sun.sun' , 'below_horizon') }}"
action:
  service_template: >
    {% if now().hour < 22 %} homeassistant.turn_off
    {% else %} homeassistant.turn_on {% endif %}
  data_template:
    entity_id: >
      {% if now().hour < 22 %} light.LIGHT
      {% else %} script.PATH_LIGHT_AND_OTHER {% endif %}
1 Like

I’ve also just realised that where you’ve put

Is the light on? > Yes/no > turn off light

You might have actually meant to turn it on if was off, and off if it was on.

If that’s the case change line 10 from homeassistant.turn_off to homeassistant.toggle

Yes you are right about that!
One question though, so I’ve changed the light because I wanted it to either turn on or off as you mentioned above.

- 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 now().hour < 22 %} homeassistant.toggle
      {% else %} homeassistant.turn_on {% endif %}
  data_template:
    entity_id: >
      {% if now().hour < 22 %} light.led_ibox
      {% else %} script.nightlight {% endif %}

So does it look like we are good to go?

Side question, if I wanted to run another script between ‘below horizon’ and 22, then I just have to switch the line with: homeassistant.turn_on and then add script.xxx on the same line below data template - am I right?

  action:
    service_template: >
      {% if now().hour < 22 %} homeassistant.turn_o
      {% else %} homeassistant.turn_on {% endif %}
  data_template:
    entity_id: >
      {% if now().hour < 22 %} script.ONE
      {% else %} script.TWO {% endif %}

Looks good to go to me, presuming that trigger is correct :slight_smile:

If all you’re launching is scripts you can just use a service template

action:
  service_template: >
    {% if now().hour < 22 %} script.ONE
    {% else %} script.TWO {% endif %}

No extra data required :+1:

Arh yes makes sense! Learning a lot here!

So when I use the homeassistant.toggle then it simply turns on the light at previously known value and turn it off if on and the other way around.
If I wanted to add such info, brightness etc. When turning on, then I should state that in the data_template?

Yes, but that’s where it gets in to the territory of using an external script to do it because otherwise the template won’t resolve properly, so like before 10pm you’ll get

service: light.turn_on
entity_id: light.ibox
brightness_pct: 60 

Which is fine, but there’s no way to remove the brightness_pct data, so after 10pm you get

service: script.turn_on
entity_id: script.bedtime
brightness_pct:

Which is invalid, because script.turn_on doesn’t accept a brightness parameter.

Hence why I said you need to get the flow chart right first, because as soon as you slightly change the flow chart it massively affects how we can automate it. It can always be done, but the code to do it can be drastically different from last time.

Yes absolutely.
Just trying to figure out when you should call scripts, when you should use service_template and when you should use the data_template.

I will try with the above, hopefully you can get some rest while i fool around :slight_smile:
Hope you are up for some other challenges later :smile:

1 Like

Always love a challenge :+1:

Sounds good.

Unfortunately when pasting the automation into my automation file I get an error:

Invalid config for [automation]: [data_template] is an invalid option for [automation]. Check: automation->data_template. (See /config/configuration.yaml, line 17). Please check the docs at Automation - Home Assistant

EDIT: Never mind - I got it accepting it. I had to change some of the spacing at the beginning of the data_template.

1 Like

So I tested it and the first part is running with the light toggle. But here after 22 (10pm), it doesn’t run the script, but keeps toggling the light.

Do you have any idea why?

Is the time right on your device and the timezone set correctly in homeassistant?

Yes - it shows the correct time.[quote=“mf_social, post:33, topic:83466, full:true”]
Is the time right on your device and the timezone set correctly in homeassistant?
[/quote]

Yes time affects my region and it shows the correct time.

I have no idea then, the template resolves that if the hour returned by now() is less than 22 it gives the light result, and 22 or above the script result.

Stick it in the template editor and see what it says and move the hour around to see what is returning?

Template editor?

Here is the “working code” did I miss anything else?

- 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 now().hour < 22 %} homeassistant.toggle
      {% else %} homeassistant.turn_on {% endif %}
    data_template:
      entity_id: >
        {% if now().hour < 22 %} light.ibox
        {% else %} script.nightlight {% endif %}

On the sidebar, looks like a sheet of paper with <> on it.

The automation certainly looks fine from here.

Template working fine here, if I leave it at 22 (it’s just after midday here) it would toggle the light…

But if I change it to twelve, which we’re now after, it would activate the script…

Didn’t know that feature! SO useful!

It shows the exact same result as on yours. Hmm…

Will try again when I get home, but could it be related to the condition then?

1 Like

Well, no, because the condition will stop the whole automation being run so it won’t get as far as the action (template or no template). :confused:

There’s no other automations superceding this one is there?