Use input variable in value_template

Hello i´m just getting started with Home Assistant automations and i´m stuck for hours now, i hope someone can help me.

I´m trying to extend this blueprint. https://github.com/Aohzan/hass-blueprints/blob/87209ae2617b8523e5779a02214a19a946252ed6/blueprints/medication_reminder_android.yaml

I want to add an additional offset that it only triggers every x days not every day at the defined time.

To inputs i added:

 days_interval:
      name: Days between reminding
      description: Amount of days till the next reminder
      default: 3
      selector:
        number:
          min: 1
          max: 365
          unit_of_measurement: days
          step: 1

    last_triggerd_time:
      name: Dedicated helper last taken
      description: Create and set here an input_datetime to handle the last reminder
      selector:
        entity:
          domain:
            - input_datetime
          multiple: false

I created this variables (not sure if they are required, just found this as a -not working- solution somewhere):

variables:
  days_interval: !input days_interval
  last_watered: !input last_triggerd_time

and finally i modified the trigger:

trigger:
  - platform: time
    at: !input reminder_time
  - platform: template
    value_template: >
      {% set days_between = (as_timestamp(states(last_watered)) - as_timestamp(now())) / (60*60*24)  %}
      {{ days_between >= states('input_number.days_interval') | int }}

The problem is that i always get an error that the value passed to “as_timestamp” is invalid.

What is the correct syntax do pass an input variable into the value_template?

Is your input_datetime used for !input last_triggerd_time time-only, date-only, or date and time?

It´s used only for the date. The time uses reminder_time

If your input_datetime is date-only or date and time, then there should be no issue passing the state to as_timestamp()… However, a time-only input won’t work with that function. Post the whole error message.

If I understand your goal, this should be a condition not a trigger.

trigger:
  - platform: time
    at: !input reminder_time
condition:
  - condition: template
    value_template: |
      {% set days_between = (now() - states(last_watered) | as_datetime | as_local).days   %}
      {{ days_between >= states('input_number.days_interval') | int }}

Or, you need to add another variable to hold the reminder time input as well so it can be used in a template.

trigger:
  - platform: template
    value_template: |
      {% set days_between = (now() - states(last_watered) | as_datetime | as_local).days   %}
      {{ (now() >= today_at(reminder_time_var) ) and 
      (days_between >= states('input_number.days_interval') | int ) }}

@petro
This scenario might be a nice addition to easy_time.
I actually looked the options over in case you had already thought of it.

No it seems like none of those suggestions work.

The first solution throws:

Error evaluating condition in ‘Pflanzen gießen’: In ‘condition’: In ‘template’ condition: ValueError: Template error: int got invalid input ‘unknown’ when rendering template ‘{% set days_between = (now() - states(last_watered) | as_datetime | as_local).days %} {{ days_between >= states(‘input_number.days_interval’) | int }}’ but no default was specified

The second solution throws

Template variable error: ‘last_watered’ is undefined when rendering ‘{% set days_between = (now() - states(last_watered) | as_datetime | as_local).days %} {{ (now() >= today_at(reminder_time_var) ) and (days_between >= states(‘input_number.days_interval’) | int ) }}’

that’s because you didn’t provide that information in your initial template. He assumed you knew what last_watered was.

1 Like

I defined last_watered as a variable which is set by the blueprint.
How do i have to change this?

variables:
  days_interval: !input days_interval
  last_watered: !input last_triggerd_time
  reminder_time_var: !input reminder_time

You have to use trigger variables instead. variables are resolved after a trigger fires.