Entity that derives its value from the time of day

How are Template Selects supposed to work? The documentation is pretty light on and I couldn’t find much else out there/here.

I see you define options, and state, and then along comes select_option to confuse me.

I had expected the Template Select was a read only entity whose state was one of several options. Pre-defining those possible options as options seems slightly unnecessary if the selected option just falls out of some equation/logic that defines state, but I can accept the existence of options. But what is select_option and how does it exist alongside state?

Would love to see an example of something in use if anyone has something they could share?

I’m talking philosophically/generally, but my immediate problem is that I just want an entity that derives its value from one of three or four options based on the time of day.

1 Like

Can you link to the documentation you are looking at. I can’t even find it.

There’s an example here:

This is the service you use to set the current selection from the available configured options.

I’m looking here and there is a loose example lower in the page.

In vscode, the editor supports the documentations assertion that each of state, select_option, and options is mandatory.

If I simply want to say: “if now() is between x and y then FOO, else between y and z then BAR, else ZOO” I would have thought I’d write that template formula for state.

To reiterate what I said initially, defining options then seems somewhat redundant to me… but I’ll comply, and it does make it clearer in any case. But I don’t need a service to call some script anywhere (via select_option) if I can just define the state.

In any case, I’m stabbing in the dark because it seems there are literally no examples of this being used that Google can find me!

(I had seen the example you linked but struggled to get anything from it.)

Answering OP's Questions about Select entities

The basic function of state-based Template selects seems to be to make it so you do not have to create separate input select helpers and automations. If you want a single functional unit, they can be used like mini-automations with State triggers.

Template selects are not read-only… they are like Input select helpers, but the options: can be either static lists (like an Input select) or a list derived from a template.

The state: template defines their current value. As far as I can tell, it has to render as one of the items in the options: list or it will return “unavailable”.

select_option: is the analog to the Action section of an automation. It defines what to do when a user or automation causes a new selection event.


That sounds like a job for a template sensor or a set of time of day sensors…

Here’s an example of a time of day template sensor:

template:
  - sensor:
      - name: Time of Day
        state: >
          {% set a = today_at("05:00")%}
          {% set b = today_at("07:00")%}
          {% set c = today_at("12:00")%}
          {% set d = today_at("18:00") %}
          {% set e = today_at("20:00")%}
          {% set f = today_at("01:00")%}
          {% set map = {
          a < now() <= b: 'Early Morning',
          b < now() <= c: 'Morning',
          c < now() <= d : 'Afternoon',
          d < now() <= e: 'Evening',
          e < now() < f + timedelta(days=1): 'Night',
          today_at() < now() <= f: 'Night',
          f < now() <= a: 'Late Night'
          } %}
          {{ map.get(True) }}

NOTE:

Since it wasn’t actually needed to solve OP’s problem, but presented information that many users have found useful, the majority of the content from this post has been moved to its own article in the forum’s Cookbook.

13 Likes

Great, thank you, I had seen TOD sensors but wasn’t aware you could set state like that. It looked like it was purely hard-coded times.

Those examples would need to be included in the poor documentation. With their help, I could create a select box auto-populated with labeled entities, allowing me to choose a light and control its properties using only one IKEA STYRBAR—which is amazing!