Hi everyone,
my problem may be very simple to solve but i don’t know (yet ) the yaml code and I’ve been stuck for several days on this. So, i ask your help
OBJECTIVE: compare my wife and my “next alarm sensor” and update an helper with the lowest value. So, all the morning’s automation will be actived once the first person in the house will wake up.
I have made an automation that should do this comparision any day at 2:00 AM. If I use one “next alarm sensor” the automation works proprely. The problem start with comparision. Below are the ACTION parts of the automation made:
where:
“input_datetime.orario_sveglia” is my helper to update
“sensor.isa_s9_prossimo_allarme” is my wife “next alarm sensor”
“sensor.marco_s9_prossimo_allarme” is my “next alarm sensor”
Can someone hlep on this please? I would really appreciate it.
thanks
I test it again more deeply…I have a problem on the automation…
Test note:
both the “next alarm” sensors are show correctly in HA —> no App problem
The timestamp of the helper works because when i lunch the automation its value change with one of the two “next allarm” sensor time. (not the correct one and this is the problem)… —> no timestamp or helper error
After different test it seem that it works only the “first part (if)” of the automation… in fact, if I put “…marco…” in the second time stamp the helper is update with marco’s next allarm value and viceversa with “…Isa…”. No matter if one allarm is lower or bigger than the other one.
just onther question for for personal knowledge only. Thinking that the problem was the impossibility to make math operation with time i tried to convert the allarm sensors into an int or float value but without succes…
first: would that have been a possible solution?
if yes, what if wrong in the following code (part of code)?
{% if sensor.marco_s9_prossimo_allarme | float > sensor.isa_s9_prossimo_allarme | float %}
{% if states('sensor.marco_s9_prossimo_allarme') | float > states('sensor.isa_s9_prossimo_allarme') | float %}
Not like this:
{% if sensor.marco_s9_prossimo_allarme | float > sensor.isa_s9_prossimo_allarme | float %}
However, you never provided an example of the values of the two sensors. If they look like this: 07:45
then the float filter cannot convert a time string into a floating-point number (and the template will be invalid).
Looks like ferri0685 reported the wrong value (posted the value of the sensor’s Local Time attribute as opposed to its state value).
Anyway, the float filter will definitely fail to convert the sensor’s state value, so ferri0685’s original attempt to compare two floating-point numbers would have failed to work correctly.
This mean that I don’t know how to check the time format of the sensor… , sorry. I just copy the Local time attribute (as 123 correctly say) thinking it was the format… may the formati is “1 luglio 2021, 7:15”? (first row?).
What i know (because i tested) is that the code post by @koying works
I asked for the float / int convertion because it will be usefull add or remove dicrete time from a variable and using number it seem to me more sample…
The screenshot you posted indicates the sensor has an attribute called Time in Milliseconds. It appears to be a Unix Timestamp so it can be used for time calculations (after it has been converted to seconds by dividing by 1000).
The template can be reduced to the following:
{% set marco = state_attr('sensor.marco_s9_prossimo_allarme', 'Time in Milliseconds') /1000 %}
{% set isa = state_attr('sensor.isa_s9_prossimo_allarme', 'Time in Milliseconds') /1000 %}
{{ (marco if marco > isa else isa) | timestamp_custom('%Y-%m-%d %H:%M:00') }}
Alternatively, you can use each sensor’s state value after converting it to a Unix Timestamp.
{% set marco = as_timestamp(states('sensor.marco_s9_prossimo_allarme')) %}
{% set isa = as_timestamp(states('sensor.isa_s9_prossimo_allarme')) %}
{{ (marco if marco > isa else isa) | timestamp_custom('%Y-%m-%d %H:%M:00') }}
thank you both for your feedbacks and suggestions. Every thing it more clear now. @123 : i will try to “play” in some automation with your alternative solution just to deepley understand those.
Thanks again!
I’m trying to add this same automation but I’m getting “Error: Error rendering data template: Result is not a Dictionary” has there been a change in the last 3 years that has made these templates obsolete?
I Just double check my script and it still working properly.
Sorry but I can’t able to help you about the error… I am just I beginner in home assistant.
Trying to help you below you can find my part of my script. You will find different steps but the template used is the same