Q: How to update a helper with a calculated value of another helper

Hi,

In my Lovelace-UI I have a input_datetime which triggers an event. This works. Now I would like to trigger another event 5 minutes before. I suggested that I just have to write an automatisation that updates a helper. But I do not know how to do this.

KR,
Ch.

I don’t see why you need to do that.

Use a Template Trigger in your automation that subtracts 5 minutes from the Input Datetime’s value and checks if the current time is greater than or equal to the result.

Example

  - platform: template
    value_template: >
      {{ now() >= states('input_datetime.your_datetime') | as_datetime | as_local - timedelta(minutes=5) }}

I would like to use this value for serveral events and would also like to display it (for debugging) That’s why I would like to update a helper

Then I suggest you create a Template Sensor.

template:
  - sensor:
      - name: Whatever Name You Want
        device_class: timestamp
        value_template: >
          {{ (states('input_datetime.your_datetime') | as_datetime | as_local - timedelta(minutes=5)).isoformat() }}

You can reference the Template Sensor directly in a Time Trigger (or in a condition or wherever else you need it).


If you are uncomfortable with configuring a Template Sensor using YAML, you can configure it using the UI.

Settings > Device & Services > Helpers > Create Helper > Template > Template a sensor

In addition to Taras’ comments, if your original Input datetime is time-only you will need to use a slightly different template:

{{ (today_at(states('input_datetime.example')) - timedelta(minutes=5)).time() }}

Thanks a lot for your hints! I will try this in the evening, when back @ my smart home :wink:

KR,.
Ch.

P.S: I have to admit I am quite new to HA (I moved from another architecture) and I still have to get used to the underlying concept.

Mhm, it does not work as intended, this error is popping up in the logfiles:

Logger: homeassistant.components.sensor.helpers
Source: components/sensor/helpers.py:22
Integration: Sensor (documentation, issues)
First occurred: 21:23:31 (7 occurrences)
Last logged: 21:28:53

None rendered invalid timestamp: 06:25:00
sensor.weckzeit_vorlauf rendered invalid timestamp: 06:25:00
sensor.weckzeit_vorlauf rendered invalid timestamp: 05:55:00
None rendered invalid timestamp: 05:55:00

I entered 06:30 and 06:00 in the datetime input. For me this look like that I did a mistake somewhere with the format of the sensor.

If you are using the time-only version I posted, remove the device class line or leave the field blank if you are using the GUI Template helper. I should have included that in my earlier post.

Thanks, now it works!