Jinja2 Help

I can’t wrap my head around this Jinja stuff. No matter what I try I get no value in the sensor. Here’s what I’m trying to accomplish: I have an alarm for every day of the week. Before I go to bed I have a goodnight scene that uses tts to tell me what time I have the alarm set for in the morning. So this sensor should in theory figure out the day and then look up that day’s alarm value. I do have one extra thing in there, if I go to bed before midnight, it looks up the alarm for the next day, if it’s after midnight, it would find the value of that day. Anyways, I don’t think I’m too far off, but I can’t get any value. Any ideas?

 next_alarm_time:
   friendly_name: Next Alarm Time
   entity_id:
    - sensor.alarmtime_sun
    - sensor.alarmtime_mon
    - sensor.alarmtime_tue
    - sensor.alarmtime_wed
    - sensor.alarmtime_thu
    - sensor.alarmtime_fri
    - sensor.alarmtime_sat
    - sensor.alarmtime_all
   value_template: >
      {%- macro settype() -%}
         {%- if states.input_boolean.alarm_clock_all.state == "true" -%}
            {% set alarmTime = states.sensor.alarmtime_all.state %}
         {%- else -%}
            {% set currentHour = now().strftime("%H") | int  %}
            {%- macro setday() -%}
               {%- if currentHour > 11 -%}
                  {% set currentDay = (now.weekday() | int) + 1 %}
               {%- else -%}
                  {% set currentDay = now.weekday() | int %}
               {%- endif -%}
            {%- endmacro -%}
            {%- macro setalarmtime() -%}
               {%- if currentDay == 6 -%}
                  {% set alarmTime =  states.sensor.alarmtime_sun.state  %}
               {%- elif currentDay == 0 -%}
                  {% set alarmTime =  states.sensor.alarmtime_mon.state  %}
               {%- elif currentDay == 1 -%}
                  {% set alarmTime =  states.sensor.alarmtime_tue.state  %}
               {%- elif currentDay == 2 -%}
                  {% set alarmTime =  states.sensor.alarmtime_wed.state  %}
               {%- elif currentDay == 3 -%}
                  {% set alarmTime =  states.sensor.alarmtime_thu.state  %}
               {%- elif currentDay == 4 -%}
                  {% set alarmTime =  states.sensor.alarmtime_fri.state  %}
               {%- elif currentDay == 5 -%}
                  {% set alarmTime =  states.sensor.alarmtime_sat.state  %}
               {%- endif -%}
            {%- endmacro -%}
         {%- endif -%}
         {{alarmTime}}
      {%- endmacro -%}

Is this really everything? Because if it is it has two flaws.

  1. You are defining macros but never call them. They are like functions.
  2. Read up on variable scopes in jinja2. It’s not possible to change a variable out of it’s scope (this gave me so many headaches)

~Cheers

I just went ahead and did the thing I hope you can see how you were wrong with it. Should work like this:

{%- macro getday(currentHour) -%}
  {%- if currentHour > 11 -%}
    {{ (now().weekday() | int) + 1 }}
  {%- else -%}
    {{ now().weekday() | int }}
  {%- endif -%}
{%- endmacro -%}
{%- macro getalarmtime(currentDay) -%}
 {%- if currentDay == 6 -%}
    {{  states.sensor.alarmtime_sun.state  }}
 {%- elif currentDay == 0 -%}
    {{  states.sensor.alarmtime_mon.state  }}
 {%- elif currentDay == 1 -%}
    {{  states.sensor.alarmtime_tue.state  }}
 {%- elif currentDay == 2 -%}
    {{  states.sensor.alarmtime_wed.state  }}
 {%- elif currentDay == 3 -%}
    {{  states.sensor.alarmtime_thu.state  }}
 {%- elif currentDay == 4 -%}
    {{  states.sensor.alarmtime_fri.state  }}
 {%- elif currentDay == 5 -%}
    {{  states.sensor.alarmtime_sat.state  }}
 {%- endif -%}
{%- endmacro -%}
{%- if states.input_boolean.alarm_clock_all.state == "true" -%}
  {% set alarmTime = states.sensor.alarmtime_all.state %}
{%- else -%}
  {% set currentHour = now().strftime("%H") | int  %}
{%- endif -%}
{{ getalarmtime(getday(currentHour) | int) }}

~Cheers

Awesome! Got it working now. Thanks so much for your help. I was banging my head trying to get this to work.

1 Like

Curious, can you have like a separate file that holds some jinja macros and use them in other files?

EDIT: Sorry misunderstood your question! I don’t know of a way to do this.

~Cheers