Trigger automation with timestamp-class sensor

I thought I would post this - it might help others.
(It has taken me hours to figure this out!)

Automations can be triggered with a timestamp-class sensor.
This is what I’ve used:

  - sensor:
      - name: "my timestamp-class sensor"
        device_class: timestamp
        state: >
            {{(states('input_datetime.my_test_datetime_helper')|as_datetime|as_local).isoformat()}}   

Perhaps there’s a simpler way!

Why not just trigger on the input datetime?

Indeed. That even has its own section in the time trigger documentation.

image

Because the actual sensor is very complex.

What sensor?
It’s a datetime helper.
You named it my test datetime helper.

  - sensor:
      - name: "Linktap scheduled start time isoformat"
        device_class: timestamp
        state: >
            {% set a = states('input_number.linktap_default_am_time_in_minutes')|float %}
            {% set b = (a/60)|int %}
            {% set c = (a - (b*60))|int|string %}
            {% if c|length==1 %}
                {% set c = "0" + c %}
            {% endif %}
            {% set am = b|string + ":" + c|string + ":00" %}
            
            {% set a = states('input_number.linktap_default_pm_time_in_minutes')|float %}
            {% set b = (a/60)|int %}
            {% set c = (a - (b*60))|int|string %}
            {% if c|length==1 %}
                {% set c = "0" + c %}
            {% endif %}
            {% set pm = b|string + ":" + c|string + ":00" %}
            
            {% if states('input_boolean.linktap_am_pm_0') == "on"  %}
              {% set tod = pm %}
            {% else %}
              {% set tod = am %}
            {% endif  %}
            {% set day = states('input_datetime.linktap_date_0') %}
            {% set thedate = day + " " + tod %}
            {{(thedate|as_datetime|as_local).isoformat()}}

(I’m sure that can be simplified :stuck_out_tongue_closed_eyes: )

@tom_l is there any documention about timestamp-class sensors?
This isn’t very intuitive:

|as_datetime|as_local).isoformat()

Sensor - Device Class

Either a datetime object or timestamp string (ISO 8601) is acceptable… in your example the .isoformat() is superfluous because you already have a datetime object.

I think the following should do the same thing:

{% set am_pm = 'pm' if is_state('input_boolean.linktap_am_pm_0', 'on') else 'am' %}
{% set ent = 'input_number.linktap_default_'~am_pm~'_time_in_minutes' %}
{% set mins = states(ent)|int(0) %}
{% set date = states('input_datetime.linktap_date_0')|as_datetime|as_local %}
{{ date + timedelta(minutes=mins) }}

Thanks @Didgeridrew.
That is so funny - 5 lines instead of 22 :smile:
There’s some useful stuff there for me to learn - thank you.
(As you can tell I’m not very good at this yet.)

The sensor is part of quite a big project (and I am extremely proud of it).
Here’s one of the dashboards: