Fixed myself: Missing zero on sensor state (was: Problem with alarm - alarm time less 5 minutes triggers services, alarm time does not trigger service)

Firstly, many thanks for the help and advice the excellent people of this forum take their time to post. I’ve learnt so much from reading about the problems other people have had. I’ve tried to fix this one myself and tried reading around, but am still at a loss.

I’ve created an alarm clock with variable times by mainly following this method:

My exact entries are:

configuration.yaml:

input_number:
  alarm_clock_hour:
    initial: 6
    min: 0
    max: 23
    step: 1
    mode: box
  alarm_clock_minute:
    initial: 00
    min: 0
    max: 55
    step: 5
    mode: box

# Boolean
input_boolean:
  alarm_clock:
    name: Alarm
    initial: on
    icon: mdi:timer

# Alarm Clock
  - platform: time_date
    display_options:
      - 'time'
  - platform: template
    sensors:
      alarm_clock_hour:
        value_template: '{{ states.input_number.alarm_clock_hour.state | int }}'
      alarm_clock_minute:
        value_template: '{{ states.input_number.alarm_clock_minute.state | int }}'
      alarm_clock_time:
        value_template: >-
          {{ states.sensor.alarm_clock_hour.state }}:
          {%- if states.sensor.alarm_clock_minute.state|length == 1 -%}
            0
          {%- endif -%}
            {{ states.sensor.alarm_clock_minute.state }}
      alarm_clock_time_less_5:
        value_template: >-
          {%if (states("input_number.alarm_clock_minute") | int >= 5) %}
          {{ "%0.02d:%0.02d" | format(states("input_number.alarm_clock_hour") | int, states("input_number.alarm_clock_minute") | int -5) }}
          {% else %}
          {{ "%0.02d:%0.02d" | format(states("input_number.alarm_clock_hour") | int -1, states("input_number.alarm_clock_minute") | int +55) }}
          {% endif %}
      alarm_clock_time_less_30:
        value_template: >-
          {%if (states("input_number.alarm_clock_minute") | int >= 30) %}
          {{ "%0.02d:%0.02d" | format(states("input_number.alarm_clock_hour") | int, states("input_number.alarm_clock_minute") | int -30) }}
          {% else %}
          {{ "%0.02d:%0.02d" | format(states("input_number.alarm_clock_hour") | int -1, states("input_number.alarm_clock_minute") | int +30) }}
          {% endif %}

I won’t lie and pretend I fully follow the template sections of it, but I get the gist.

The entities all show fine as badges in the UI. My automatons are:

- alias: Alarm Clock
  trigger:
  - platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.alarm_clock_time.state }}'
  condition:
  - condition: state
    entity_id: input_boolean.alarm_clock
    state: 'on'
  action:
  - data:
      entity_id: media_player.landing
      source: Absolute Radio
    service: media_player.select_source
  - data:
      entity_id: media_player.kitchen
      volume_level: '0.25'
    service: media_player.volume_set
  - data:
      entity_id: media_player.landing
      volume_level: '0.16'
    service: media_player.volume_set
  - data:
      entity_id: media_player.landing
    service: media_player.media_play
  - service: light.turn_on
    data:
      entity_id: light.bedroom
      brightness: '0'
      color_temp: '454'
  - service: light.turn_on
    data:
      entity_id: light.bedroom
      brightness: '120'
      color_temp: '320'
      transition: '30'

# Alarm 5 Minutes Prior
- alias: Alarm Clock 5 Min Prior
  trigger:
  - platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.alarm_clock_time_less_5.state }}'
  condition:
  - condition: state
    entity_id: input_boolean.alarm_clock
    state: 'on'
  action:
  - service: light.turn_on
    data:
      entity_id: light.landing
      brightness: '200'
      color_temp: '320'
      transition: '420'

What I don’t get is that the Alarm Clock 5 Min Prior automation triggers (shows in the logbook and services are called) every time, but Alarm Clock automation only triggers half the time. The services are not called, and the automation doesn’t show as being triggered in the logbook. Equally, I cannot see any errors in the log.

UPDATE: I’ve identidied a pattern. The Alarm Clock only doesn’t trigger when we’re in the first 10 hours of the day, when the times start with a zero, ie 06:30, but works fine when I test later in the day 18:30, so the preceding zero on time causes issues with Alarm Clock but not Alarm Clock 5 Min Prior.

I’ll keep digging and post if I find the answer, but any guidance welcome.

And again, many thanks for to the excellent helpers on this site for all the time they put in.

Turns out that is my exact problem. The Alarm Clock time is missing a preceding zero:

Trying to fix it to no avail. Heading of main post updated. Any help would be welcomed.

Fixed it!

I changed this:

      alarm_clock_time:
        value_template: >-
          {{ states.sensor.alarm_clock_hour.state }}:
          {%- if states.sensor.alarm_clock_minute.state|length == 1 -%}
            0
          {%- endif -%}
            {{ states.sensor.alarm_clock_minute.state }}

To this:

        value_template: >-
          {{ "%0.02d" | format(states("input_number.alarm_clock_hour") | int ) }}:
          {%- if states.sensor.alarm_clock_minute.state|length == 1 -%}
            0
          {%- endif -%}
            {{ states.sensor.alarm_clock_minute.state }}

Thanks to anyone that read my stream of consciousness. Posting actually helped me figure it out, and I hope this helps anyone else in the same predicament.