No output from automation with service template

I have difficulties in getting the below automation to output a value to ‘input_datetime.drip1_next_run_time’. The template gives the right values when testing in Jinja2.
It is supposed to output the next time for the drip irrigation based on the time of the day. If the sun is above horizon and the drip irrigation is enabled it just add the interval time and if the sun is below horizon and the drip irrigation is enabled the next drip time will be at sun rise next day as it is not supposed to run in the night.
I haven’t been able to figure it out by searching the forum.

  - alias: Set drip1 next scheduled run time
    trigger:
      - platform: state
        entity_id: input_boolean.drip1_valve
        to: "off"
        for: "00:00:05"

    action:

      - service: input_datetime.set_datetime
        entity_id: 
          - input_datetime.drip1_next_run_time
      - service: >
          {% if is_state('input_boolean.drip1_enable' , 'on') and is_state('sun.sun' , 'above_horizon') %}
            {{(state_attr('input_datetime.last_drip1_time', 'timestamp') + (states('input_select.drip1_interval_time') | int * 3600)) | timestamp_custom('%d-%m-%Y at %H:%M') }}
          {% elif is_state('input_boolean.drip1_enable', 'on') and  is_state('sun.sun', 'below_horizon') %}
            {{ (as_timestamp(states.sun.sun.attributes.next_rising)) |float| timestamp_custom('%d-%m-%Y at %H:%M')}}
          {% else %}
            None
          {% endif %}

first issue is that you need to use “data:” which is part of the initial service call instead of a separate “service:” for the time.

      - service: input_datetime.set_datetime
        entity_id: 
          - input_datetime.drip1_next_run_time
        data: 
          datetime: >
            {% if is_state('input_boolean.drip1_enable' , 'on') and is_state('sun.sun' , 'above_horizon') %}
              {{(state_attr('input_datetime.last_drip1_time', 'timestamp') + (states('input_select.drip1_interval_time') | int * 3600)) | timestamp_custom('%d-%m-%Y at %H:%M') }}
            {% elif is_state('input_boolean.drip1_enable', 'on') and  is_state('sun.sun', 'below_horizon') %}
              {{ (as_timestamp(states.sun.sun.attributes.next_rising)) |float| timestamp_custom('%d-%m-%Y at %H:%M')}}
            {% else %}
              None
            {% endif %}

Next the datetime format needs to be a specific format.

it has to be either a properly formatted datetime string or a timestamp.

This won’t work because it has the ‘at’ in it and the format is moved around:

timestamp_custom('%d-%m-%Y at %H:%M')

here is the example datetime format from the docs:

data:
    datetime: "2020-08-25 05:30:00"

Seems like it is working now. Thanks for your help.

1 Like