I have an automation to set alarms on an alexa device based on the time in an input helper. Alexa doesn’t take the time in the input helper directly. I found this macro that breaks it out so the alarm can be set correctly:
service: media_player.play_media
data:
media_content_id: >-
{% set hour_twentyfour =
states('input_datetime.alison_weekday_alarm_helper').split(':')[0]|int(0) %}
{% set hour_twelve = hour_twentyfour if hour_twentyfour < 13 else hour_twentyfour - 12 %}
{% set minutes = states('input_datetime.ma_alarm_set_time').split(':')[1] %}
{% set minutes_formatted = minutes if minutes != '00' else '' %}
{% set am_pm = 'am' if hour_twentyfour < 12 else 'pm' %}
Cancel alarm for {{hour_twelve}} {{minutes_formatted}} {{am_pm}}
media_content_type: custom
target:
entity_id: media_player.office_echo
I’d like to make this a service that I can call. I’ve created the service below. The input date/time helper is not evaluated by the script. As you can see in the script I added a persistent notification so I can see what it was doing with the time. It always comes back as zero. What is the best way to pass this input date/time helper to the script and have it evaluated by this macro?
alias: Alexa Alarm Hander
sequence:
- service: media_player.play_media
data:
media_content_id: >
{% set hour_twentyfour =states( "{{ date_time_helper }}"
).split(':')[0]|int(0) %} {% set hour_twelve = hour_twentyfour if
hour_twentyfour < 13 else hour_twentyfour - 12 %} {% set minutes =
states( "{{ date_time_helper }}" ).split(':')[1] %} {% set
minutes_formatted = minutes if minutes != '00' else '' %} {% set am_pm =
'am' if hour_twentyfour < 12 else 'pm' %} {{hour_twelve}}
{{minutes_formatted}} {{am_pm}}
media_content_type: custom
target:
entity_id: media_player.office_echo
enabled: false
- if:
- condition: template
value_template: "{{on_off == 'on' }}"
then:
- service: media_player.play_media
data:
media_content_id: >
{% set hour_twentyfour=states( '{{date_time_helper}}'
).split(':')[0]|int(0) %} {% set hour_twelve = hour_twentyfour if
hour_twentyfour < 13 else hour_twentyfour - 12 %} {% set minutes =
states( '{{date_time_helper}}' ).split(':')[1] %} {% set
minutes_formatted = minutes if minutes != '00' else '' %} {% set
am_pm = 'am' if hour_twentyfour < 12 else 'pm' %} Set alarm for
{{hour_twelve}} {{minutes_formatted}} {{am_pm}}
media_content_type: custom
target:
entity_id: media_player.office_echo
enabled: true
- service: notify.persistent_notification
data:
message: >
On
Helper{{states('input_datetime.alison_weekday_alarm_helper').split(':')[0]|int(0)}}
field: {{states('{{ date_time_helper }}').split(':')[0]|int(0)}}
else:
- service: media_player.play_media
data:
media_content_id: >
{% set hour_twentyfour=states('{{ date_time_helper
}}').split(':')[0]|int(0) %} {% set hour_twelve = hour_twentyfour if
hour_twentyfour < 13 else hour_twentyfour - 12 %} {% set minutes =
states('{{date_time_helper}}').split(':')[1] %} {% set
minutes_formatted = minutes if minutes != '00' else '' %} {% set
am_pm = 'am' if hour_twentyfour < 12 else 'pm' %} Cancel alarm for
{{hour_twelve}} {{minutes_formatted}} {{am_pm}}
media_content_type: custom
target:
entity_id: media_player.office_echo
enabled: true
mode: single
icon: mdi:alarm
fields:
target_speaker:
selector:
text: null
name: target_speaker
date_time_helper:
selector:
text: null
name: date_time_helper
description: ID Of the Date Time Helper
on_off:
selector:
text: null
name: On/Off
description: Turn Alarm On or Off