Help with time comparaisons in template


I am trying to set up an automation to control my pellet stove based on time of day and temperature.
However, I need to be able to change the “Time of day” in question.
Depending on what time I am working (getting up) I want the stove to turn on. So I am using an input_datetime that I can control from the frontend based on my needs.
I have added the following as an automation, but am having two difficulties:

#TurnStoveOn under 21.5 Morning and evening
- alias: Stove Auto On Timer and Temp 
    - platform: numeric_state
      entity_id: sensor.ht_living_temp
      below: 22.5
    condition: and
    - condition: template
      value_template: "{{ as_timestamp(state('sensor.time')) > (state_attr('input_datetime.stove_morning_start', 'timestamp') | int | timestamp_custom('%H:%M', True)) }}"
    - condition: template
      value_template: "{{ as_timestamp(state('sensor.time')) < (state_attr('input_datetime.stove_morning_finish', 'timestamp') | int | timestamp_custom('%H:%M', True)) }}"
  - service: switch.turn_on
    - switch.pellet_stove
  1. It seems to trigger regardless of the time set in input_datetime.stove_morning_finish or input_datetime.stove_morning_start
  2. I do not know how to get HASS.IO to show the values that it is using for the comparaison.

Is there something wrong with my code?
Does anyone have any suggestions as to how I can fix this?

The values for input_datetime.stove_morning_start and input_datetime.stove_morning_finish, as well as the value returned by the state of sensor.time can be seen in the screenshots below. (Correction - I’m a new user and cannot post more than one image.)

The input_datetime.stove_morning_finish has a state of 13:02:00 (timestamp attribute is 46920 - no date data here)
The input_datetime.stove_morning_start has a state of 04:40:00 (timestamp attribute is 16800)


The logic (as far as I can see) says: Once triggered (temp), if the state.time value is greater than 04:40 and is less than 13:02, turn the stove on. However, manually triggering at at 13:58, it is still turning the stove on!

Do I need to convert to timestamp of sensor.time (or (now) with null date, to perform numeric comparaisons? If so, how to do?

Any help would be greatly appreciated.

Generally you are correct but there is a simpler way of doing this.
All entity states are presented as text strings and the time string is presented as a 24 hour clock
So just compare the strings instead. We just need to lose the seconds from the input_datetime hence the [0:5], which means take the first 5 characters (starts at 0 at takes upto 5, but not including 5 - so ‘09:00’ rather than ‘09:00:00’


      - condition: template
        value_template: "{{ states('input_datetime.stove_morning_start') [0:5] <= states('sensor.time') < states('input_datetime.stove_morning_finish') [0:5] }}"

Note: please examine the above, it has double quotes on the OUTSIDE and ONLY single quotes within the code to be evaluated, this is required by the interpreter AND make it easier for humans to read. You may need to manually enter the above into the file (i.e. avoiding the automation editor).

Manually triggering any automation, BYPASSES any trigger condition (obviously) BUT also BYPASSES any conditions too.

You WILL need something to turn the stove off (I assume once it passes a set temperature)

Thank you.
A lot cleaner and makes sense.

What I didn’t know (could have been testing for hours) is that manually triggering an automation bypasses the conditions. That it bypasses the trigger is 100% understandable, but the conditions? Seems strange, but logical also! Let me change my values and test “live” without manual triggering.

I have other automations to turn the stove on outside of the defined times when it gets too cold (don’t want the house to get below 12 C) and also to turn it off when it gets too hot. Both of these should take place at any time, so are not concerned by my immediate problem.

Ooops, I made a typo, the above is corrected but replace the comparison ‘<-’ with ‘<=’

Couldn’t understand what I had done. Copied the code and pasted it to test in templates in the dev options. Saw and corrected the error, but when I want to type a reply indicating the typo - the code was correct!
This code checks out fine and does what I need.

Now onto creating the input mechanisms on my HADashboard and also Lovelace to input the start and end times easily from a selection of values…

Thank you so much for your help.

Haha, I’d like to say I was just testing if you’d spot the error but it was my fat fingers on a little phone :rofl:

Regarding interface I’d just create a card with temp set points and times on it (the input_datetimes and input_numbers) and call the card “Stove Control” or some such (simple is better in my book)

If you consider the thread closed it’s usual to mark off a post as your solution, it helps others looking for similar AND those members who are looking to help.

Finally please read the sticky at the top of the forum, it will pay dividends