Adding more information to a list for speaking it

Hello there,

and i want to achieve that i can ask Alexa which windows are open and how long.
But i dont want to add node red or something like this.

Just a routine in Alexa that i can start and then HA tells via Alexa which windows are open and how long.

now i got the Alexa Media Player running and i can trigger a tts.

would be cool to have it as announcement, but i dont know how to do this too.

where i stand now is i got a group with some of my windows
and i got from another thread i got a line which filters that list:

{{ expand(‘switch.fenster’)|selectattr(‘state’,‘eq’,‘on’)|map(attribute=‘name’)|list|join(’, ') }}

now i would like to be able to let her say;
“the following windows are open:”

and then for each entry in the list:
“xxxxx window since xx hours xx minutes”

is there a way to achieve this?

Is there a code reference for that line:
{{ expand(‘switch.fenster’)|selectattr(‘state’,‘eq’,‘on’)|map(attribute=‘name’)|list|join(’, ') }}

which language is that?

where can i help myself?

i feel like the last idiot on this world for having to ask every step on here.

It’s jinja and you can definitely do this just by looping through the list of windows where they are open and then retrieving that data for each. You should use the developer tab template section and construct it till you get closer then ask questions on the way.

The first question for the outer loop is how do you identify all the windows to loop through? By a naming convention? Domain? etc… Once you can produce a list of windows the next step is to filter on the ‘open’ which is already part of your code selectattr above.

Maybe this will get you going…

{% for entity_id in expand('group.presence_simulation_group')|selectattr('state','eq','on')|map(attribute='entity_id')|list %}
  {{state_attr(entity_id,'friendly_name')}} are {{states(entity_id)}}
{%- endfor %}

Output:

Kitchen Overhead Lights are on

thanks for your quick reply.

the outer loop i could achieve with just looping through my switch.fenster helper-group.
then filter by the state eq on.

then i would need to use the attribute ‘name’ and search for the attribute of how long in state (which i dont know what it could be called).

foreach object i iterate i could make a text line added to a variable and in the end send the variable to the endpoint.

that would be my path i guess?

I have routines set up with specific trigger phrases that then run scripts in HA. If you have HA exposed to Alexa, your scripts are available as end points under Smart Home > Scenes in the Alexa routines.

This is what I use for open windows, it is room-based rather than listing every open window by name…
I don’t have anything set up to pull time open.

1 Like

Yes.

{% for entity_id in expand('group.presence_simulation_group')|selectattr('state','eq','on')|map(attribute='entity_id')|list %}
  {{state_attr(entity_id,'friendly_name')}} are {{states(entity_id)}}
  ----  In there you then just need to write the query to compare the current time to the time last_changed time.  Then you know 'how long'.

{%- endfor %}

Or you find someone who has already wrote this to paste their code and win. :slight_smile:

the problem is that i struggle with literally every step in that HA system…

i mean i now got your code snippet and the info that i should use the developer tab.

so i took your snippet, went to the developer tab and then i had the following in the tab:

and guess what? it doesnt do a thing.

what do i do wrong, and why do i need to ask every step?

EDIT: and why does my group not have the name group.fenster?
why is it switch.fenster? i made a helper group.

and why can i have 4 of 5 sonoff sensors in it, but the last one is not a switch, its a binary.sensor.
and its also a sonoff device, i bought 5 and one is different…

thankyou for that too.

i look at it, and then i ask myself: how do i implement this?
i see cool code and all, but where to put and how to call?

i am so lost at this thing called HA.

The indention is wrong. You need to move that template over by 4 spaces…

but that service execution won’t support templates anyway. Use a script to test and play with it.

btw, don’t give up. We’ve all been there. You’ll get the hang of it as you go. Trust me. Keeping asking questions as well.

For Example:

alias: New Script
sequence:
  - service: tts.google_cloud_say
    data:
      entity_id: media_player.back_office_speaker
      message: >-
        {% for entity_id in
        expand('group.presence_simulation_group')|selectattr('state','eq','on')|map(attribute='entity_id')|list   
        %}
          {{state_attr(entity_id,'friendly_name')}} are {{states(entity_id)}}
        {%- endfor %}

1 Like

i moved it 4 spaces, now its saying something.

is there a way to see the output in a console window?

and what is a template and what a script? (in terms of HA)

i developed an application in C# with over 18’000 lines of code, all by myself. Wrote a manual of over 50 pages for it, and 5 people worked with my application all day long for years. All that wasnt that frustrating as it is to implement something so simple in a system like HA.

Yes the developer tab has a ‘template’ tab. That is what you should use to craft this and then test in a script or similar.

a template is a piece of code like the above that can be included in a template sensor, automation or script.

a script is like an automation but without triggers and such. Its useful to call scripts to reuse code a bit but they have lots of uses.

You’ll get this pretty quickly… templating is very powerful. But also pretty confusing.

i now am in the template tab and i got the code there, and i see an output…

its getting somewhere…

thank you for your help.

i now try to get the information of which attributes my switch has from somewhere…
is there a reference?
or can i see it in HA somewhere?

looks like there is no more attribute than the friendly_name:

but HA knows it:
image

where does HA store this information?

attributes are also in the developer tab but in the ‘states’. You can filter on what you want and the attributes are on the right.

On that same tab, you can manually update attributes/states (set state’ section) of entities to aid in testing in the top there.

i found that, i inserted two pics in my answer above…

do you have an idea, where to get the “logbook” entries?

The on and off is the ‘state’ of the entity. The ‘how many minutes ago’ is the last_changed fields.

You should look here for that info:

for example:

{{ as_timestamp(states.binary_sensor.garage_door.last_changed) }}

btw, something like this is going to get you where you need to go to determine how long the window was open…

now i got this:

{{states.switch.sonoff_10015c8b2f.last_changed}}

which gives me a datetime. (i also could use the as_timestamp)

but if i want to have it not from that specific entity, but from the one in the iteration of the for loop?

i tried this:
{{as_timestamp(states.(‘entity_id’).last_changed)}}
and some 20 other variants…

nothing…

{% for entity_id in expand('switch.fenster')|selectattr('state','eq','on')|map(attribute='entity_id')|list %}
  {{state_attr(entity_id,'friendly_name')}} ist offen seit {{as_timestamp(states.entity_id.last_changed)}}
{% endfor %}

{{states.switch.sonoff_10015c8b2f.last_changed}}

and is there a possibility to get that keyhole window a bit bigger in that template tab?
so at least i could see a full line?

Yes it’s tricky. THis will get you it.


 {{ states[entity_id].last_changed.timestamp() }}

But you’ll also need that thread I linked to to make it ‘pretty’ for alexa to say… Like you’ll need the difference between now and the last change and convert that to minutes/hours/days. that is what that thread I linked you to should help with…

Annoying right? I dont think so. I saw an enhancement request for this but nada.

The example I posted was a script. Scripts can be created in the UI or saved to the file scripts.yaml located in the config directory. IMO Scripts are the best way to integrate Alexa routines with HA. The example relies on you creating a binary sensor group through the UI (options for legacy style groups can be found lower down in that same gist).

The following is quick and dirty, and still relies on a binary sensor group, but it gives name and relative time open… I have set it up as a service call for you to test it, but you will want to use a script if your desire is to be able to trigger it by voice through an Alexa.

service: notify.alexa_media_echo_studio_2
data:
  message: >
    {%- set ow = expand('binary_sensor.all_window_sensors')
    | selectattr('state','eq','on')
    | map(attribute='entity_id') | list %}
    {%- set ns = namespace(open_time=[], name=[]) %}
    {%- for w in expand(ow)|list %}
    {%- set open_time = relative_time(expand(w)
    | map(attribute='last_changed')|join|as_datetime) %}
    {%- set name = w.name%}
    {% set ns.open_time = ns.open_time + [name~" has been open for about "~open_time]%}
    {%- endfor %}
    {% set qty = ow | count %} 
    {% if qty != 0 %}
    There {{'is an' if qty==1 else 'are '~qty}} open window{{' ' if qty==1
    else 's '}} 
    {% if ns.open_time not in [[], None, 'unknown'] %}
    {{ns.open_time|join(", \n")}}
    {% endif %}
    {% else %}
    There are no open windows
    {% endif %}
  data:
    type: tts 

making it pretty will be the next step, making it run is basic i guess :slight_smile:

The keyhole is really annoying, true…it is consuming some brainpower only by not seeing the text right, hahahaha

i switched from work notebook to private one, and will give it a try again…

thanks for hanging in with me.
i am not very friendly i know, thats because of my autism. but i seriously am happy that you help me!

1 Like

comes up the question:

what is a binary sensor group?

i made a helper → group → switch and put my “switches” in it. 4 of 5 sonoff. the 5th is a binary…

that makes me more clueless than i was before…