If else statements in automastions

First off I am new to HA and I apologize if there is already some amazing doc that I’ve missed that explains all this.

I am trying to use some basic if then else statements to include in my automation, I have one specifically that if motion is detected by one of my sensors do different things depending on the time of day. Now I currently have them using choose > conditions but just bypassing the visual editor and being able to write the code out seems easier, if I can ever figure it out.

So this is just an example but this first piece of code works as expected but the second one will not work.

alias: Test 234
variables:
 variable1: Warning

description: ''
trigger:
Trigger here - 
condition: []
action:
  - service: notify.mobile_app_user_phone
data:
  data:
    priority: high
    ttl: 0
      message: "{{ variable1 }}"
  title: Alert

As soon as I try to add an if condition the whole thing breaks, I’ve not been able to find any documentation on the exact logic or placement of the if condition but this exact code does work in the template section under dev tools

{% if is_state("sun.sun", "above_horizon") -%}    ## also tried if states("sun.sun", here
{% set tod = {"type": "Shutdown_Day_Motion"} %}
{%- else -%}
{% set tod = {"type": "Shutdown_Night_Motion"} %}
{%- endif %}

alias: Test 234
description: ''
trigger:
Trigger here - 
condition: []
action:
  - service: notify.mobile_app_user_phone
data:
  data:
    priority: high
    ttl: 0
      message: "{{ tod.type}}"
  title: Alert

Any help would be appreciated.

This is a key value pair:

key: value

You can only use templates in the value. You have your template on it’s own, outside the automation even. You need to put it after the variable: key.

alias: Test 234
variables:
  variable1: >
    {% if is_state("sun.sun", "above_horizon") -%}    ## also tried if states("sun.sun", here
      {% set tod = {"type": "Shutdown_Day_Motion"} %}
    {%- else -%}
      {% set tod = {"type": "Shutdown_Night_Motion"} %}
    {%- endif %}
    {{ tod }}
description: ''
trigger:
Trigger here - 
condition: []
action:
  - service: notify.mobile_app_user_phone
data:
  data:
    priority: high
    ttl: 0
      message: "{{ variable1 }}"
  title: Alert

That did the trick, thank you for that.

The final code that is working for me in a single automation is below. The trigger is just a test I did and I’m just using the message section to ensure the variable is set properly and I get the expected outcome depending on the suns state. I can expand this to actually do something useful now thanks to you.


variables:
  variable1: >
    {% if is_state("sun.sun", "above_horizon") -%}
    {% set tod = {"type": "Shutdown_Day_Motion"} %}
    {%- else -%}
    {% set tod = {"type": "Shutdown_Night_Motion"} %}
    {%- endif %}
    {{ tod.type }}

alias: Test 234
description: ''
trigger:
  - platform: time
    at: '18:22:30'
condition: []
action:
  - service: notify.mobile_app_my_phone
    data:
      message: '{{ variable1 }}'
      data:
        priority: high
        ttl: 0

mode: single