Why am I not able to read the state of a variable and send it to tts?

'm not really getting why it says state

unknown

Would any of you be so kind to explain what I’m missing here? I’m not really sure how to send the state on the passed variable containing an input boolean through to tts.

This is my code:

# make sure you have created an entity called input_boolean.living_room_auto_lighting (or other name) as a helper.
# you can call this script in an automation or as a service call.
# pass the given variables as data so that the script knows what speaker and switchs to use 
#
# service: script.auto_lighting_switch (or other name)
# data: 
#   entity_id: media_player.living_room_speaker
#   switch: input_boolean.living_room_auto_lighting

alias: Auto Lighting - General Switch
sequence:
  - service: media_player.turn_on
    data: {}
    target:
      entity_id: '{{ entity_id }}'
  - service: input_boolean.toggle
    data: {}
    target:
      entity_id: '{{ switch }}'
  - service: tts.cloud_say
    data:
      entity_id: '{{ entity_id }}'
      message: Auto lighting is turned {{ states('{{ switch }}') }}
description: A script to set the auto lighting switch to on/of and TTS to a speaker.
fields:
  entity_id:
    description: Speaker
    example: media_player.living_room_speaker
    required: true
  switch:
    description: Switch
    example: input_boolean.living_room_auto_lighting
    required: true
mode: single

I’m getting this as a response:

Executed: March 15, 2022, 8:48:42 PM
Result:

params:
  domain: tts
  service: cloud_say
  service_data:
    entity_id: media_player.living_room_speaker
    message: Auto lighting is turned unknown
  target: {}
running_script: false
limit: 10
1 Like

Hi @elvee

try this

  message: Auto lighting is turned '{{ states(switch) }}'

Kind regards,
Ghassan

Thanks a lot. That seemed to work! I was so close! Would you be so kind to explain the syntax (point me to the right direction) because I’m not really sure why the format is like this.

'{{ and }}' marks the beginning and end of the template, that is the expression to be interpreted.
That’s why you shouldn’t make a new begin/end with in the overall begin/end marking.

Hope this gives sence :slight_smile:

Didn’t see your reply yet. Sorry! You explained it as well.

I thank you both for your quick response. I’ve been breaking my head on this one.