Adding more information to a list for speaking it

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…

Did you create the group in the UI or in a file like configuration.yaml or groups.yaml?

It shouldn’t be possible to combine switches and binary sensors in the UI…

in the UI… (shame on me)

nope i couldnt combine, that was a problem for later… means 20 questions later, hahahaha

Ok, do you want to keep the sensors defined as switches?

do i have a choice?

Yes, you can modify the domain in a couple ways…

If they are zigbee devices (Sonoff’s zigbee devices often are mis-classified) you can change their domain in your zigbee integration.

You can create a “mirror” entity in Helpers > Change device type of a switch, but binary sensor is not an option as an output.

You can create a template binary sensor for each entity mis-classified as a switch. This could be tedious.


If none of those appeal to you, it is still possible to create a mixed-domain group, but it must be done in the configuration.yaml. Old-Style Groups

My suggestion is to ‘fix that after’ in the zigbee integration… But at least get this tts accomplished for now so you feel accomplished. You can always just change the group again after.

they are wifi ones… because i initially needed wifi ones for the washing machine and the dryer, because the room is far away and no chance to get cable there to mount a zigbee hub…

and then of 2 ordered, one came broken. then i ordered more and now i got 5.

but one is different, he identifies as binary. the rest as switch.

Drew’s code will work with any of the above for now… It’ll expand any of them i believe…

Strange…

Option 4: We work around the strangeness… replace the all caps part with your binary sensor’s actual object id.

service: notify.alexa_media_echo_studio_2
data:
  message: >
    {%- set ow = expand('switch.fenster', 'binary_sensor.YOUR_BINARY_SENSOR')
    | 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