Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 96121

Binary_sensor 'current time within interval': can it be better?

$
0
0

@AhmadK wrote:

Hi All,

For my heating automations I needed a binary sensor that is on when current time is between times set in UI via input_datetimes from and to.
I’m using it to trigger an automation that sets night preset on my thermostat and reverts it back to normal next morning.
Initially I had this:

binary_sensor:
  - platform: template
    sensors:
      central_heating_night_time:
        value_template: >
          {% set from = strptime(states.input_datetime.central_heating_preset_night_from.state, "%H:%M:%S") %}
          {% set to = strptime(states.input_datetime.central_heating_preset_night_to.state, "%H:%M:%S") %}
          {% set cur = strptime(states.sensor.time.state, "%H:%M") %}
          {{ (cur < to or from <= cur) if from > to else from <= cur < to }}

but I didn’t like a lot of conversions and after a few transformations now it’s:

central_heating_night_time:
  # all variables below (excluding hour & minute) hold UTC times converted to timestamps (int)
  # as internally HA times are in UTC
  # nothe that you have to have sensor.time_utc configured
  value_template: >
    {% set from = states.input_datetime.central_heating_preset_night_from.attributes.timestamp %}
    {% set to = states.input_datetime.central_heating_preset_night_to.attributes.timestamp %}
    {% set hour, minute = states.sensor.time_utc.state.split(':') | map('int') %}
    {% set cur = hour*3600 + minute*60 %}
    {{ (cur < to or from <= cur) if from > to else from <= cur < to }}

It does work.
Any thought on what can be improved as it’s basically a simple task?

p.s I spent some time reading docs and this topic and even have a docs PR as it’s not as simple as it looks :wink:

Posts: 3

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 96121

Trending Articles