Help with template to tts please?

I have found in a previous post;

input_number:
  alarm_1_hour:
    name: Hours
    icon: mdi:timer
    initial: 5
    min: 0
    max: 23
    step: 1
  alarm_1_minutes:
    name: Minutes
    icon: mdi:timer
    initial: 40
    min: 0
    max: 59
    step: 1
  alarm_1_offset:
    name: Transition
    icon: mdi:blur-linear
    initial: 10
    min: 0
    max: 60
    step: 5
    
input_boolean:
  alarm_1_enable:
    name: "Enable Alarm"
  alarm_1_mon:
    name: Monday
    icon: mdi:calendar
  alarm_1_tue:
    name: Tuesday
    icon: mdi:calendar
  alarm_1_wed:
    name: Wednesday
    icon: mdi:calendar
  alarm_1_thu:
    name: Thursday
    icon: mdi:calendar
  alarm_1_fri:
    name: Friday
    icon: mdi:calendar
  alarm_1_sat:
    name: Saturday
    icon: mdi:calendar
  alarm_1_sun:
    name: Sunday
    icon: mdi:calendar    


sensor:
  - platform: template
    sensors:
      alarm_1_time:
        friendly_name: 'Alarm Time'
        value_template: >-
          {{ "%0.02d:%0.02d" | format(states("input_number.alarm_1_hour") | int, states("input_number.alarm_1_minutes") | int) }}
  - platform: template
    sensors:
      alarm_1_time_minus_offset:
        friendly_name: 'Offset Time'
        value_template: >-
          {{ '%0.02d:%0.02d' | format( ((((states('input_number.alarm_1_hour') | int)*60 + (states('input_number.alarm_1_minutes') | int) - (states('input_number.alarm_1_offset'))| int)/60)| int),(((((((((states('input_number.alarm_1_hour') | int)*60 + (states('input_number.alarm_1_minutes') | int) - (states('input_number.alarm_1_offset')) | int)/60)) - ((((states('input_number.alarm_1_hour') | int)*60 + (states('input_number.alarm_1_minutes') | int) - (states('input_number.alarm_1_offset')) | int)/60)| int))*100) | int)*60/100) | round) ) }}

group:
  alarm_1:
    name: Work Alarm
    entities:
    - input_boolean.alarm_1_enable
    - sensor.alarm_1_time
    - sensor.alarm_1_time_minus_offset
    - input_number.alarm_1_hour
    - input_number.alarm_1_minutes
    - input_number.alarm_1_offset
    - input_boolean.alarm_1_mon
    - input_boolean.alarm_1_tue
    - input_boolean.alarm_1_wed
    - input_boolean.alarm_1_thu
    - input_boolean.alarm_1_fri
    - input_boolean.alarm_1_sat
    - input_boolean.alarm_1_sun


automation:
- id: alarm_1
  alias: 'Work Alarm'
  trigger:
    - platform: time_pattern
      minutes: '/1'
      seconds: 00
  condition:
    - condition: template
      value_template: "{{ states.sensor.time.state == states.sensor.alarm_1_time_minus_offset.state  }}"
    - condition: state
      entity_id: input_boolean.alarm_1_enable  
      state: 'on'
    - condition: or
      conditions:
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarm_1_mon
              state: 'on'
            - condition: time
              weekday: 
                - mon
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarm_1_tue
              state: 'on'
            - condition: time
              weekday: 
                - tue
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarm_1_wed
              state: 'on'
            - condition: time
              weekday: 
                - wed
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarm_1_thu
              state: 'on'
            - condition: time
              weekday: 
                - thu
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarm_1_fri
              state: 'on'
            - condition: time
              weekday: 
                - fri
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarm_1_sat
              state: 'on'
            - condition: time
              weekday: 
                - sat             
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarm_1_sun
              state: 'on'
            - condition: time
              weekday: 
                - sun               

  action:
  - data: {}
    service: script.bedroom_sunrise    

my script.bedroom_sunrise reads;

bedroom_sunrise:
  alias: morning has broken
  sequence:
  - service: light.turn_on
    data:
      brightness_pct: '0'
      entity_id:
      - light.my_room_light_4
      rgb_color:
      - 255
      - 169
      - 92
      transition: '0'
  - service: mqtt.publish
    data:
      topic: homeassistant/tts/android_johns_phone_tts/tts
      payload: time to wake up dude the current time is "{{ 'states.sensor.time.state' }}" and alarm is set to "{{ 'states.sensor.alarm_1_time.state' }}" with an offset of "{{ 'states.input_number.alarm_1_offset.state' }}" set to "{{ 'states.sensor.alarm_1_time_minus_offset.state' }}" Weekdays ("{{ 'states.input_boolean.alarm_1_weekday.state' }}" ) and Weekends ("{{ 'states.input_boolean.alarm_1_weekend.state' }}" ) 00:"{{ 'states.input_number.alarm_1_offset.state | int' }}":00

But on alarm activation the payload reads exactly as it is written rather than returning the values of the template.
Now I am sure it is a ‘thunder-fingers’ error (read typo or misunderstanding) for the life of me I cannot see it!

I have tried a couple of permutations but am as yet unable to wrap my head round it?

examples, guidance, and pointers to docs are all appreciated.

Happy New Year to you all may it gift you with the learning you need to succeed!

Change data: to data_template: in the mqtt.publish service call.

See here for more info.

Edit: also, you don’t need double quotes around each of the templates. You can just wrap the entire payload in double quotes. Here’s an example of an automation I have that involves a data_template:

  - service: notify.telegram
    data_template:
      message: "Black ink is at {{ states('sensor.printer_black_ink') }}%."
1 Like

Thank you quick, informative and also complete! As in what I did wrong and the pointer to the explanation that explained why! Everything I asked for.

1 Like