Time sensor and automation

Hi,

I’ve been really struggling to fix something I believe is simple, I must be missing something.
I have a template sensor that I want to use for multiple automation:

- template:
  - sensor:
    - name: wake_up_time
      state: >
        {%if states('binary_sensor.today_is_workday') == 'off' -%}
          {{ (states('input_datetime.holidays_wake_up_time')) }}
        {%- elif states('input_boolean.school_holidays') == 'on' -%}
            {{ (states('input_datetime.school_holiday_wake_up_time')) }}
        {%- else -%}
            {{ (states('input_datetime.default_wake_up_time')) }}
        {%- endif %}
      icon: "mdi:clock"

this sensor looks ok when I check his state with the developer template tool:

{{ states('sensor.wake_up_time')  }}  =>   wake_up_time  18:53:00

however my automation

- alias: "Parents wake up"
  condition:
    - condition: state
      entity_id: input_boolean.automation
      state: "on"
  trigger:
    - platform: time
      at: sensor.wake_up_time
  action:
    - service: homeassistant.turn_on
      entity_id:
        - switch.curtains_master
        - switch.curtains_living

is never triggered…what am I missing??

Thanks in advance

Add device_class: timestamp to your template sensor.

https://www.home-assistant.io/docs/automation/trigger/#sensors-of-datetime-device-class

thanks for your answer, but I m afraid it still not working

- template:
  - sensor:
    - name: wake_up_time
      device_class: timestamp
      state: >
        {%if states('binary_sensor.today_is_workday') == 'off' -%}
          {{ (states('input_datetime.holidays_wake_up_time')) }}
        {%- elif states('input_boolean.school_holidays') == 'on' -%}
            {{ (states('input_datetime.school_holiday_wake_up_time')) }}
        {%- else -%}
            {{ (states('input_datetime.default_wake_up_time')) }}
        {%- endif %}
      icon: "mdi:clock"

Interestingly the template editor shows now :

{{ states('sensor.wake_up_time') }}
unknown

would you have an ideaof anything else missing?

If you add this to a Template Sensor:

device_class: timestamp

then the value produced by the Template Sensor should include time and date in ISO format. For example:

- template:
  - sensor:
    - name: wake_up_time
      device_class: timestamp
      state: >
        {%if states('binary_sensor.today_is_workday') == 'off' -%}
          {{ today_at(states('input_datetime.holidays_wake_up_time')).isoformat() }}
        {%- elif states('input_boolean.school_holidays') == 'on' -%}
            {{ today_at(states('input_datetime.school_holiday_wake_up_time')).isoformat() }}
        {%- else -%}
            {{ today_at(states('input_datetime.default_wake_up_time')).isoformat() }}
        {%- endif %}
      icon: "mdi:clock"

Alternative:

- template:
  - sensor:
    - name: wake_up_time
      device_class: timestamp
      state: >
        {% if is_state('binary_sensor.today_is_workday', 'off') -%}
          {% set t = 'holidays_wake_up_time' %}
        {%- elif is_state('input_boolean.school_holidays', 'on') -%}
          {% set t = 'school_holiday_wake_up_time' %}
        {%- else -%}
          {% set t = 'default_wake_up_time' %}
        {%- endif %}
        {{ today_at(states('input_datetime.' ~ t)).isoformat() }}
      icon: "mdi:clock"
1 Like

Thank you very much, indeed adding the

today_at(

make my sensor a valid time sensor, and my automation worked!.. The first day …

But the date of the sensor stays at the date of the last update, so if I don’t change my input_datetime, the next day , wake_up_time is if set to previous day and the automation is not trigger.

I guess an event “the date has changed” should update it, but looks like today_at is not triggering it…

Any idea please? ( And thanks for the help so far)

A Template Sensor’s template is updated whenever one of the entities referenced in the template changes state. If none of the entities change state, the template is not updated.

I suggest you create a Trigger-based Template Sensor like this:

- template:
    - trigger:
        - platform: time
          at: '00:00:01'
        - platform: state
          entity_id:
            - binary_sensor.today_is_workday
            - input_datetime.holidays_wake_up_time
            - input_boolean.school_holidays
            - input_datetime.default_wake_up_time
      sensor:
        - name: wake_up_time
          device_class: timestamp
          state: >
            {% if is_state('binary_sensor.today_is_workday', 'off') -%}
              {% set t = 'holidays_wake_up_time' %}
            {%- elif is_state('input_boolean.school_holidays', 'on') -%}
              {% set t = 'school_holiday_wake_up_time' %}
            {%- else -%}
              {% set t = 'default_wake_up_time' %}
            {%- endif %}
            {{ today_at(states('input_datetime.' ~ t)).isoformat() }}
          icon: "mdi:clock"

It will update at the start of every day and when any of the entities changes state.

Thank you so much for the detailed explanation, works perfectly!

1 Like