Pls help: same values == False (or: create an alarm clock)

Hi everybody,

I am trying to create an alarm clock with this example.

All the code is below; what I am trying to do is

  • set (and forget) an alarm for weekdays and weekend days
  • trigger a scene at the time the alarm is set to

Whatever I do, it does not trigger the alarm. I tried the following code in templates:

{{ ((as_timestamp(now())|int)|timestamp_custom("%H:%M")) }}
{{ states('sensor.weekday_alarm_time') }}

{{ ((as_timestamp(now())|int)|timestamp_custom("%H:%M"))  == states("sensorweekday_alarm_time") }}

That would output

20:36
20:36

False

I changed the code from the link a bit by putting it all in a package (except for the scene; see below).

I don’t understand why 20:36 == 20:36 does not equal true, nor trigger the scene. The scene itself works, so I have not pasted the code here. When I manually call the scene, the light will act accordingly. Unlike the person in the post, I did use a scene instead of a script, because I could not get their script to work (even when copy/pasting the original code, the script would not show up with my other scripts, so I created a scene, which works).

Thanks in adance for your help :slight_smile: :pray:

# {{{ Sensoren
input_boolean:
  weekday_sunrise:
  weekend_sunrise:

input_number:
  weekday_alarm_hour:
    name: Hour
    icon: mdi:timer
    min: 0
    max: 23
    step: 1
  weekday_alarm_minutes:
    name: Minutes
    icon: mdi:timer
    min: 0
    max: 59
    step: 2
  weekend_alarm_hour:
    name: Hour
    icon: mdi:timer
    min: 0
    max: 23
    step: 1
  weekend_alarm_minutes:
    name: Minutes
    icon: mdi:timer
    min: 0
    max: 59
    step: 2

sensor:
  - platform: template
    sensors:
      weekday_alarm_time:
        friendly_name: 'Weckzeit Wochentag'
        value_template: '{{ "%02d:%02d" | format(states("input_number.weekday_alarm_hour") | int, states("input_number.weekday_alarm_minutes") | int) }}'
      weekend_alarm_time:
        friendly_name: 'Weckzeit Wochenende'
        value_template: '{{ "%02d:%02d" | format(states("input_number.weekend_alarm_hour") | int, states("input_number.weekend_alarm_minutes") | int) }}'
# }}}

automation:
# {{{ Wochentag
  id: "094934344"
  alias: 'Weekday Sunrise'
  initial_state: 'on'
  trigger:
    # - platform: time
    - platform: time_pattern
      minutes: '/1'
      seconds: 0
  condition:
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: state
      entity_id: input_boolean.weekday_sunrise
      state: 'on'
    - condition: template
      value_template: '{{ ((as_timestamp(now())|int)|timestamp_custom("%H:%M"))  == states("sensor.weekday_alarm_time") }}'
  action:
    - service: scene.turn_on
      entity_id: scene.wecker
# }}}
# {{{ Wochenende
  id: "9834934"
  alias: 'Weekend Sunrise'
  initial_state: 'on'
  trigger:
    # - platform: time
    - platform: time_pattern
      minutes: '/1'
      seconds: 0
  condition:
    - condition: time
      weekday:
        - sat
        - sun
    - condition: state
      entity_id: input_boolean.weekend_sunrise
      state: 'on'
    - condition: template
      value_template: '{{ ((as_timestamp(now())|int)|timestamp_custom("%H:%M"))  == states("sensor.weekend_alarm_time") }}'
  action:
    - service: scene.turn_on
      entity_id: scene.wecker
# }}}

In the first block of code I think you’re getting False because of a typo: states("sensorweekday_alarm_time") is missing a period.

I’d suggest using a trigger like this:

trigger:
  platform: template
  value_template: >
    {{ states('sensor.time') == states('sensor.weekday_alarm_time') }}

instead of a time_pattern trigger and a condition.

1 Like

Thank you! When I paste {{ states('sensor.time') == states('sensor.weekday_alarm_time') }} in templates, it will return True now. I have changed my trigger accordingly, but it does not work.

Do I need to specify that this has to be checked once every minute? Or did I miss something else?

If you use the trigger I posted it should trigger when the two sensors have the same value, and then it won’t trigger again until they first do not equal, and then become equal again, just as you’d want.

The way it works is, the template will be evaluated anytime either of the sensors changes. So, since sensor.time changes once a minute, it is effectively evaluated at least every minute.

If it’s not “working” then there is probably a problem somewhere else. Either the automation is not turned on, or a condition is preventing it from running when it should, or… Please post the automation as it stands now.

1 Like

This is what my automation currently looks like. I have kept the old code, but commented it out.

automation:
# {{{ Wochentag
  id: "094934344"
  alias: 'Weekday Sunrise'
  initial_state: 'on'
  # trigger:
    # - platform: time
    # - platform: time_pattern
    #   minutes: '/1'
    #   seconds: 0
  trigger:
    platform: template
    value_template: >
      {{ states('sensor.time') == states('sensor.weekday_alarm_time') }}
  condition:
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: state
      entity_id: input_boolean.weekday_sunrise
      state: 'on'
    # - condition: template
    #   value_template: '{{ ((as_timestamp(now())|int)|timestamp_custom("%H:%M"))  == states("sensor.weekday_alarm_time") }}'
  action:
    - service: scene.turn_on
      entity_id: scene.wecker
# }}}

scene.wecker will turn on light.schlafzimmer_bett_led, so this is how I tested this

  • set weekday alarm to 16:26 when it was around 16:24-16:25 (I have changed steps by 5 to steps by 2 in order to be able to test this more than every 5 minutes)
  • wait until it was actually 16:26
  • check whether scene.wecker and/or light.schlafzimmer_bett_led appeared in the log
  • manually checked whether light.schlafzimmer_bett_led was turned on (which it should be if triggered by this)

EDIT: oh, and input_boolean.weekday_sunrise is on.

You should also see that the automation triggered in the logbook. And if you look at the automation on the states page you should see its last_triggered attribute should have been updated.

If you indeed have sensor.time, and it’s updating correctly, and sensor.weekday_alarm_time has values in exactly the same format as sensor.time (no extra spaces, etc.), then I don’t see why this won’t trigger. Maybe try commenting out the condition section as another test???

1 Like

Thank you @pnbruckner

This was my fault. For some reason, I had two input_booleans for weekday and weekend, but there was only one single automation - when there should have been two. I don’t quite understand why, but while I did have an automation for the weekend, there was none for weekday to be found. So I changed to weekend automation to include every day of the week, set the timer, and it worked!

Either way, thank you so much for your help :slight_smile:

EDIT: the reason why I had only one automation working was that I probably never will see errors in yaml right away. I should have had it like

automation:
  - id: 
    (...)
 - id:
    (...)

Instead, I only had id: without a dash in front of it. So only the first automation was created. I changed this now and believe that it should work (still changing variable names etc. before final test)