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

Hi all,

I have a question whether this is possible or not. Currently I do have a lot of automation which is fine, but if I could cook some of them down to one :slightly_smiling_face:

So here is what I want to accomplish:

When motion detection is turned on then:
- If between 00-06am
   then do this
   - Turn on light:
      - light1
      - light2
   - Data:
         Brightness: 20%
- if between 06 and sun is above horizon
   then do this
   - Turn on light:
      - light1
      - light2
   - Data:
       Brightness: 100%
- if between below horizon and 00pm
   then do this
   - Turn on light:
      - light1
      - light2
   - Data:
          Brightness: 50%

Or do I need to create 3 Automations in order to do this?

2 Likes
automation:
  alias: lights to different brightnes
  trigger:
    platform: state 
    entity_id: MOTION_SENSOR
    to: DETECT_MOTION
  action:
    service: light.turn_on
    entity_id:
      - LIGHT_ONE
      - LIGHT_TWO
    data_template:
      brightness_pct:
        {% if now().hour < 6 %} 20
        {% elif is_state('sun.sun' , 'below_horizon') %} 50
        {% else %} 100 {% endif %}
3 Likes

Hey mf_social,
Nice and thanks for the response. Seems to be running great!
I tried to modify it so I could run a script bases on the time, but no success.

Do you have an idea of how I should tweak the above if I wanted it to trigger 3 different scripts depending on the time?

I read some other forums where people says it’s best to use the conditions in the automation instead of in the script. I don’t know whats best though.

1 Like
automation:
  alias: lights to different brightnes
  trigger:
    platform: state 
    entity_id: MOTION_SENSOR
    to: DETECT_MOTION
  action:
    service_template: >
      {% if now().hour < 6 %} script.ONE
      {% elif is_state('sun.sun' , 'below_horizon') %} script.TWO
      {% else %} script.THREE {% endif %}
4 Likes

Thanks man!
Nice when you guys have the time to help out some starters!

Curious, so if I wanted to change the timing from 00 to 10pm, and only run another script during the ‘below horizon’ would I just have to change it like this?:

automation:
  alias: lights to different brightnes
  trigger:
    platform: state 
    entity_id: MOTION_SENSOR
    to: DETECT_MOTION
  action:
    service_template: >
      {% if((now().hour <= 6) or (now().hour >= 22))}%} script.ONE
      {% else %} is_state('sun.sun' , 'below_horizon') {% endif %} script.TWO

Or am I completely off here?

Close :slight_smile:

 automation:
  alias: lights to different brightnes
  trigger:
    platform: state 
    entity_id: MOTION_SENSOR
    to: DETECT_MOTION
  condition:
    condition: or 
    conditions:
      - condition: time 
        after: '22:00:00'
        before: '06:00:00'
      - condition: sun 
        after: sunset
      - condition: sun
        before: sunrise
  action:
    service_template: >
      {% if((now().hour <= 6) or (now().hour >= 22))}%} script.ONE
      {% else %}  script.TWO {% endif %}

So in this case I need the above, because I move ahead og 00:00?

Why isn’t the below not enough?

service: script.turn_on
data_template:
  entity_id: >
    {% if((now().hour <= 6) or (now().hour >= 22))}%}
      script.script1
    {% elif (now().hour <= 19) %}
      script.script2
    {%else%}
      script.script3
    {% endif %}

I get this error when trying:

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ‘)’, got ‘:’) for dictionary value @ data[‘action’][0][‘data_template’][‘entity_id’]. Got None. (See /config/configuration.yaml, line 17). Please check the docs at Automation - Home Assistant

You keep changing what you want to get out of this automation, and I don’t really know why.

The reason I added conditions to the third one is because otherwise there are times in the day that would not be resolved by the template. Each variation you have asked for is actually completely different, the first required templated optional data keys that were resolvable 100% of the time, the second required service template that is resolvable 100% of the time, and the third one requires a service template that cannot always be resolved so needs to be guarded with conditions. Then you’ve asked why a service template that does resolve 100% of the time isn’t enough, but that template doesn’t match the requirement you asked for when I gave the third example because it relies only on time, and takes no account of sun position.

I really don’t know how I can help if the goal posts get moved every time I give you a solution :slight_smile:

Sorry about that. I can see that I wrongly pasted another piece of code here, that I was playing with.
I will try to play around with the two you created in the first place. They seem to do what I need for now.

The main goal here is to use my Xiaomi button to do several things depending on the time.
If my daughter clicks the Xiaomi button once, the light should turn on a LED light, but only if the sun is down and before 22:00. If she clicks again it should turn off.
But, if she presses the button during 22 and 06 (perhaps before sunrise instead), then it should call another script that turns on some lighs towards the master bedroom for 10 minutes and then turn off…

Does it make sense?

Yeah absolutely.

Basically what you need to do is work out exactly what the parameters are that need to be checked when the button is pressed, and then based on that formulate the correct combination of templates / scripts to make it happen.

The tricky bit is actually working out what the combinations of parameters are, after that the templates should be easy enough.

Look at every hour from 00:00 to 23:59 and think "if I press the button now, what should happen?

  • Light goes on?

Probably not

  • further checks?

Yup, first check is “is the light already on?”, then if yes switch the light off, if not go for some more further checks. Next check is the sun down etc etc.

If you can make it in to a flow chart, then it can be made to happen, but you need to get the flow chart right first, because as you’ve seen above the slightest change to the flow chart makes massive changes to the automation :slight_smile:

Yeah absolutely.
I might not be skilled enough to build it that way :slight_smile:

But thanks!

Ha, draw me a flow chart and I’ll make it in to something you can use in homeassistant :wink:

WOW - much appreciated.
What do you use when drawing such things? Any good webtools or just pen and paper?

Pen and paper - just jot down the decision making process step by step. So like

Button clicked
       |
Is the light on   - yes - turn it off 
     |
    No
      |
Is it dark - no - turn it on, but only for 10 minutes
      |
    Yes
      |
Some other stuff etc etc

Will give it a shot tomorrow and ping you back!

1 Like

No worries :+1:

Hey Sir,

I tried to put down a notepad drawing and I hope it makes sense.
Here is what I came up with:

Xiaomi Click Button - Single Press


           Single Click
                |
                |
    Is the time between sunrise and sunset ------------------------> Yes - Do nothing
                |                                         		 
		        |							 
    	        No					
	         	|					   		 
    Is the time between sunset and 22? ------> Yes
		|	   		            |
		|           		   	|
		|         		Is the light turned on? ----> Yes - Turn off light
		|			        	|
		|			        	|
		|			        	No
		|			        	|		         
 		|			     Turn off light
		|
		No
		|
   Is the time between 22 and sunrise ---------------------------> Yes - run script (Turn on path light and other stuff)
        |
		|
		No
		|
	   Do nothing (run dummy script perhaps)
1 Like

I’ll chuck something together when I get home :smile: