Creating a alarm clock

Hi Bob,
Yes I know turning on the automation doesn’t trigger it :).
What I’m trying to accomplish is that the first automation turns on the heating.
Then the second automation should trigger after the starting time + duration minutes occurs, and should turn off the heating.

I copied it from this thread (hmmm, maybe I should post my question there then :))

Hi @PuckStar,

Unless I did not understand your request, if you simply want to turn your heating off x minutes after it’s been turned on, why not add a delay to your first automation then turn the heating off?

  action:
    service: scene.turn_on
    entity_id: scene.Ochtend_Prog_uit
    delay: 00:10:00
    service: scene.turn_off
    entity_id: scene.Ochtend_Prog_uit

I want it to be variable and to be set through the UI. So 1 day I may want it to stay on 15minutes, the other day 60minutes.
Can I use input_numbers as the delay variable?

in that case I’d personally stick with 2 automations indeed.
The 2nd should run periodically (e.g. every 1 min) and have a condition to check against.

I’d say make sure you’re on you can use release 0.55 or later and use input_datetime in your condition. I would not use the == operator but the >= operator to make sure your automation still runs in the event where there is a delay.
I’d use something like this:

     - condition: template
        value_template: '{{ (as_timestamp(states.automation.ochtend_verwarming.attributes.last_triggered + input_datetime.time_to_heat) -  as_timestamp(states.automation.ochtend_verwarming.attributes.last_triggered) > 0}}'

I’ve not tested the above code so can’t guarantee it’s error-free but should give you a starting point hopefully.

OR you could use an input_select with a list of times and use this instead of input_datetime.time_to_heat if your list of possible times is not infinite

hello guys i use the alarm clock.
when i restart my pi i lost all values of my Sliders.
how can i do?

Have a look here…

Something goes wrong with the statement half hour before but I’m not sure what I’m doing wrong.
Any thoughts?


- alias: 'Half uur voor de wekker CV aan'
  initial_state: 'on'
  hide_entity: True
  trigger:
    platform: template
    value_template: '{{ ((now().strftime("%s") | int + 1800 ) | timestamp_custom("%H:%M")) == states.sensor.alarm_clock_time_long.state }}'
  condition:
    condition: or
    conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: input_boolean.alarmstatus
          state: 'on'
        - condition: state
          entity_id: input_boolean.alarmweekday
          state: 'on'
      - condition: and
        conditions:
        - condition: state
          entity_id: input_boolean.alarmstatus
          state: 'on'
        - condition: state
          entity_id: input_boolean.alarmweekday
          state: 'off'
  action:
    -   service: scene.turn_on
        entity_id: scene.toon_comfort

@keithh666 i tried it but not works.
I would like to use mqtt.

I use this:

alias: ‘test’
trigger:
platform: template
value_template: '{{ states.sensor.time.state == states.sensor.alarm_time.state }}
action:
service: notify.notify
data_template:
message: ‘teste’

How can transform that ,on mqtt?
I dont want lost my sliders values when i restart.

@masterkenobi can u help too?

I think if you were to create an MQTT switch that had as payload your slider state and set the state and command topics to the same, then set the retain switch to true, that would force the MQTT broker to resend the slider state back after a reset. You would have to setup an automation to send the state when it’s state changed.

@keithh666 i am using this alarm https://github.com/master-kenobi/ha-alarmclock
Can put here that changes ? I dont have skills to create that you told me. If you can help was great.

An alarm clock that plays a radio station in the morning?

What would be the best code

can you post your code, thanks

from automation.yaml
Publish the alarm time hours when it has changed.

 - alias: MQTT TimeSlider Hours
   trigger:
     - platform: state
       entity_id: input_number.alarmtimehrs
   action:
     - service: mqtt.publish
       data_template:
         topic: "cmnd/MQTTSliderSaveHrs"
         retain: true
         payload_template: '{{states.input_number.alarmtime.state | int}}' <------ this may need to be changed or you may be able to use the new input.date_time - doing it this way you would need to do the same for the minutes

The following sets the Alarm time hours from the MQTT that is received when HA resets…

 - alias: 'Set Alarmtime Hours from MQTT'
   trigger:
     - platform: mqtt
       topic: cmnd/MQTTSliderSaveHrs
   action:
     - service: input_number.set_value
       data_template:
         entity_id: input_number.alarmtimehrs
         value: '{{ trigger.payload}}'

You will have to modify this to your own use case.

I don’t know why the other way (that I showed above) would not work except that you would need to change every use of input_slider to input_number in the yaml and the python code.

input_slider has changed to input_number in version 0.55. I have updated the code in my github to reflect that.

Don’t worry. The value will not lost after restart.

I am using this: https://www.youtube.com/watch?v=qJ8ZoJuuZVA
Works great :slight_smile:

@masterkenobi i follow your alarm on github.
I use hass.io but when i restart home assistant i lost the values hour and minutos. The values not stored. I think the solution is what @keithh666 said.

@keithh666 i tried your example but not works.
The values of hours and minutes are reseted when i restart home assistant :frowning:

Sorry. You need to remove the initial from the input_boolean and input_number. I have updated the codes. It should remember the value now.

You can use something like this Chromecast Radio with station and player selection and create an automation for when the alarm clock goes start playing one of the radios

@masterkenobi thanks mate.
now it Works, but with one BIG problem.
if you delete home-assistant.log and home-assistant_v2.db all the sliders values are deleted and not stored.
@dale3h had the big solution for this, using MQTT.

the config is:

inside of customize.yaml

automation.persistence_publish:
  friendly_name: "Persistence Publish"
automation.persistence_subscribe_binary:
  friendly_name: "Persistence Subscribe (Binary)"
automation.persistence_subscribe_select_option:
  friendly_name: "Persistence Subscribe (Select Option)"
automation.persistence_subscribe_select_value:
  friendly_name: "Persistence from MQTT (Select Value)"
#inputes
input_number.alarmhour:
  persistent: true
input_number.alarmminutes:
  persistent: true

inside of automations.yaml

- alias: persistence_publish
  initial_state: 'on'
  hide_entity: true
  trigger:
    - platform: event
      event_type: state_changed
  condition:
    - condition: template
      value_template: "{{ trigger.event.data is not none }}"
    - condition: template
      value_template: "{{ trigger.event.data.new_state is not none }}"
    - condition: template
      value_template: "{{ trigger.event.data.new_state.attributes is not none }}"
    - condition: template
      value_template: "{{ trigger.event.data.new_state.attributes.persistent|default(false, true) }}"
    - condition: template
      value_template: "{{ trigger.event.data.old_state is not none }}"
    - condition: template
      value_template: "{{ trigger.event.data.new_state.state != trigger.event.data.old_state.state }}"
    - condition: or
      conditions:
        - condition: template
          value_template: "{{ trigger.event.data.entity_id.split('.')[0] == 'automation' }}"
        - condition: template
          value_template: "{{ trigger.event.data.entity_id.split('.')[0] == 'input_boolean' }}"
        - condition: template
          value_template: "{{ trigger.event.data.entity_id.split('.')[0] == 'input_select' }}"
        - condition: template
          value_template: "{{ trigger.event.data.entity_id.split('.')[0] == 'input_number' }}"
        - condition: template
          value_template: "{{ trigger.event.data.entity_id.split('.')[0] == 'light' }}"
        - condition: template
          value_template: "{{ trigger.event.data.entity_id.split('.')[0] == 'switch' }}"
  action:
    - service: mqtt.publish
      data_template:
        topic: "home-assistant/{{ trigger.event.data.entity_id|replace('.', '/') }}"
        payload: "{{ trigger.event.data.new_state.state }}"
        retain: true

## Persistent Subscriptions
- alias: persistence_subscribe_binary
  initial_state: 'on'
  hide_entity: true
  trigger:
    - platform: mqtt
      topic: home-assistant/automation/+
    - platform: mqtt
      topic: home-assistant/input_boolean/+
    - platform: mqtt
      topic: home-assistant/light/+
    - platform: mqtt
      topic: home-assistant/switch/+
  condition: &subscribe_condition
    - condition: template
      value_template: "{{ ((states[trigger.topic.split('/')[1]][trigger.topic.split('/')[2]]|default).attributes|default).persistent|default(false, true) }}"
    - condition: template
      value_template: "{{ not is_state(trigger.topic|replace('home-assistant/', '')|replace('/', '.'), trigger.payload) }}"
  action: &subscribe_action
    - service_template: "{{ trigger.topic.split('/')[1] }}.turn_{{ trigger.payload|lower }}"
      data_template:
        entity_id: "{{ trigger.topic|replace('home-assistant/', '')|replace('/', '.') }}"

##
- alias: persistence_subscribe_select_option
  initial_state: 'on'
  hide_entity: true
  trigger:
    - platform: mqtt
      topic: home-assistant/input_select/+
  condition: *subscribe_condition
  action:
    - service_template: "{{ trigger.topic.split('/')[1] }}.select_option"
      data_template:
        entity_id: "{{ trigger.topic|replace('home-assistant/', '')|replace('/', '.') }}"
        option: "{{ trigger.payload }}"

##
- alias: persistence_subscribe_select_value
  initial_state: 'on'
  hide_entity: true
  trigger:
    - platform: mqtt
      topic: home-assistant/input_number/+
  condition: *subscribe_condition
  action:
    - service_template: "{{ trigger.topic.split('/')[1] }}.set_value"
      data_template:
        entity_id: "{{ trigger.topic|replace('home-assistant/', '')|replace('/', '.') }}"
        value: "{{ trigger.payload }}"

now you can update your GitHub :wink: it Works like a charm.
for all the peope that want to use this is a best solution, because all data is saved via MQTT

*** this method Works with: automation, input_boolean, input_select, input_number, light e switch***