How to convert MQTT message to an input time (for a wake up alarm synced with phone)

Hi guys! I want to get an automation that wakes me up gently when my alarm is set. I got this working with the tons of examples available on the web, but I want a slightly different approach. I want to set the alarm time on my phone and not on the web interface.

I can currently send my set alarm clock time through tasker (with the AutoAlarm and the MQTT client plugins) in any format. Currently it just sends “HH:MM” (so 07:40, or the likes). How do I convert this incoming packet to an input time?
For my sensor I currently have the following (in sensors.yaml), which is displayed on the dashboard.

sensor:
- platform: mqtt
  state_topic: "tasker/phone_alarm_time"

The example wake up light (in configurations.yaml) used to be set from the dashboard with the following:

input_datetime:
  wakeup_time:
    name: "Start lights at"
    has_time: true
    has_date: false
    initial: "07:20"

And is used in the trigger (in automations.yaml)

# Wake up light
- alias: "Wake me up with bedroom light transition for weekdays"
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.wakeup_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"

So my question is: how to get the incoming MQTT message in my code?

ps. I am still planning on using the on/off boolean to disable the alarm if the MQTT sends “no_alarm” when there will be no alarm the following day.

This is my current layout:
wake_up_light_interface

I also use Tasker to send my alarmtime and use this to import into Hass:

- alias: Set alarmtime_hour
  trigger:
    platform: mqtt
    topic: "setHour"
  action:
     service: input_number.set_value
     data_template:
      entity_id: input_number.alarm_hour
      value: "{{ trigger.payload}}"
      
- alias: Set alarmtime_minutes
  trigger:
    platform: mqtt
    topic: "setMinutes"
  action:
     service: input_number.set_value
     data_template:
      entity_id: input_number.alarm_minutes
      value: '{{ trigger.payload}}'   
1 Like

I am currently not at home, but will test it when I get back. This looks like the easiest solution I have ever seen. Thanks for your input. Do the values stay stored when hassio reboots? I heard someone say you must use input_datetime to keep your values after a restart, do you know how that works?

And how do you store the stored HH and MM values so it can be used in the automation? The trigger I posted would not work anymore, since it is not in input_datetime format.

states.input_datetime.wakeup_time.attributes.timestamp

So this should work?

- alias: Set alarmtime_from_tasker
  trigger:
    platform: mqtt
    topic: "tasker/phone_alarm_time"
  action:
    service: input_date.set_value
    entity_id: input_datetime.wakeup_time
    data:
      time: '{{ trigger.payload}}'

Yes they do. I reboot every weekday at 5am to keep my instance running smooth and (almost) never miss an alarm.

I’m not sure about this:

action:
    service: input_date.set_value

You better check that.

I got that part directly from the Hass website, so I hope it works. I’ll let you know.

And could you give me your wake up alarm automation, so I have an example? This would make it easier as well :slight_smile:

A lot of people asked me for this, it’s rather complicated “construction” so I really have to dive in the package docs and create a package that I can share. I will increase the urgency level on my todo-list :wink:

1 Like

For people stumbling upon this topic, I believe an easier solution (than using tasker and mqtt on my phone) is to use an app which creates a sensor for your next alarm time:

An example for automations can be found on the github

  trigger:
  - minutes: /1
    platform: time_pattern
    seconds: 0
  condition:
  - condition: template
    value_template: '{{ (((as_timestamp(now()) | int) + 3*60) | timestamp_custom("%Y-%m-%d %H:%M:00")) == states.sensor.next_alarm.state }}'
  action:
  - data:
      entity_id: scene.wakeup
    service: scene.turn_on