How often will this template binary sensor refresh?

How often will this template binary sensor refresh?

wondering if I should add a trigger or if anything now() based will automatically refresh every x seconds? or once every day at midnight since it has now().day as the smallest interval?

Thank you!

- binary_sensor:     

  - name: "SEASON heating"
    unique_id: season_heating
    state: "{{ not ((5,1) < (now().month, now().day) < (10,1)) }}"

https://www.home-assistant.io/docs/configuration/templating/#time

Thank you, that’s what I thought. However the sensor history was supposed to be ‘on’ since October 1st but for some reason only started showing “on” when I restarted HA yesterday. Odd.

maybe just a database issue.
Pretty sure the actual binary sensor was “on” since oct 1st though.

Hmm. Maybe it is once a day if using .day. I know bdraco did a lot of optimisation of this.

You could usse a triggered template sensor. They are restored at start up (no actual need to trigger at start).

- trigger: time
  at: '0:00' # trigger once a day at midnight
  binary_sensor:     
  - name: "SEASON heating"
    unique_id: season_heating
    state: "{{ not ((5,1) < (now().month, now().day) < (10,1)) }}"

Though this too will be unknown until the first time 0:00 occurs. Then it should be restored after a restart at any time.

You can update it manually using Developer Tools → Actions, homeassistant.update_entity.

thanks! just weird that it would have never triggered in almost 2 months.
guess it doesn’t hurt to add the trigger.

I have a number of these “seasons”, will add the trigger for all
but I had to change the syntax to make it work, not sure if yours/mine is accurate or not?

- trigger:
    - trigger: time_pattern
      # This will update every night
      hours: "0"
      minutes: "0"

  binary_sensor:     
  - name: "SEASON heating"
    unique_id: season_heating
    state: "{{ not ((5,1) < (now().month, now().day) < (10,1)) }}"

  - name: "SEASON hq winter credits"
    unique_id: season_hq_winter_credits
    state: "{{ not ((4,1) < (now().month, now().day) < (11,15)) }}"

  - name: "SEASON christmas"
    unique_id: season_christmas
    state: "{{ not ((1,10) < (now().month, now().day) < (11,30)) }}"

  - name: "SEASON outdoor living"
    unique_id: season_outdoor_living
    state: "{{ ((4,15) < (now().month, now().day) < (11,1)) }}"

I based my syntax on this example:

But why does it expect strings and not numbers (code is under template.yaml)?

That’s just the HA helper extension. It is often wrong needs constant maintenance. You can open an issue (or fix it yourself) here: Issues · keesschollaart81/vscode-home-assistant · GitHub

Thank you,

So if I understand correctly, both triggers/syntax (yours/mine) should run fine, I can simply ignore the VScode helper messages. (or open issue/fix myself)

No. Mine is wrong. It is missing the first trigger.

Should be:

template:
  - trigger:
      - trigger: time
        at: 0:00

Thank you :+1:

If you’re bothered about updates (and no need to be, you’ve used up orders of magnitude more time on this topic than your HA will have done recalculating), you can install the time-date integration and do something like this with no need for triggers:

{{ not('05-01' < states('sensor.date')[5:] < '10-01') }}
1 Like

You’re likely excluding the device from recorder.

Thanks for the hint,
but it is part of my inclusions:

I will keep an eye on my revamped triggered templates and see how they behave.