Automation trigger time at not working with a template

I have built a GUI based on sliders to pick a time (hours, minutes) for my sprinklers to start. After that I am concatenating the values into a string using this:

sensor sprinkler time:
platform: template
sensors:
sprinklertime:
friendly_name: ‘Sprinkler Time’
value_template: ‘{{ states.input_number.sprinklerhour.state | int }}:{% if states.input_number.sprinklerminutes.state | length == 3%}0{% endif %}{{ states.input_number.sprinklerminutes.state |int }}’

The value renders correctly as confirmed by the template GUI: 19:00:00

But when trying to create a time triggered automation, the config does not validate:

This is the start of the automation:

- alias: ‘Sprinkler Schedule’
initial_state: true
trigger:
- platform: time
at: “{{ states.sensor.sprinklertime.state }}:00”

You can’t use a template with a time trigger, you need to use a template trigger :wink:

I also tried that but as per the docs, you can not use “now()” in a template trigger

value_template: '{{ (as_timestamp(now()) | int | timestamp_custom("%H:%M")) == states.sensor.sprinklertime.state }}'

Rendering templates with time ( now() ) is dangerous as trigger templates only update based on entity state changes.

Yes, use the time sensor in the template :slight_smile:

2 Likes

Thanks @anon43302295, I was finally able to set it up using the time sensor hackaround. It seems like its being used already for other examples around the forum but I did not know it was not part of the standard “sensors”

I will like to describe the solution for other fellas:

Create a “time based sensor” that will hold the current time

  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'time_date'
      - 'time_utc'

Note that you will only need the “time” display option but the others are handy

Create an automation with a “template” trigger type:

- alias: 'Sprinkler Schedule'
  initial_state: true
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.sprinklers_start_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"

Do NOT use the “at” type with a template as @anon43302295 already told us

1 Like

I can not seem to get this time based template trigger to work…

I set up my Time sensor, which seems to work fine (displays correctly in Lovelace):

- platform: time_date
  display_options:
    - 'time'
    - 'date'
    - 'date_time'

And I set up my automation like this:

- id: '1578424590085'
  alias: Doorbell Chime Off
  description: ''
  trigger:
  - platform: template
    value_template: '"{{ states(''sensor.time'') == (states.input_datetime.doorbell_chime_off.attributes.timestamp | int | timestamp_custom(''%H:%M'', False)) }}"'
  condition: []
  action:
  - entity_id: switch.doorbell_chime
    service: switch.turn_off

Still will not fire :frowning:

image

After playing around in the Template editor, I confirmed that a few variations seem to work fine there, but neither will trigger the automation.

If anyone has any ideas as to why this isnt working, I would love to hear them. Thanks!

Hello do you solve the problem? I have the same issue!

Yes, I did. A few notes:

  • Do not use “at” as it expects a hardcoded value
  • Slowly try your template in the Developer tools -> template tab. By slowly I mean, put each part independently to verify the syntax, and at the end, try the full string and verify the result changes to True once the time matches the current time
  • If the template works on the template viewer, then it should trigger your automation on the template trigger type

To add to this, be careful with the automation editor in the GUI.
I built my automation there, and it added a few extra quotes in unnecessary places, which can be seen in my previous post.
Opening the automation.yaml file and editing manually solved this for me.

Before I corrected it:

- id: '1578424590085'
  alias: Doorbell Chime Off
  description: ''
  trigger:
  - platform: template
    value_template: '"{{ states(''sensor.time'') == (states.input_datetime.doorbell_chime_off.attributes.timestamp | int | timestamp_custom(''%H:%M'', False)) }}"'
  condition: []
  action:
  - entity_id: switch.doorbell_chime
    service: switch.turn_off

And after I corrected it:

- id: '1578424590085'
  alias: Doorbell Chime Off
  description: ''
  trigger:
  - platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.doorbell_chime_off.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  condition: []
  action:
  - entity_id: switch.doorbell_chime
    service: switch.turn_off

It is now triggering perfectly and working like a charm.