CSC Jinja Custom Template


Referring page: Custom Sprinkler Controller (CSC)

This is a custom jinja template I created for my sprinkler controller. The file is named irrigation_time.jinja and it needs to be saved in the config/custom_templates folder.

More info on using custom templates is available here.

{% macro get_last_start_time(valve_num, trigger) %}
    {% set e1 = 'sensor.valve_z0' ~ "" ~  valve_num  ~ "" ~ '_last_start_time'  %}
    {% set last_start = states(e1) | float(0) %}
    {% if trigger is defined and trigger.id == "off_2on" %}
        {{ as_timestamp (now()) }}
    {% else %}
        {{ last_start }}
    {% endif %}

{% endmacro %}


{% macro get_run_duration(valve_num, trigger) %}
    {% set e1 = 'sensor.valve_z0' ~ "" ~  valve_num  ~ "" ~ '_last_start_time'  %}
    {% set duration = state_attr(e1, 'run_duration') | float(0) %}
    
    {% if trigger is defined and trigger.id == "on_2off" %}
        {{ (as_timestamp (now()) - states(e1) | float()) | round (0) }}
    {% else %}
        {{ duration }}
    {% endif %}

{% endmacro %}


{% macro get_last_end_time(valve_num, trigger) %}
    {% set e1 = 'sensor.valve_z0' ~ "" ~  valve_num  ~ "" ~ '_last_start_time'  %}
    {% set last_start = states(e1) | float(0) %}
    {% set last_end = state_attr(e1, 'last_end_time') | float(0) %}
    
    {% if trigger is defined and trigger.id == "on_2off" %}
        {{ last_start + (as_timestamp (now()) - last_start | float()) | round (0) }}
    {% else %}
        {{ last_end }}
    {% endif %}

{% endmacro %}


{% macro get_count_today(valve_num, trigger) %}
    {% set e1 = 'sensor.valve_z0' ~ "" ~  valve_num  ~ "" ~ '_last_start_time'  %}
    {% set count_today = state_attr(e1, 'count_today') | int(0) %}

    {% if trigger is defined and trigger.id == "nightly_reset" %}
        {{ 0 }}
    {% elif trigger is defined and trigger.id == "off_2on" %}
        {{ count_today + 1 }}
    {% else %}
        {{ count_today }}
    {% endif %}

{% endmacro %}


{% macro get_count_last_week(valve_num, trigger) %}
    {% set e1 = 'sensor.valve_z0' ~ "" ~  valve_num  ~ "" ~ '_last_start_time'  %}
    {% set count_last = state_attr(e1, 'count_last_week') | int(0) %} 
    {% set count_this = state_attr(e1, 'count_this_week') | int(0) %} 
    
    {% if trigger is defined and trigger.id == "nightly_reset" and (now() + timedelta(seconds=30)).weekday() == 6 %}
        {{ count_this }}
    {% else %}
        {{ count_last }}
    {% endif %}

{% endmacro %}


{% macro get_count_this_week(valve_num, trigger) %}
    {% set e1 = 'sensor.valve_z0' ~ "" ~  valve_num  ~ "" ~ '_last_start_time'  %}
          {% set count_this = state_attr(e1, 'count_this_week') | int(0) %} 
          {% if trigger is defined and trigger.id == "nightly_reset" and (now() + timedelta(seconds=30)).weekday() == 6 %}
            {{ 0 }}
          {% elif trigger is defined and trigger.id == "off_2on" %}
            {{ count_this + 1 }}
          {% else %}
            {{ count_this }}
          {% endif %}

{% endmacro %}


{% macro get_run_day_of_week(valve_num, day_idx, trigger) %}
    {% set e1 = 'sensor.valve_z0' ~ "" ~  valve_num  ~ "" ~ '_last_start_time'  %}
    {% set e_attr = 'day'~ "" ~  (day_idx + 1) %}
    {% set day = state_attr(e1, e_attr) | int(0) %}
    
    {% if trigger is defined and trigger == "nightly_reset" and (now() + timedelta(seconds=30)).weekday() == 6 %}
        {{ 0 }}
    {% elif now().weekday() == day_idx %}
        {{ 1 }}
    {% else %}
        {{ day }}
    {% endif %} 

{% endmacro %}



{# Retrieves & sets the monthly runtime totals per zone #}
{% macro get_month_time(valve_num, month_num) %}
    {% set e1 = 'sensor.valve_z0' ~ "" ~  valve_num  ~ "" ~ '_time_today'  %}
    {% set e2 = 'sensor.valve_z0' ~ "" ~  valve_num  ~ "" ~ '_last_start_time'  %}
    {% set e_attr = 'time_' ~ ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'][month_num - 1] %}
    
    {% set init_time = state_attr(e1, e_attr) %}
    {% set time_month = strptime(init_time if init_time not in ['unknown', 'unavailable', None, ''] else '00:00:00', "%H:%M:%S") %}
    {% set seconds_month = time_month.hour * 3600 + time_month.minute * 60 + time_month.second %}
    {% set seconds_last_run = state_attr(e2,'run_duration') | float(0) %}  
    
    {% if (now() + timedelta(seconds=30)).month == month_num and now().month != month_num  %}
        {{ '00:00:00' }}
    {% elif now().month == month_num %}
        {{ (seconds_last_run + seconds_month) | timestamp_custom('%H:%M:%S', false) }}
    {% else %}
        {{ seconds_month | timestamp_custom('%H:%M:%S', false) }}
    {% endif %}  

{% endmacro %}