SOLVED: Compare two "next alarm" phone sensors and update an helper

Hi everyone,
my problem may be very simple to solve but i don’t know (yet :wink:) the yaml code and I’ve been stuck for several days on this. So, i ask your help :pray:

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:

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.orario_sveglia
data_template:
  datetime: >-
    {% if 'sensor.marco_s9_prossimo_allarme' < 'sensor.isa_s9_prossimo_allarme'}
      as_timestamp(states('sensor.marco_s9_prossimo_allarme'))|timestamp_custom('%Y-%m-%d %H:%M:00')
    {%- else -%}
      {% if 'sensor.marco_s9_prossimo_allarme' > 'sensor.isa_s9_prossimo_allarme'}
        as_timestamp(states('sensor.isa_s9_prossimo_allarme'))|timestamp_custom('%Y-%m-%d %H:%M:00')
    {%- endif %}

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

Try this for your data_template

    {% if 'sensor.marco_s9_prossimo_allarme' > 'sensor.isa_s9_prossimo_allarme' %}
      {{ as_timestamp(states('sensor.isa_s9_prossimo_allarme')) | timestamp_custom('%Y-%m-%d %H:%M:00') }}
    {%- elif 'sensor.marco_s9_prossimo_allarme' <= 'sensor.isa_s9_prossimo_allarme' %}
      {{ as_timestamp(states('sensor.marco_s9_prossimo_allarme')) | timestamp_custom('%Y-%m-%d %H:%M:00') }}
    {%- endif %}
1 Like

Thanks a lot Craig for your feedback! I really appreciate it
I just update the automation and no errors occours that (form me) it is a big step :sweat_smile:

I will try to push the automation as soon my wife it will come back at home…actually her sensors is not actived :unamused:

i will back soon. thanks

1 Like

I tested and it works perfectly :smiley:.
Thanks so much for your help!

1 Like

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.

So, if possible, I need your help please!!

Thanks

  • All HA states are inherently strings, so you should do your comparisons on timestamps.
  • In your case, as you only have 2 sensors, the elif is superfluous.
    {% if as_timestamp(states('sensor.marco_s9_prossimo_allarme')) > as_timestamp(states('sensor.isa_s9_prossimo_allarme')) %}
      {{ as_timestamp(states('sensor.isa_s9_prossimo_allarme')) | timestamp_custom('%Y-%m-%d %H:%M:00') }}
    {%- else %}
      {{ as_timestamp(states('sensor.marco_s9_prossimo_allarme')) | timestamp_custom('%Y-%m-%d %H:%M:00') }}
    {%- endif %}
1 Like

Thanks a lot! you make my day :smile:!
it work properly :+1:

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 %}

thanks

This is how to do that:

{% 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).

you are right…The sensor has the following format on the entity page:
Thu Jul 01 07:15:00 GMT+02:00 2021

If that’s the time format of each sensor’s value then as_timestamp() will also fail to convert it correctly.

The “next alarm” from the Android companion, at least, is an ISO date.

image

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.

Oh, yeah, definitely.

OP, fwiw, as_timestamp returns an int which is the number of seconds since 1st Jan 1970. You can do math on that one.

This mean that I don’t know how to check the time format of the sensor… :sweat:, 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?).
chrome_wvWnBz2lIG
What i know (because i tested) is that the code post by @koying works :partying_face:

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…

Yeah, it shows a formatted timestamp there for me, too.
You can see the actual state with the developer tools.

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

choose:
  - conditions:
      - condition: and
        conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.marco_cell_prossimo_allarme') == 'unavailable'
              }}
          - condition: template
            value_template: >-
              {{ states('sensor.isa_pixel6_prossimo_allarme') == 'unavailable'
              }}
    sequence:
      - service: input_datetime.set_datetime
        target:
          entity_id: input_datetime.orario_sveglia
        data:
          datetime: >-
            {{ (as_timestamp(now()) + (24*3600)) | timestamp_custom('%Y-%m-%d
            08:00:00') }}
  - conditions:
      - condition: template
        value_template: "{{ states('sensor.marco_cell_prossimo_allarme') == 'unavailable' }}"
    sequence:
      - service: input_datetime.set_datetime
        target:
          entity_id: input_datetime.orario_sveglia
        data:
          datetime: >-
            {{ as_timestamp(states('sensor.isa_pixel6_prossimo_allarme')) |
            timestamp_custom('%Y-%m-%d %H:%M:00') }}
      - service: input_select.select_option
        target:
          entity_id: input_select.prima_sveglia
        data:
          option: Isa
  - conditions:
      - condition: template
        value_template: "{{ states('sensor.isa_pixel6_prossimo_allarme') == 'unavailable' }}"
    sequence:
      - service: input_datetime.set_datetime
        target:
          entity_id: input_datetime.orario_sveglia
        data:
          datetime: >-
            {{ as_timestamp(states('sensor.marco_cell_prossimo_allarme')) |
            timestamp_custom('%Y-%m-%d %H:%M:00') }}
      - service: input_select.select_option
        target:
          entity_id: input_select.prima_sveglia
        data:
          option: Marco
  - conditions:
      - condition: template
        value_template: >-
          {{ as_timestamp(states('sensor.marco_cell_prossimo_allarme')) <
          as_timestamp(states('sensor.isa_pixel6_prossimo_allarme')) }}
    sequence:
      - service: input_datetime.set_datetime
        target:
          entity_id: input_datetime.orario_sveglia
        data:
          datetime: >-
            {{ as_timestamp(states('sensor.marco_cell_prossimo_allarme')) |
            timestamp_custom('%Y-%m-%d %H:%M:00') }}
      - service: input_select.select_option
        target:
          entity_id: input_select.prima_sveglia
        data:
          option: Marco
  - conditions:
      - condition: template
        value_template: >-
          {{ as_timestamp(states('sensor.isa_pixel6_prossimo_allarme')) <
          as_timestamp(states('sensor.marco_cell_prossimo_allarme')) }}
    sequence:
      - service: input_datetime.set_datetime
        target:
          entity_id: input_datetime.orario_sveglia
        data:
          datetime: >-
            {{ as_timestamp(states('sensor.isa_pixel6_prossimo_allarme')) |
            timestamp_custom('%Y-%m-%d %H:%M:00') }}
      - service: input_select.select_option
        target:
          entity_id: input_select.prima_sveglia
        data:
          option: Isa
  - conditions:
      - condition: template
        value_template: >-
          {{ as_timestamp(states('sensor.marco_cell_prossimo_allarme')) ==
          as_timestamp(states('sensor.isa_pixel6_prossimo_allarme')) }}
    sequence:
      - service: input_datetime.set_datetime
        target:
          entity_id: input_datetime.orario_sveglia
        data:
          datetime: >-
            {{ as_timestamp(states('sensor.marco_cell_prossimo_allarme')) |
            timestamp_custom('%Y-%m-%d %H:%M:00') }}
      - service: input_select.select_option
        target:
          entity_id: input_select.prima_sveglia
        data:
          option: Marco

Please post the code you use and use a proper formatting for it. :slight_smile: