Trigger automation 15 minutes before date_time stored in input_datetime

Post the other sensor. Maybe it has more useful data because this one is nothing more than a text string (which makes me wonder why you tried to use its timestamp attribute when it clearly doesn’t have one).

Hehe, i alrady said i dont understand it…

Paste this into the Template Editor and see if it produces the result you want:

{{ (states('sensor.fjr_p') | as_datetime | as_local - timedelta(minutes=15)).timestamp() | timestamp_custom('%H:%M') }}

2 Likes

The time is correct now! Do i need to make a sensor of it and then use it for automations or is there another way for this?

It’s your choice, it can be used in an automation’s Template Trigger or in a Template Sensor. It all depends on your intended application.

Thank you verry much! Gonna test this out.

I don’t know if you are aware but if you intend to use it in a Template Trigger, you will need to enhance it slightly:

{{ now().ctime()[11:16] >= (states('sensor.fjr_p') | as_datetime | as_local - timedelta(minutes=15)).timestamp() | timestamp_custom('%H:%M') }}

Template trigger is the one in the automations right?

Edit:

It is working! I dont understand it but it is working. Now gonna use the same template and edit a bit. Where can i learn those things?

This thread helped me quite a bit. However I am struggling quite a bit. I am trying to calculate a trigger time based on an input_datetime and a numeric sensor. When I try it in the Jinja 2 template editor I get results, but when I try to make sensors out of them I only get unkown. I am wondering what I am doing wrong.

- platform: template
  sensors:   
    timestamp_geplante_fahrt:
      value_template: '{{ as_timestamp(states("input_datetime.geplante_fahrt")) | int }}'
      device_class: timestamp
    time_to_charge:
      friendly_name: 'Time to Charge'
      value_template: '{{ (as_timestamp(states("input_datetime.geplante_fahrt")) | int) - ((states("sensor.sportage_estimated_charge_duration") | int) * 60) }}'
      device_class: timestamp

you can use input_datetime directly in time triggers, same with timestamp sensors. You don’t need the first template.

Is sensor.sportage_estimated_charge_duration a duration sensor? Anyways, you aren’t specifying a default or providing a availability template, so if that sensor is unknown/unavailable, the template will error.

- platform: template
  sensors:   
    time_to_charge:
      friendly_name: 'Time to Charge'
      value_template: >
        {{ states("input_datetime.geplante_fahrt") | as_datetime - timedelta(seconds=states("sensor.sportage_estimated_charge_duration") | int) }}
      availability_template: >
        {{ states("sensor.sportage_estimated_charge_duration") | is_number }}
      device_class: timestamp

I had to swap “seconds” with “minutes” since that sensor is in minutes and get a result in the template editor but the sensor is still unkown… I swapped to the new template syntax after realizing that something new exists.

- sensor:
  - unique_id: time_to_charge
    name: 'Time to Charge'
    state: '{{ states("input_datetime.geplante_fahrt") | as_datetime - timedelta(minutes=states("sensor.sportage_estimated_charge_duration") | int) }}'
    availability: '{{ states("sensor.sportage_estimated_charge_duration") | is_number }}'
    device_class: date

It is probably something very stupid, but I do not get it…

it’s most likely an error due to your quoting. That’s why I was avoiding it in my response by using the multiline notation, but you switched it back to a single line template. Try copy/pasting exactly what I wrote.

tried the multiline implementation without any success.

can you post exactly what you tried?

- sensor:
  - unique_id: time_to_charge
    name: 'Time to Charge'
    state: >
      {{ states("input_datetime.geplante_fahrt") | as_datetime - timedelta(minutes=states("sensor.sportage_estimated_charge_duration") | int) }}
    availability: >
      {{ states("sensor.sportage_estimated_charge_duration") | is_number }}
    device_class: date

oh wait it fails rendering the device_class date. let me try timestamp.

it should be a timestamp device_class.

in the log I can see this now:

Logger: homeassistant.components.sensor.helpers
Source: components/sensor/helpers.py:26
Integration: Sensor (documentation, issues)
First occurred: 5:17:20 PM (1 occurrences)
Last logged: 5:17:20 PM

sensor.time_to_charge rendered timestamp without timezone: 2023-04-25 03:50:00

and the sensor is unkown. do I need to add the timezone to the timestamp string?

Add | as_local after the as_datetime before the -

We have a winner. Thanks for your support and patience!