Template assistance please

I am wanting to create an automation (B) which has a time based trigger to occur 10 minutes before another automation (A). Automation (A) starts based on an input_datetime using the below template:

trigger:
  platform: template
  value_template: "{{ states('sensor.time') == (states.input_datetime.retic_program1_start_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"

I have played around with the template dev tool but can’t get the correct syntax to basically have automation (B) use a template value of the above input_datetime.retic_program1_start_time minus 10 minutes.

I tried this below which doesn’t error, but gives no return value at all:

value_template: "{{ (states.input_datetime.retic_program1_start_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) - '00:10' }}"

Can someone please help?

I don’t have an input datetime to test with but I think you should be able to use something like this:

value_template: "{{ states('sensor.time') == ((states.input_datetime.retic_program1_start_time.attributes.timestamp - 600) | int | timestamp_custom('%H:%M', False)) }}"

My understanding is that the input datetime timestamp attribute is in seconds so you need to subtract the 600 seconds (10 min) before you convert to a custom timestamp then compare to now.

If it doesn’t work then I can probably throw together a test input datetime and play around with it.

Thanks! that looks like it will work based on the dev tool. I’ll code it in and see what happens

I have built most of the rest of the automation that the above template was needed for but now I am not quite sure how to ‘convert’ the two conditions into a templated action.

I need to turn the input_boolean ON if the conditions are met and OFF otherwise (because it may already be ON from the previous day)

I think I need to remove the conditions and include them in an IF/ELSE type action template…just not sure how to write it.

  - alias: Check rain parameter     # check that todays rain doesnt need to disable the retic from running
    initial_state: 'on'
    trigger:
      platform: template
      value_template: "{{ states('sensor.time') == ((states.input_datetime.retic_program1_start_time.attributes.timestamp - 600) | int | timestamp_custom('%H:%M', False)) }}"
    condition:
      conditions:
        - condition: template
          value_template: "{{ states('sensor.bom_current_weather_rain_today') <= states('input_number.allowable_rain_quantity') }}"
        - condition: template
          value_template: "{{ states('sensor.possible_rainfall_today') <= states('input_number.allowable_rain_percentage') }}"
    action:
      - service: homeassistant.turn_on
        entity_id: input_boolean.retic_rain_parameters_met

I’m guessing it would be something similar to this below, but I don’t think I have it quite right.

  - service_template: >
      {% if states('sensor.bom_current_weather_rain_today') <= states('input_number.allowable_rain_quantity') and states('sensor.possible_rainfall_today') <= states('input_number.allowable_rain_percentage') %}
        homeassistant.turn_on: input_boolean.retic_rain_parameters_met
      {% else %}
        homeassistant.turn_off: input_boolean.retic_rain_parameters_met
      {% endif %}

I’m pretty sure it’s like this:

- service_template: >
  {% if states('sensor.bom_current_weather_rain_today') <= states('input_number.allowable_rain_quantity') and states('sensor.possible_rainfall_today') <= states('input_number.allowable_rain_percentage') %}
    homeassistant.turn_on
  {% else %}
    homeassistant.turn_off
  {% endif %}
  entity_id: input_boolean.retic_rain_parameters_met

that makes much more sense, thank you

Hi @finity I am finally back home and tried to test this out. Unfortunately it gives the below error whenever I trigger the automation. The trigger I have seems ok given that it proceeds to the action, however I get the error on the action…

this is the action:

action:
  - service_template: >
      {% if states('sensor.bom_perth_rain_today') <= states('input_number.allowable_rain_quantity') and states('sensor.possible_rainfall_today') <= states('input_number.allowable_rain_percentage') %}
        homeassistant.turn_on
      {% else %}
        homeassistant.turn_off
      {% endif %}
      entity_id: input_boolean.retic_rain_parameters_met

which gives this error:

Template rendered invalid service: homeassistant.turn_on
entity_id: input_boolean.retic_rain_parameters_met

first try to use the correct service for input_boolean. input_boolean.turn_on and input_boolean.turn_off

If that works, you might even want to try this:

- service_template: >
    input_boolean.turn_{{'on' if states('sensor.bom_perth_rain_today') <= states('input_number.allowable_rain_quantity') and 
                                 states('sensor.possible_rainfall_today') <= states('input_number.allowable_rain_percentage')  else 'off'}}
  entity_id: input_boolean.retic_rain_parameters_met

or

- service_template: >
    input_boolean.turn_{{'on' if states('sensor.bom_perth_rain_today') <= states('input_number.allowable_rain_quantity') and 
                                states('sensor.possible_rainfall_today') <= states('input_number.allowable_rain_percentage') else 'off'}}
  data:
    entity_id: input_boolean.retic_rain_parameters_met

can’t say anything about the template itself, since I don’t have these sensors or input_numbers of course.

Simply changing it to input_boolean. turn_on / off didn’t work but your first example seems to be, no more errors. Thanks heaps!

cool!
just be ware: when I typed it in my dev-template it showed fine, even selected the ‘on’… which made we wonder why, since I don’t have these entities, and I think it should have been ‘off’ .

Hence my remark about the template itself. If its running alright, and you can see actual change in behavior when changing the input_numbers to a template=flase state and see the service change to input_boolean.turn_off, all is well.

If not, please come back and well have to analyze the template further.