Get string value from input_datetime

I followed this link (changed to Google backend) to set up Sonos TTS:

I setup this automation:

alias: Sleep_tobeupdated
description: ''
trigger:
  - platform: time
    at: input_datetime.time_sleep
condition: []
action:
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.is_sleeping
  - service: light.turn_on
    entity_id: light.all_lights
    data: {}
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.is_home
  - service: script.sonos_say
    data:
      sonos_entity: media_player.library
      volume: 0.3
      message: {{"You have set wake up time to be " ~ states.input_datetime.time_wake.attributes.hour ~ ":" ~states.input_datetime.time_wake.attributes.minute}}
      delay: '00:00:10'
mode: single

When I run the script, it reads out “object object none” from my Sonos speaker. “message” is sometimes automatically changed by system to be something like this (from my memory, I cannot reproduce it):

message:
  [object][object]: None

In other times, it is automatically changed to Null

When I use the Template editor from Developer, the text is correct and string value is returned:

I feel that value_template may be the way to go, but I consulted the official documentation on Templating and there is no example for such use case.

Thanks. Revan

Add single quotes outside of the two curly brackets at the beginning and the end of your template:

message: '{{"You have set wake up time to be " ~ states.input_datetime.time_wake.attributes.hour ~ ":" ~states.input_datetime.time_wake.attributes.minute}}'
1 Like

Except that he’s triggering on a different input_datetime than the one used in the measage.

1 Like

Many thanks, this does the trick! I actually tried a double quotation mark before, which did not work.

You need to have either single quotes outside the curly brackets and double quotes inside the curly brackets or vice versa.

1 Like

Why are you combining the hour and minute attributes, from input_datetime.time_wake, when you can get the time directly from its state?

2 Likes

@Revan_Ng - here’s the doc info on the quotes and how to use them in templates. My MO is to always start the template on the next line so I don’t get bitten by incorrect quotes - that’s at least one less thing I need to worry about :wink:

Automation Templating - Home Assistant (home-assistant.io)

This has some other good info on using templates in automations - specifically how to use the trigger state object. I find it to be very useful.

2 Likes