Automation time trigger not firing

Hello all,
I am despairing of the automation time trigger:

The following scenario:

I want to open my shutters at a certain time. To do this, I define the time parameters for weekdays, weekends and holidays via the frontend.
I have a template that calculates the opening time “today”.
On the desktop the template entity is displayed very well, e.g. “07:30:00”.
However, when I use the entity as a time trigger in an automation, the trigger does not fire!

I have already tried everything, e.g. also device_class: timestamp or state_attr(as_timestamp(…)), direct use of the entity without states(), … - but all that always results in error messages.
It seems that the template just does not want to be a timestamp type, even though it looks correct on the desktop.
When I use the input_timedate entities directly as time triggers in automation, the trigger fires. So it can’t be the entity itself.

What am I doing wrong? Here comes the code:

input_datetime:
  fire_time_weekdays:
    has_date: false
    has_time: true

  fire_time_weekend:
    has_date: false
    has_time: true

  fire_time_holiday:
    has_date: false
    has_time: true
    
sensor: 
  - platform: template
    sensors:
      next_fire:
        friendly_name: „today’s time to fire“
        value_template: >
          {% set holiday = is_state('binary_sensor.holiday_sensor', 'on') %}
          {% if holiday %}
            {{ states('input_datetime.fire_time_holiday') }}
          {% elif now().weekday() in [5, 6] %}
            {{ states('input_datetime.fire_time_weekend') }}
          {% else %}
            {{ states('input_datetime.fire_time_weekdays') }}
          {% endif %}

I’d do this by triggering off all of the input datetimes, and use or’d template conditions to choose whether to proceed. No need for the template sensor then.

condition:
  - or:
    - "{{ is_state('binary_sensor.holiday_sensor', 'on') and trigger.entity_id == 'input_datetime.fire_time_holiday' }}"
    - (add the other two yourself, I'm on a phone! )
    - (make sure they exclude it being a holiday)

I think your quotes in the friendly_name will be causing a problem. You could create this template sensor in the UI: use the same template in the State Template box.

If you want to use a Template Sensor as the source for a Time Trigger, its device_class must be timestamp and its state value should be a datetime object or a timestamp in ISO 8601 format.

sensor: 
  - platform: template
    sensors:
      next_fire:
        friendly_name: "Today’s time to fire"
        device_class: timestamp
        value_template: >
          {% if is_state('binary_sensor.holiday_sensor', 'on') %}
            {% set entity = 'input_datetime.fire_time_holiday' %}
          {% elif now().weekday() in [5, 6] %}
            {% set entity = 'input_datetime.fire_time_weekend' %}
          {% else %}
            {% set entity = 'input_datetime.fire_time_weekdays' %}
          {% endif %}
          {{ today_at(states(entity)).isoformat() }}

The following version of the template reduces some of the redundancy present in the previous version.

sensor: 
  - platform: template
    sensors:
      next_fire:
        friendly_name: "Today’s time to fire"
        device_class: timestamp
        value_template: >
          {% if is_state('binary_sensor.holiday_sensor', 'on') %}
            {% set entity = 'holiday' %}
          {% elif now().weekday() in [5, 6] %}
            {% set entity = 'weekend' %}
          {% else %}
            {% set entity = 'weekdays' %}
          {% endif %}
          {{ today_at(states('input_datetime.fire_time_' ~ entity)).isoformat() }}

EDIT

If this Template Sensor will be used by just one automation then you may as well just create a Template Trigger and eliminate the Template Sensor.

alias: example
trigger:
  - platform: template
    value_template: >
      {% set entity = 'holiday' if is_state('binary_sensor.holiday_sensor', 'on') 
         else 'weekend' if now().weekday() in [5, 6]
         else 'weekdays' %}
      {{ now().strftime('%H:%M:00') == states('input_datetime.fire_time_' ~ entity) }}
condition: []
action:
  ... your actions ...

thanks a lot!
As an HA newbie and still have a lot to learn… ;D
And will asap try your suggestion!

Sry, the “double quotes” are a typical german thing, in reality they are both upper (" " - probably a copy&paste issue) and don’t make any trouble, but anyway I’ll try hard to get used to ‘single quotes’ …

Ah, I believe that’s the key - without this, device_class: timestamp wasn’t accepted.

Many thx, I’ll try it asap :slight_smile:

PS. The issue is that I need it NOT ONLY in one automation, because of specific shutters get own automations with own conditions (sleep late… :grin:)

In that case, your original plan to use a Template Sensor was correct, just that you needed to configure it correctly as explained in my previous post.

If you are interested, you can use the streamlined version of the template in the Template Sensor.

sensor: 
  - platform: template
    sensors:
      next_fire:
        friendly_name: "Today’s time to fire"
        device_class: timestamp
        value_template: >
          {% set entity = 'holiday' if is_state('binary_sensor.holiday_sensor', 'on') 
             else 'weekend' if now().weekday() in [5, 6]
             else 'weekdays' %}
          {{ today_at(states('input_datetime.fire_time_' ~ entity)).isoformat() }}

Many thanks, Taras !!!

Your first proposal works for me but only without states() in the last line:

{{ today_at(entity).isoformat() }}`

For some reason, the streamlined version doesn’t work - I may have made a typo - never mind :wink:

… and the best news: the trigger fired! :clap: :clap: :clap: :sweat_smile:

In that case, I don’t think you used my example exactly the way I posted it above. However, that’s not important at this point, only that the template ultimately produces a proper ISO 8601 timestamp.

Glad to hear it fixed the problem.

Please consider marking my post above with the Solution tag. Only you, the author of thiis topic, can do that (or a moderator) and it will automatically place a checkmark next to the topic’s title. This signals, to other users who may have a similar question, that this topic has been resolved

Ah ok thx → done!

You might be right: I needed to adopt your solution, as my specific program is full of german expressions that i didn’t want to bother anyone in this post… anyway, it works and you made my day (resp. night :sweat_smile: )

1 Like

Well, this lesson was really very instructive and I am glad that now I know this and can deal with it.
Meanwhile, I’ve also learned that I could have solved it differently: I didn’t realize that I could query in the ‘action:’ section with further conditions in if…then clauses. In this respect, a single automation is actually enough for my original purpose - but what the heck…

I also find it interesting that you can identify multiple triggers by using ‘trigger_id’ and even within a single automation formulate specific conditions depending on which trigger has fired… this could also be very helpful for me - I already have a use case in mind… :slight_smile:

I’m almost afraid that HA is so diverse and powerful that I’ll never grasp and understand all its functions, even if they were actually useful to me…