Home Assistant Only Evaluates Template at Reboot

Hello,

I am using a template to have a switch turn on, a certain amount of time after a google calendar event is fired and for some reason home assistant will only evlaute the template on reboot. does anyone know a way to get HA to evalute it every 5 minutes or so?

Thanks!

Here is the code for reference.

binary_sensor:

  • platform: template
    sensors:
    initial_cool_switch:
    friendly_name: “Initial Cool”
    value_template: >-
    {% set start_date = as_timestamp(states.calendar.airbnb_cal.attributes.start_time) %}
    {% set date = as_timestamp(now()) %}
    {%set diff = start_date -date %}
    {{ diff < -28800 }}

My guess would be that this is due to using now() in your template - see the docs for more info; essentially the value from now() is only determined when something else in the template updates state.

Maybe using sensor.time would be a better option for you?

It only evaluates your Template Binary Sensor at startup, and never again, because it’s unable to identify any entity within the template that it should monitor for changes.

Try this the following example. Home Assistant may have an easier time finding the entity it needs to monitor within the template (namely calendar.airbnb_cal).

binary_sensor:
  platform: template
    sensors:
      initial_cool_switch:
        friendly_name: 'Initial Cool'
        value_template: >-
          {% set start_date = as_timestamp(state_attr('calendar.airbnb_cal', 'start_time')) %}
          {{ start_date - now().timestamp() < -28800 }}

If it still fails to work add an explicit entity to monitor:

binary_sensor:
  platform: template
    sensors:
      initial_cool_switch:
        friendly_name: 'Initial Cool'
        entity_id: calendar.airbnb_cal
        value_template: >-
          {% set start_date = as_timestamp(state_attr('calendar.airbnb_cal', 'start_time')) %}
          {{ start_date - now().timestamp() < -28800 }}

If that still fails to work properly, it means calendar.airbnb_cal isn’t changing state often enough so add sensor.time as the entity to be monitored. sensor.time changes state every minute so the Template Binary Sensor will be updated every minute.

binary_sensor:
  platform: template
    sensors:
      initial_cool_switch:
        friendly_name: 'Initial Cool'
        entity_id: sensor.time
        value_template: >-
          {% set start_date = as_timestamp(state_attr('calendar.airbnb_cal', 'start_time')) %}
          {{ start_date - now().timestamp() < -28800 }}

Ensure you define sensor.time elsewhere in your configuration because it doesn’t magically exist by default.

1 Like

Ok Great! thanks for the info I really appreciate it! I’ll give all this a shot in the next few days and report back which works best.