Rewrite time condition in value_template

HI,

rewriting all conditions in the form of value_template, to easily check them in the dev-templates, what would be the best way to transform this:

    - condition: time
      before: '21:00:00'
      after: '08:30:00'

into this form:

condition: template
value_template: >
  {{ '08:30:00'< now().timestamp() < '21:00:00' }}

or

{{'08:30'< states('sensor.time') < '21:00'}}

You’ll have to convert them to seconds for this to work. String operations on numbers does not work the way you think it does.

{{ (8*60*60+30*60) < state_attr('sensor.time','timestamp') < 21*60*60 }}

thank you, I’ll do that, seems a bit hacky though. Isn’t there a better way? I merely tried the setup with sensor.time, because it passed the template checker and the shortest time display without further timestamps etc.

does seem to work though;-)

your template renders empty…
because this:
{{state_attr(‘sensor.time’,‘timestamp’)}} is None ?

Hmm, i thought that had a timestamp attribute. Use as_timestamp(states(‘sensor.time’))

As for your little example, let me show you why string representations of numbers do not work:

not yet…

btw using 09:33 makes it work correctly ? no matter which silly times I enter, it always is correct and evaluates the current time correctly too, unless of course I go over the 60 for the minutes…

this would be possible I think:

{{ (8*60*60+30*60) < ((now().hour)*60*60 + (now().minute)*60) < 21*60*60 }}

which would be the same as:

{{ (8*60 + 30) < ((now().hour)*60 + (now().minute)) < 21*60 }} or even

{{ 8*60 + 30 < now().hour*60 + now().minute < 21*60 }}

are you actually using sensor.time? Is sensor.time from the time component or is it something you made?

Also, if you are looking for the simplest approach, you could keep your units in hours:

{{ ( 8 + 30/60 ) < ( now().hour + now().minute/60 ) < ( 21 + 33/60 ) }}

or in decimal format:

{{ 8.5 < ( now().hour + now().minute/60 ) < 21.55 }}
1 Like

well, yes, sure. I use:

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'beat'

44

cool, getting better. As a matter of fact, I’d need the times to be between 07 am and 21 pm, so

{{ 7 < ( now().hour + now().minute/60 ) < 21 }}
nice! thanks