Need help to compare date time with a sensor containing date time in binary_sensor

Hi,

What I want is to compare today’s date & time with a future date & time (set alarm).
I want this as a binary_sensor to create a true/false.

I’m so stuck at this, and I assume this has something to do with string or integer…
I’ve got this

{{(as_timestamp(now()) | timestamp_custom("%A, %d %h %H:%M")) }}

Result: Friday, 06 Mar 21:12

Then I have this:

{% set timestamp = as_timestamp(states.sensor.arielam30_arielam30_alarm_sensor.attributes.state) | timestamp_custom("%A, %d %h %H:%M") %} {{ timestamp.lstrip("0") }}

Result: Monday, 09 Mar 09:05

Still, have not been able to resolve this…

Here is the sensor

@petro and @tom_l are you going to rescue me? :pray:

compare the unix time values, which are integers.

@nickrout
Date & Time are my nightmare… Could you point me more or less to the solution, please?

Why are you using the attribute state of the sensor and not the state itself? From the screenshot you show they are both the same.

What @nickrout probably means is

{{ as_timestamp(now()) == as_timestamp(states('sensor.arielam30_arielam30_alarm_sensor')) }}

This will return True when the current date& time is equal to the date&time of the sensor and False otherwise.

1 Like

Yes that is what I mean. It will only be true momentarily true as now() (contrary to what I said earlier) has 5 decimal places. Probably better set it to an integer first.

1 Like

So it will be true only for a fraction of time?
My goal was for a minute (not using seconds). That is where I started messing with those strings (and as I stated, I assumed I needed this as integer).

Again, unfortunately time & date are still my nightmare…

Divide each number by 60 and convert to integer (I think)

It probably helps to know that these numbers are just seconds. Another way would be ti subtract them and see if the result is less than 30.

@nickrout

Like this ?

{{ as_timestamp(now()) / 60 | int ==
as_timestamp(states('sensor.arielam30_arielam30_alarm_sensor')) /60 | int }}

May I ask why you need it to be on for 1 minute?

Sure you can ask :smiley: No particular reason, but it should be more than a 1/100 second, as I want to trigger automations and scripts based on this binary_sensor. Probably a second is more than enough - never done any testing if there are any delays from a binary_sensor switches between true/false to trigger an automation.

I make a lot of stuff regarding templates, sensors and stuff - though as stated - I hate time & date.

{{ as_timestamp(now()) / 60 | int ==
as_timestamp(states('sensor.arielam30_arielam30_alarm_sensor')) /60 | int }}

did not make it flip between false & true…

As far as I know, timestamp is just the number of seconds passed since 1. January 1970. So the binary_sensor should be on for one second, I think this should be enough to trigger automations.

here is some additional information…

Have you checked that “as_timestamp” for your sensor actually returns a valid unix timestamp in the template editor?

I think it should but since you having troubles it is easy enough to check.

1 Like
{{ as_timestamp(now()) | int }}

returns 1583534241

At the moment,

{{ as_timestamp(states('sensor.arielam30_arielam30_alarm_sensor')) | int }}

returns 1583741100

So yes, they do… but the sensor never changes from false to true.

        value_template: >
          {{ as_timestamp(now()) | int == as_timestamp(states('sensor.arielam30_arielam30_alarm_sensor')) | int }}

I’m not sure if i’m losing something but those aren’t “equal” from what you posted.

try using a < or > just to make sure you are on the right track. one or the other must be true unless they are truly equal for 1 second.

the other thing to remember is that the template editor won’t update in real time. you have to modify the contents of that page before it will update the results. So you would literally have to make a change to the text every second to see it switch from false to true to false again.

1 Like

Don’t know if it’ll help you but I have a sensor that returns true a couple of minutes before the set time

{{ ((as_timestamp(calendar_tvattid) - as_timestamp(now())) / 86400) }}
right now returns calendar laundry time event in 2.5 days

<= 0.004 makes the sensor true a couple minutes before the calendar event

tvatta_countdown_timer_trigger:
  entity_id: sensor.time
  value_template: >-
    {% set calendar_tvattid = state_attr('calendar.tvatta', 'start_time') %}
    {% if ((as_timestamp(calendar_tvattid) - as_timestamp(now())) / 86400) <= 0.004 %}
    true 
    {% else %}
    false
    {% endif %}

then I automate an input boolean to show a timer

1 Like

Hi @finity

Actually, I think I’ve found the reason for not getting the == (or why it is incredible short state).
As I have programmed alarms for every day in the morning, as soon as the ((now)) == alarm-sensor, the alarm-sensor updates to the next alarm time, and ((now)) is again < sensor-alarm.

I did turn off all alarms, created a one-time alarm on the android device, using this:


{{ as_timestamp(now()) | int > as_timestamp(states('sensor.arielam30_arielam30_alarm_sensor')) | int }}

Guess what, as soon as the alarm started, it changed from False to True, meaning the rule works but I assume as the alarm-sensor as soon it is triggered by the ==, it resets to next alarm. Perhaps HA doesn’t even get tu update the binary_sensor?

So, for what Mattias wrote in his sensor, it would be smart to actually trigger the binary_sensor a bit before they actually are ==. See my point?

Maybe it would be helpful for you to post the entire code for your binary_sensor instead of the just the template. You might be doing something else in that code that is preventing the sensor from updating…

I just did a test to turn on an input boolean based on now being equal to an input datetime making the seconds required to be equal and it worked for me.

@Mattias_Persson !!! You’re the man!

Here is the working solution, it turn on the binary_sensor 3 minutes before it actually should trigger. One of the issues earlier was exactly the part that as there is a new alarm coming up, somehow the sensor did never flip from ‘false’ to ‘true’.

I can edit the time before by changing the <= 0.002 % which I changed to 0.002 % = approx 3 minutes. Now I can trigger my morning routine where I actually do all the stuff that doesn’t work with Google Home in countries outside US; wish me good morning, turn on lights, open bliders, give me the weather report and turn on the radio on the Google Home.

        entity_id: sensor.time
        value_template: >-
          {% set trigger_alarm = state_attr('sensor.arielam30_arielam30_alarm_sensor', 'state') %}
          {% if ((as_timestamp(trigger_alarm) - as_timestamp(now())) / 86400) <= 0.002 %}
          true 
          {% else %}
          false
          {% endif %}

Thank you everybody helping me resolve this :slight_smile: :pray:

In mine it gives a figure with 5 or 6 decimal places eg 1583547700.674958