Formatting script

Hi guys

i’m on the edge of going all hulk on this its doing my head in.

i’m editing my script and i have tried formatingg this so many ways and it errors no matter what i do.

tried.

service: notify.alexa_media
data:
  data:
    type: tts
  message: "{%- if is_state('sensor.current_lights_on', '0') -%}
           {{ 'There are no lights on.'}}
         {%- else -%}
           {{'There are currently ' + {{states.sensor.current_lights_on.state}} + ' Lights on. Which are,'}}
         {%- endif -%}
         {%- if is_state('light.main_bedroom_ceiling_light_2', 'on') -%}
           {{ 'Main Bedroom ceiling light is on'}}
         {%- endif -%}
         {%- if is_state('light.bathroom_light_2','on') -%}
           {{ 'Bathroom light is on' }}
         {%- endif -%}
         {%- if is_state('light.main_bedroom_lamp','on') -%}
           {{ 'Main Bedroom lamp light is on' }}
         {%- endif -%}
         {%- if is_state('light.main_hallway_light','on') -%}
           {{ 'Main Hallway light is on' }}
         {%- endif -%}
         {%- if is_state('light.kitchen_light_1_2','on') and is_state('light.kitchen_light_2_2','on') -%}
           {{ 'Both Kitchen lights is on' }}
         {%- elif is_state('light.kitchen_light_1_2','on') or is_state('light.kitchen_light_2_2','on') -%}
           {{ ' 1 Kitchen light is on' }}
         {%- endif -%}
         {%- if is_state('light.lounge_room_light','on') -%}
           {{ 'Lounge light is on' }}
         {%- endif -%}
         {%- if is_state('light.back_room_light','on') -%}
           {{ 'Back room light is on' }}
         {%- endif -%}
         {%- if is_state('light.dinning_room_light','on') -%}
           {{ 'Dining room light is on' }}
         {%- endif -%}
         {%- if is_state('light.front_room_light','on') -%}
           {{ 'Front room light is on' }}
         {%- endif -%}
         {%- if is_state('light.hallway_light_2','on') -%}
           {{ 'Hallway light is on' }}
         {%- endif -%}
         {%- if is_state('light.computer_room_light','on') -%}
           {{ 'Computer room light is on' }}
         {%- endif -%}
         {%- if is_state('light.bedroom_light_2','on') -%}
           {{ 'Bedroom light is on' }}
         {%- endif -%}
         {%- if is_state('switch.outside_light_switch','on') -%}
           {{ 'Outside lights is on' }}
         {%- endif -%}"
  target: '{{ states.sensor.last_alexa.state }}'

tried


message: "{% if is_state('sensor.current_lights_on', '0') %}
           There are no lights on.
         {% else %}
           There are currently {{states.sensor.current_lights_on.state}} Lights on. Which are,
         {% endif %}
         {% if is_state('light.main_bedroom_ceiling_light_2', 'on') %}
           Main Bedroom ceiling light is on
         {% endif %}
         {% if is_state('light.bathroom_light_2','on') %}
           Bathroom light is on
         {% endif %}
         {% if is_state('light.main_bedroom_lamp','on') %}
           Main Bedroom lamp light is on
         {% endif %}
         {% if is_state('light.main_hallway_light','on') %}
           Main Hallway light is on
         {% endif %}
         {% if is_state('light.kitchen_light_1_2','on') and is_state('light.kitchen_light_2_2','on') %}
           Both Kitchen lights is on
         {% elif is_state('light.kitchen_light_1_2','on') or is_state('light.kitchen_light_2_2','on') %}
            1 Kitchen light is on
         {% endif %}
         {% if is_state('light.lounge_room_light','on') %}
           Lounge light is on
         {% endif %}
         {% if is_state('light.back_room_light','on') %}
           Back room light is on
         {% endif %}
         {% if is_state('light.dinning_room_light','on') %}
           Dining room light is on
         {% endif %}
         {% if is_state('light.front_room_light','on') %}
           Front room light is on
         {% endif %}
         {% if is_state('light.hallway_light_2','on') %}
           Hallway light is on
         {% endif %}
         {% if is_state('light.computer_room_light','on') %}
           Computer room light is on
         {% endif %}
         {% if is_state('light.bedroom_light_2','on') %}
           Bedroom light is on
         {% endif %}
         {% if is_state('switch.outside_light_switch','on') %}
           Outside lights is on
         {% endif %}"

Even how it was orginally in the script yaml file

"{% if is_state('sensor.current_lights_on', '0') %}\nThere are no lights on.\n{% else %}\nThere are currently {{states.sensor.current_lights_on.state}} Lights on. Which are,\n{% if is_state('light.main_bedroom_ceiling_light_2', 
        \ 'on') %}\nMain Bedroom ceiling light is on\n{% endif %}\n{% if is_state('light.bathroom_light_2',\
        \ 'on') %}\nBathroom light is on\n{% endif %} \n{% if is_state('light.main_bedroom_lamp',\
        \ 'on') %}\nMain Bedroom lamp light is on\n{% endif %} {% if is_state('light.main_hallway_light',\
        \ 'on') %}\nMain Hallway light is on\n{% endif %} {% if is_state('light.kitchen_light_1_2',\
        \ 'on') %}\nKitchen light is on\n{% endif %} {% if is_state('light.lounge_room_light',\
        \ 'on') %}\nLounge light is on\n{% endif %} {% if is_state('light.back_room_light',\
        \ 'on') %}\nBack room light is on\n{% endif %} {% if is_state('light.dinning_room_light',\
        \ 'on') %}\nDining room light is on\n{% endif %} {% if is_state('light.front_room_light',\
        \ 'on') %}\nFront room light is on\n{% endif %} {% if is_state('light.hallway_light_2',\
        \ 'on') %}\nHallway light is on\n{% endif %} {% if is_state('light.computer_room_light',\
        \ 'on') %}\nComputer room light is on\n{% endif %} {% if is_state('light.bedroom_light_2',\
        \ 'on') %}\nBedroom light is on\n{% endif %} {% if is_state('switch.outside_light_switch',\
        \ 'on') %}\nOutside lights is on\n{% endif %}"

the template works fine i can get it to work in the template area in development

its just the formatting in the yaml fine i cant get it to work!

so fustrating

You can use the line continuation character, thereby allowing you to start the template on the following line and eliminating the outer double-quotes.

message: >
  {%- if is_state('sensor.current_lights_on', '0') -%}
  ... etc ...

Shouldn’t target specify the entity_id of the desired media player?


If you are interested, copy-paste the following example into the Template Editor and see if it reports the lights that are on to your satisfaction:

{% set  lights = states.light | selectattr('state', 'eq', 'on') | map(attribute='name') | list | join(', ') %}
{% set qty = states('sensor.current_lights_on') | int(0) %}
{% if qty > 0 %}
  {% set plural = 's' if qty > 1 else '' %}
  {% set prep = 'are' if qty > 1 else 'is' %}
  There {{prep}} {{qty}} light{{plural}} on. {{' and '.join(lights.rsplit(', ', 1))}} light{{plural}}, {{prep}} on.
{% else %}
  There are no lights on.
{% endif %}

If it works as desired, you can incorporate it into the Alexa notification.

1 Like

Wow i didnt expect you to rewrite the script. i was just asking how to format it so it didnt error when adding it to the script due to formatting

it works great. just 1 little problem i have a switch in the list, it doesnt use the light extention

all good i worked it out :slight_smile: Thank you very very much

i did

{% set  lights = states.light | selectattr('state', 'eq', 'on') | map(attribute='name') | list | join(', ') %}
  {% if is_state('switch.outside_light_switch','on') %}
    {% set lights = lights + ', Outside lights' %}
  {% endif %}
{% set qty = states('sensor.current_lights_on') | int(0) %}
{% if qty > 0 %}
  {% set plural = 's' if qty > 1 else '' %}
  {% set prep = 'are' if qty > 1 else 'is' %}
  There {{prep}} {{qty}} light{{plural}} on. {{' and '.join(lights.rsplit(', ', 1))}} light{{plural}}, {{prep}} on.
{% else %}
  There are no lights on.
{% endif %}

In that case, you can use the expand function to combine all lights and that one switch.

{% set lights = expand(states.light, states.switch.outside_light_switch) | selectattr('state', 'eq', 'on') | map(attribute='name') | list | join(', ') %}