woempiej
(Woempiej)
July 27, 2023, 5:11pm
1
Hi,
Does anyone know how I can use a date/time sensor as a automation trigger.
The sensor value is shown below;
I want to use it as a trigger in a automation to create a calendar event so I can sent myself a notification 5 minutes before 25 juli 2023 at 20:30
woempiej
(Woempiej)
July 27, 2023, 5:48pm
3
Thanks for pointing, I had this page actually opened on one of my browser tabs
Long day I guess
woempiej
(Woempiej)
July 27, 2023, 5:56pm
4
But how can I configure a offset on this trigger?
If the sensor gets the state for example “29-07-2023 at 20:30”
It only triggers if the time of the sensor is equal to “time now”
rossk
July 28, 2023, 6:04am
5
You can’t use offset with date time triggers at present (really don’t know why).
I am sure it could probably be done via template trigger but that’s defo not my field of expertise.
1 Like
tom_l
July 28, 2023, 7:17am
6
Yes a template trigger would be required if you want an offset. e.g.
trigger:
- platform: template
value_template: "{{ now() >= states('sensor.my_time_sensor')|as_datetime - timedelta(minutes = 5) }}"
woempiej
(Woempiej)
July 28, 2023, 4:52pm
7
Hi,
Thanks for the example Tom, I’m always struggling with templating,
The template right now shows a type error but that is because the sensor status right now
is “unknown” I think?
Sensor state:
Because when I add a sensor with a value (last delivery sensor with a state) it returns;
Sensor state:
Why the boolean state shows “true”?
tom_l
July 29, 2023, 4:01am
8
You will need to define a default if your sensor value is occasionally unknown.
Also it would help if you show the actual state of the sensor (developer tools → states) when it has a date and time to ensure it does not need formatting first.
woempiej
(Woempiej)
July 29, 2023, 7:38am
9
The format of the sensor is UTC time. My local time is +2
2023-07-31T15:15:00+00:00
tom_l
July 29, 2023, 8:01am
10
Perfect. as_datetime()
can work with that. So you just need to define a default (that will result in a false
template result) for the case when your sensor is unavailable. Maybe something like this (untested)
trigger:
- platform: template
value_template: "{{ now() >= states('sensor.my_time_sensor')|default(now() + timedelta(minutes = 10))|as_datetime - timedelta(minutes = 5) }}"
woempiej
(Woempiej)
July 29, 2023, 8:01am
11
This format works with my local time
{{ now() >= states('sensor.picnic_next_delivery_slot_start')| as_datetime | as_local - timedelta(minutes = 15) }}
tom_l
July 29, 2023, 8:03am
12
Yeah ok, add |as_local
to the end of the version I just posted that takes care of the unavailable state.
woempiej
(Woempiej)
July 29, 2023, 8:16am
13
Hmm, it works when the sensor has a value
But when I use a sensor with no value the template gives an error;
{{ now() >= states('sensor.picnic_next_delivery_eta_start')|default(now() + timedelta(minutes = 10))|as_datetime - timedelta(minutes = 5)|as_local }}
sensor state;
woempiej
(Woempiej)
July 29, 2023, 10:08am
14
Based on a your inffo and a little search I made this.
But I think the automation fires also when the sensor state gets “unknown” right?
trigger:
- platform: template
value_template: |-
{% if states('sensor.picnic_next_delivery_slot_start') != 'unknown' %}
{{ now() >= states('sensor.picnic_next_delivery_slot_start')|default(now() + timedelta(minutes = 10), true)|as_datetime|as_local - timedelta(hours = 53, minutes = 15) }}
{% else %}
False
{% endif %}
condition:
- condition: template
value_template: ""
action:
- service: notify.mobile_app
tom_l
July 29, 2023, 10:41am
15
Ok this is getting confusing, lets fall back to using timestamps instead. Try this:
trigger:
- platform: template
value_template: >
{{ now().timestamp() >= as_timestamp( states('sensor.picnic_next_delivery_slot_start'), now().timestamp() + 301 ) - 300 }}
I am now at my PC so have tested this in the template editor.
1 Like
woempiej
(Woempiej)
July 29, 2023, 11:58am
16
Oké I will test both of them @ the next delivery, thanks
Is the “-300” value the 300 seconden (5 minutes before) timer?
tom_l
July 29, 2023, 12:10pm
17
Yes exactly that. And the default value (301s) is set just larger than that for the ‘unavailable’ case, so the template evaluates as false and does not trigger.
1 Like
woempiej
(Woempiej)
July 29, 2023, 12:34pm
18
Do I have to use the |as_local somewhere?
Because the sensor time is UTC?
No I don’t
1 Like