Trigger time/template won't work..sensor.time needed?


using the template trigger

      value_template: >
        {{now().time().strftime('%H:%M') == states('sensor.master_bedroom_schedule_day')}}

doesn’t seem to work, while the template itself is just fine, and says True or False.

though there is no error in the log complaining about not being able to evaluate the template because of wrong or missing entity_id’s, I tried to add the entity_id: sensor.time, but that isn’t allowed in the trigger section.

So rebuilding this to a binary sensor to use for the trigger state:

        friendly_name: 'Now Master bedroom schedule day'
#        entity_id: sensor.time
        value_template: >
          {{now().time().strftime('%H:%M') == states('sensor.master_bedroom_schedule_day')}}

the binary sensor never goes to ‘on’…

        friendly_name: 'Now Master bedroom schedule day'
        entity_id: sensor.time
        value_template: >
          {{is_state('sensor.master_bedroom_schedule_day', now().time().strftime('%H:%M')) }}

is essentially the same. And yet that works and changes the binary to on and off as desired. Wouldn’t the evaluation of the is_state(‘sensor.master_bedroom_schedule_day’) part suffice? Why does this need the sensor.time?


Rendering templates with time ( now() ) is dangerous as trigger templates only update based on entity state changes.

I would make it a condition and use a periodic time_pattern trigger.

Fully aware of the above, yes.
i’m using the sensor.time as entity_id (in the binary_sensor) so it works for now. but would have thought that only to be necessary when testing for now() only, not when comparing that to another state… Is my first template to encounter this (guess there’s always a first) :wink:

this is the automation (left all the tested options for reference )

  - alias: 'Weekend Switch Off Masterbedroom motion sensor'
    id: 'Weekend Switch Off Masterbedroom motion sensor'
#    initial_state: 'on'
      platform: state
      entity_id: binary_sensor.now_master_bedroom_schedule_day
      to: 'on'
#      at: '07:00:00' # the time set in the Hue app, which has only day setting, and doesn't recognize weekend
#      platform: template
#      value_template: >
#        {{now().time().strftime('%H:%M') == states('sensor.master_bedroom_schedule_day')}}
      - condition: template
        value_template: >
          {{ is_state ('sensor.activity_selection', 'Slapen')}}
      - condition: template
        value_template: >
          {{is_state('binary_sensor.workday_sensor','off') or 
      - service: switch.turn_off
        entity_id: switch.master_bedroom_motion_sensor_switch

as you can see I started with time: at 07:00:00 and that only triggers once a day, now I have it triggering every second…