Dear all, I have been searching and trying but failing. I guess it is simple but I am not getting it.
I want to trigger an action based on a date/time that I am calculating with helpers.
my automation:
alias: test my own time helper
description: ""
triggers:
- trigger: time
at: sensor.haus_start_auto_laden_mit_haus_batterie
conditions: []
actions:
- action: notify.persistent_notification
metadata: {}
data:
message: Trigger based on my own helpers
mode: single
my template (and I guess I am doing something awfully wrong here);
do you mean not matching from a format point of view or matching state wise, i.e. the value (date and time) my sonsor is holding is never caught by the automation for some reason?
Is it an issue that it is of type string (in the template I set it to class timestamp, but I guess that is irrelevant if the value itself is not properly set)?
I will research on how to do the formatting of my sensor to match the one of now() or follow your approach with defining as int.
However, I want to zoom out again: Shouldn’t the format be completely irrelevant when using a time sensor in an automation where the trigger condition is a time(stamp)? I mean why would I need to take care of the formating of something that has many faces (as in many formats ) while always being the same thing: a point in time
Sure but you are comparing your customized time sensor output against a time stamp and saying they have to match. - trigger: time has to equal at: sensor.haus_start_auto_laden_mit_haus_batterie
Format and output type matters a whole bunch. You can’t compare a number to a letter, correct?
A=2 will never be true…
A string will never equal a number and that logic applies to time and dates.
Time is probably close to the most difficult to compare. It also brings in how often the timestamp is reported. I just would avoid it if other triggers are available.
When you added math (+,-,float) with this, the standard format went out the window
I hear you and I get your point. I guess I was hoping there is some magic happening defining my sensor as timestampt to convert it to a date_time-object and makes sure, that it can be compared to other time objects without the formating of it. But I also get your point: A != 2
I will try for some hours now and post an update. Thanks already (I will edit this post with real update so that this forum is not turning into a chat).
formating the output of my sensor to match what now() has as standard output:
My code for testing
{% set test = states.sensor.sun_next_rising.state | as_datetime + timedelta(minutes=-210) - timedelta(minutes=float(states.sensor.haus_verfugbare_auto_lade_dauer.state)) %}
value of my sensor
{{ test.strftime('%Y-%m-%d %H:%M:%S.%f+02:00') }}
value of now()
{{now() }}
the novice’s question: Even if the format of the sensor value matches to what we assume is used in the automation when checking: “is it now the time as defined in the sensor, then do something”: how would homeassist find a match even with milliseconds when doing he comparison? I guess I still don’t understand how the trigger is working:
alias: Auto_Nachts_AutoLaden
description: ""
triggers:
- trigger: time
at: sensor.haus_start_auto_laden_mit_haus_batterie
conditions: []
actions:
- action: notify.persistent_notification
metadata: {}
data:
message: Ladung in der Nacht gestartet
mode: single
the only difference I can see is the usage of
... timedelta(minutes=-210) ...
i.e. a negative value in my code that is not working; rest of code looks identical.
The core learning for me: I need to convert the value of the sensor for the next sun-rise to a datetime-object by using as_datetime. Only then the automation using a time as trigger is working.
As noted in the “States” section of the Templating docs LiQuid_cOOled linked to earlier, you should avoid using the state-object method shown above, unless it is necessary.
In some cases these errors can cause entities to get stuck in unknown or unavailable state after restart. That means they can cause knock-on issues in both the frontend as well as in your scripts and automations if they are used there.