Picnic date/time sensor as automation trigger

Hi,

Does anyone know how I can use a date/time sensor as a automation trigger.
The sensor value is shown below;

afbeelding

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

1 Like

Thanks for pointing, I had this page actually opened on one of my browser tabs :crazy_face:
Long day I guess :wink:

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”

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

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) }}"

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:
afbeelding

Because when I add a sensor with a value (last delivery sensor with a state) it returns;

Sensor state:
afbeelding

Why the boolean state shows “true”?

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.

The format of the sensor is UTC time. My local time is +2

2023-07-31T15:15:00+00:00

afbeelding

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) }}"

This format works with my local time

{{ now() >= states('sensor.picnic_next_delivery_slot_start')| as_datetime | as_local - timedelta(minutes = 15) }}

Yeah ok, add |as_local to the end of the version I just posted that takes care of the unavailable state.

Hmm, it works when the sensor has a value :slight_smile:
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;

afbeelding

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

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

Oké I will test both of them @ the next delivery, thanks :slight_smile:

Is the “-300” value the 300 seconden (5 minutes before) timer?

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

Do I have to use the |as_local somewhere?
Because the sensor time is UTC?

No I don’t :slight_smile:

1 Like