Stumped by Template Binary Sensor! Looking for guidance

I have many fairly complex binary sensors working without a hitch, but this on is really aggravating me, as I cannot make it work.
I am trying to create a binary sensor that will turn on if there is a Google Calendar event within 30 minutes.

The following template works perfectly in the Development Tools Template Editor:

`{{ (((as_timestamp(states.calendar.home.attributes.start_time) - as_timestamp(now())) / 60) | int) <= 30 and (((as_timestamp(states.calendar.home.attributes.start_time) - as_timestamp(now())) / 60) | int) > 0 }}`

But… If used in my binary sensor, the sensor never changes:

  - platform: template
      joint_calendar_event_within_30_minutes:
        friendly_name: 'Joint Calendar Event Within 30 Minutes'
        value_template: "{{ (((as_timestamp(states.calendar.home.attributes.start_time) - as_timestamp(now())) / 60) | int) <= 30 and (((as_timestamp(states.calendar.home.attributes.start_time) - as_timestamp(now())) / 60) | int) > 0 }}"

I have tried several variations such as:

      joint_calendar_event_within_30_minutes:
        friendly_name: 'Joint Calendar Event Within 30 Minutes'
        value_template: >-
          {{ (((as_timestamp(states.calendar.home.attributes.start_time) - as_timestamp(now())) / 60) | int) <= 30 and (((as_timestamp(states.calendar.home.attributes.start_time) - as_timestamp(now())) / 60) | int) > 0 }}

Thanks in advance.

you need to add to it a trigger entity.

 - platform: template
      joint_calendar_event_within_30_minutes:
        friendly_name: 'Joint Calendar Event Within 30 Minutes'
        entity_id: sensor.time
        value_template: "{{ (((as_timestamp(states.calendar.home.attributes.start_time) - as_timestamp(now())) / 60) | int) <= 30 and (((as_timestamp(states.calendar.home.attributes.start_time) - as_timestamp(now())) / 60) | int) > 0 }}"

The code that you say works perfectly in the template editor has 12 open parentheses and only 10 closed parentheses.

1 Like

No wait. I see. 12 of each. Disregard last post.

I love that somebody else (other than me) is anally retentive enough to count brackets up and down. :heart_eyes:

Though I use visual studio code (with rainbow brackets) as an editor, which helps tremendously

Edit: I still like your post, and it’s not your fault you can’t count :rofl:

1 Like

You are using calendar.home. Don’t you need to use calendar.home_everything to get the next event?

All the events are showing up properly. So I know all is well, except the binary_sensor is not triggering.
Again, it works as designed in the Development Tools Template Editor.
I have tried so many iterations of parenthesis, quotes, etc.
NO JOY!

This issue was resolved by using the time and date ( time_date ) sensor platform, and replacing now() with states.sensor.time.last_changed

As documented here.
I think you could left your template intact and just add entities: sensor.time for regular updates.

Yup, jwelter’s example resolves the issue using entity_id: sensor.time but there’s no reply from Zoriontsu confirming it.

@Zoriontsu
By adding entity_id: sensor.time to the Template Binary Sensor’s configuration, you’re instructing Home Assistant to evaluate the template whenever sensor.time changes state. That sensor changes state every minute.

In this example, I’ve added entity_id: sensor.time and revised the template to make it easier to understand its logic.

      joint_calendar_event_within_30_minutes:
        friendly_name: 'Joint Calendar Event Within 30 Minutes'
        entity_id: sensor.time
        value_template: >-
          {% set start = as_timestamp(states.calendar.home.attributes.start_time) %}
          {% diff = (start - now().timestamp()) / 60 %}
          {{ 0 < diff <= 30 }}

Naturally, you first have to define sensor.time in configuration.yaml before it can be used elsewhere.

You’ve arrived at an equivalent solution by replacing the now() function with sensor.time. Home Assistant detects the presence of sensor.time within the template and will evaluate the template every minute.