[SOLVED] Help needed with a kind of "timer"

Been searching around some, but haven’t really figured out how to write it…

I have two input_numbers and one input_boolean.

The first is input_number is “start hour” and the second is “stop hour”, and the input_boolean is just to activate or deactivate the “timer”.

How do I make that automation, so that it starts and stops at the chosen hours and when stopped, resets the input_boolean to inactive state.

I guess it’s fairly simple, but I cant wrap my head around it!

Thankful for any help :slight_smile:

I do it with two input_datetimes (not input_numbers) to control a binary sensor. The use of time only input_datetimes complicates things as they need today’s date added to be compared with now():

- platform: template
  sensors:
    lounge_ac_am_automation_time_active:
      friendly_name: "Lounge AM Automation Time Active"
      value_template: >-
        {% set update = states('sensor.time') %}
        {% set d = now().strftime("%Y-%m-%d ") %}
        {% set t = now().timestamp() %}
        {% set am_start = strptime(d + states('input_datetime.lounge_ac_am_on_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
        {% set am_end = strptime(d + states('input_datetime.lounge_ac_am_off_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
        {{ am_start <= t <= am_end }}

If you only want to match the hour with input_numbers it is a lot simpler:

binary sensor

- platform: template
  sensors:
    kind_of_timer:
      friendly_name: "Kind of Timer"
      value_template: >-
        {% set update = states('sensor.time') %}
        {% set t = now().hour %}
        {% set start = states('input_number_start_hour')|int %}
        {% set stop = states('input_number_stop_hour')|int %}
        {{ start <= t <= stop }}

You can use the state of that binary sensor (trigger) in an automation to switch your input_boolean (action).

The template updates every minute curtsey of including {% set update = states('sensor.time') %}.

Hi again and thanks for trying to help me out!

I took your example and modified it a bit, really just replaced some names…

But the sensor never changed value from False to True, any ideas why?
I set the start time to 17 and end time to 18, and then waited until the clock showed 17, I was hoping the sensor would show “True” then, but it didn’t.

What am I missing here?

The sensor…

- platform: template
  sensors:
    varmeflakt_timer:
      friendly_name: "Timer"
      value_template: >-
        {% set update = states('sensor.time') %}
        {% set t = now().hour %}
        {% set start = states('input_number_varmeflakt_start')|int %}
        {% set stop = states('input_number_varmeflakt_stopp')|int %}
        {{ start <= t <= stop }}

The inputs…

varmeflakt_start:
  name: Starttid
  icon: mdi:clock-time-two-outline
  initial: 0
  min: 0
  max: 23
  step: 1

varmeflakt_stopp:
  name: Sluttid
  icon: mdi:clock-time-five-outline
  initial: 0
  min: 0
  max: 23
  step: 1

…and last but not least, the automations for turning it on and off.

- alias: Starta värmefläkt
  trigger:
  - platform: state
    entity_id: sensor.varmeflakt_timer
    from: 'False'
    to: 'True'
  action:
    - service: switch.turn_on
      entity_id: switch.shelly_varmeflakt
- alias: Stäng av värmefläkt
  trigger:
  - platform: state
    entity_id: sensor.varmeflakt_timer
    from: 'True'
    to: 'False'
  action:
    - service: switch.turn_off
      entity_id: switch.shelly_varmeflakt

What version of home assistant are you on?

Latest, 0.115.2

Do you have a sensor.time?

Also that was meant to be a binary_sensor, not a sensor.

If you do change it use states 'on' and 'off', not True and False in your automation.

Ahh, okay, I’ve changed the sensor to a binary and changed the automations aswell!

I do have a sensor.time!

This trigger will be binary_sensor.varm... now.

- alias: Starta värmefläkt
  trigger:
  - platform: state
    entity_id: sensor.varmeflakt_timer

That’s weird…that didn’t work :frowning:

The binary sensor didn’t change from off to on based on the values from the input_numbers.

What does this give you in the developer tools / template editor:

start: {{ states('input_number_varmeflakt_start') }}
stop: {{ states('input_number_varmeflakt_stopp') }}
now hour: {{ now().hour }}
time: {{ states('sensor.time') }}

It gives:

start: unknown
stop: unknown
now hour: 19
time: 19:10

But if I change from:

start: {{ states('input_number_varmeflakt_start') }}
stop: {{ states('input_number_varmeflakt_stopp') }}
now hour: {{ now().hour }}
time: {{ states('sensor.time') }}

to:

start: {{ states('input_number.varmeflakt_start') }}
stop: {{ states('input_number.varmeflakt_stopp') }}
now hour: {{ now().hour }}
time: {{ states('sensor.time') }}

I get the following:

start: 0.0
stop: 0.0
now hour: 19
time: 19:10

Which seems more accurate!

But I don’t know if the decimals will mess things up?

Doh. Yeah those should definitely be periods not underscores. Well done.

No the decimal points wont matter for less than / greater than comparisons. They just have to be numbers.

Why are your input numbers both zero though?

Both my sliders where at zero when testing, now, when set, they show up correct numbers!

start: 20.0
stop: 21.0
now hour: 19
time: 19:19

So now I’ll just have to wait and see how it plays out :slight_smile:

1 Like

Okay, so it kinda works, the sensor gets set to “on” at specified hour, but for some reason it doesn’t get set to “off” at the next specified hour…any ideas why?

Can you post the sensor config and automation(s) involved?

See below!

- platform: template
  sensors:
    varmeflakt_timer:
      friendly_name: "Timer"
      value_template: >-
        {% set update = states('sensor.time') %}
        {% set t = now().hour %}
        {% set start = states('input_number.varmeflakt_start')|int %}
        {% set stop = states('input_number.varmeflakt_stopp')|int %}
        {{ start <= t <= stop }}
- alias: Starta värmefläkt
  trigger:
  - platform: state
    entity_id: binary_sensor.varmeflakt_timer
    from: 'off'
    to: 'on'
  action:
    - service: switch.turn_on
      entity_id: switch.shelly_varmeflakt
- alias: Stäng av värmefläkt
  trigger:
  - platform: state
    entity_id: binary_sensor.varmeflakt_timer
    from: 'on'
    to: 'off'
  action:
    - service: switch.turn_off
      entity_id: switch.shelly_varmeflakt

Might it be something with this line?

{{ start <= t <= stop }}

Automations work, I’ve tried them both setting the state manually!

No, that looks valid.

Try:

{% set t = now().hour|int %}

Unfortunantly that didn’t help :frowning:

I know what it is. Try this:

{{ start <= t < stop }}

t equal to your stop hour was keeping the sensor on.