Timestamp calculation for automation

Hi all,

I’m using the Rova integration, this integration shown me when i have to take the trash out.
The integration gives me a timestamp for the next pickup, I now have a automation rule that send a telegram message when this date changes. (so i have to take the trash out next morning), this is working fine.
But i like to have this the evening before the date changes. So basicly the timestamp minus 12 hours.
How can a set the automation to do this?
I can’t figure out to take this timestamp -12 hour and trigger the telegram automation.

What does this timestamp value look like? Is it in ISO datetime format like this or something different?

2021-09-29T10:24:05.163235-04:00

Also, what is the sensor’s entity_id?

It looks like this:
afbeelding

OK, it has no timezone information (the term is ‘timezone-naive’).

So that value is telling you to put out the trash at midnight on October 12th but you want to have it reported at noon on October 11th?

Yes exactly, When this date expires, a new one is give (for example 4 weeks lates)
This 12th october is the date that they do the pickup, (can be very early in the morning)
So you have to put it at the 11th at the end of the day, so 12th october minus a couple of our, then trigger to automation to send the message

Copy-paste this into the Template Editor and confirm it reports the desired date and time.

{{ states('sensor.rova_bio') | as_datetime - timedelta(hours=12) }}

Now i get:


That looks okay, just to know: why does the T disaplear?
Do i have to use this template as tigger now? or do have to create another template sensor in my configuration file? and use this new sensor as the trigger? or is there a better way?

The simplest way is to create a Template Sensor with this template:

{{ (states('sensor.rova_bio') | as_datetime - timedelta(hours=12)).isoformat() }}

and set its device_class to timestamp.

Then create an automation with a Time Trigger that references the Template Sensor you created and make its action send a notification reminding you to put out the bin.

I have created the sensor:
For everyone that comes googling for this:
configuration.yaml

sensor:
  - platform: template
    sensors:
      rova_bio_adjusted:
        value_template: "{{ (states('sensor.rova_bio') | as_datetime - timedelta(hours=12)).isoformat() }}"
        device_class: timestamp

In the automation under trigger:
afbeelding

But why does it say No matching entities found? it;s not a typo.

in YAML it looks like:

platform: time
at: sensor.rova_bio_adjusted

So will this trigger type as time with this sensor be triggered?

Look at what you selected “Value of a date/time helper”. It only lists input_datetime entities.

Basically, you have discovered one of the Automation Editor’s several deficiencies, in this case it lacks the means of selecting a timestamp sensor even though that’s a valid choice for a Time Trigger. You have to switch to YAML mode and manually enter the sensor’s entity_id.

On a separate note, the Template Sensor configuration you posted is unformatted. Here’s the formatted version:

sensor:
  - platform: template
    sensors:
      rova_bio_adjusted:
        friendly_name: Rova Bio Adjusted
        value_template: "{{ (states('sensor.rova_bio') | as_datetime - timedelta(hours=12)).isoformat() }}"
        device_class: timestamp

The equivalent using the new way of defining Template Sensors is like this:

template:
  - sensor:
      - name: Rova Bio Adjusted
        state: "{{ (states('sensor.rova_bio') | as_datetime - timedelta(hours=12)).isoformat() }}"
        device_class: timestamp

I have adjusted my previous post, now with preformatted text.

Remove the backquote character after the word timestamp

Done, thanks for the help and advice!

You’re welcome!

Please consider marking my post (above) with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. It will also place a link below your first post that leads to the solution post. All of this helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

The timestamp adjustment works, it shows the correct date.
But the automation didn’t trigger at this date:
In visual editor:


In YAML:

There are no other triggers for this, and no conditions,
So why is this automation not triggering?
The automation should have triggered at 4th october mid day, (as this is shown by the “sensor.rova_residual_adjusted”

Post the state value of sensor.rova_bio_adjusted exactly as it appears in Developer Tools > States.

State is :2021-10-11T12:00:00
State attributes (YAML, optional):

friendly_name: rova_bio_adjusted
device_class: timestamp

afbeelding

(it’s no longet the date from the 4th of october, now its the next pickup)

It lacks a timezone offset so that might be the reason it fails to qualify as a proper timestamp sensor for use with a Time Trigger.

Let’s add your timezone offset and see if it fixes the problem.

Change the Template Sensor’s template to this:

sensor:
  - platform: template
    sensors:
      rova_bio_adjusted:
        friendly_name: Rova Bio Adjusted
        value_template: "{{ (states('sensor.rova_bio') | as_datetime - timedelta(hours=12)).astimezone().isoformat() }}"
        device_class: timestamp

When you are done and have reloaded template entities, post the sensor’s new state value so we confirm it now has a timezone offset.

I have made the change as describted above, hope it works!

Me too. I created a version of it for myself that should trigger tomorrow at noon. I’ll let you know if it works.