Code optimization

I’m at the point not to implement new features but to optimize my code and make it more efficient.
Does anybody have an idea or can push me into the right direction, to write this template more efficient and make it maybe faster:

{% if states('binary_sensor.wohnzimmer') == 'on' or states('binary_sensor.wohnzimmer_2') == 'on' %}
        {% elif states('binary_sensor.treppe_oben') == 'on' %}
         Treppe oben
        {% elif states('binary_sensor.treppe_unten') == 'on' %}
         Treppe unten         
        {% elif states('binary_sensor.schlafzimmer') == 'on' or states('binary_sensor.schlafzimmer2') == 'on' %}
        {% elif states('') == 'on' %}
        {% elif states('binary_sensor.presence_47') == 'on' or states('binary_sensor.guestroom') == 'on' %}
        {% elif states('binary_sensor.fitnessroom') == 'on' or states('binary_sensor.fitnessroom2') == 'on' %}
        {% else %}
        {%- endif %}

It looks like old visual basic code ;-).
I have some more of these endles if…then…else templates

PS: This code is inefficient due to the fact, the when moving around, the old motion sensor is still ‘on’ meanwhile the next one turns to ‘on’. I would like to catch always the last one which changed to ‘on’
PS2: This issue is solved. I’m writing the friendly name of every sensor into a variable when turning on. That’s more efficient. But this, we call it spaghetti code, is still not elegant. Are there better notations? Did not found something via google

That has nothing to do with spaghetti code.

For optimization, it always depends on the specific code.
E.g. I see lots of people using
If sensor x = 1
then A
Elif sensor x = 2
then B

Meaning they check the same sensor multiple times, this can be solved with a mapping.
Below a random example from my system.

{% set current_activity = state_attr('remote.wohnzimmer', 'activity') %}
{% set map = {
  "Fernsehen": "Swisscom DVR",
  "Filme/Serien": "Kodi Wohnzimmer"
{{ map[current_activity] }

Thanks for the hint. You’re right.
When reacting on different states of one entity, it’s better to safe it into a variable and play with it later.
In my case, I made something similar when saving the name of the entity into a variable after being triggered.
Thanks again. I will examin my code for cases similar to the one you pointed out.

1 Like

this would be a fine example for the need of a guard we spoke of elsewhere :wink:

it might very well be the state of the entity would act up and be not in the mapper. In that case there should be a ‘guard’ for that, an else clause

{{ map[current_activity] if current_activity in map else 'Default player' }}

Ah great, thats exactly I was already looking for.
My solution for this:

 {% set current_activity = state_attr('remote.wohnzimmer', 'current_activity') %}
          {% set map = {
            "PowerOff": "off",
            "Fire TV": "41158611",
            "DVD": "41157860",
            "Musik hören": "41157868",
          {{ map[current_activity] if current_activity in map else '41157862' }}

By the way, do you have a good documentation in order to learn more about jinja.
My knowledge comes from, PL/SQL, and C# 20 years ago.
So the basics are there but I’m missing the syntax in jinja or python

Ah I see, in my example I don’t need this at it is handled in a condition before it arrives at the template. No guarding needed :slight_smile:

This was great.
I could optimize a lot of scripts like this:

    - service: alexa_media.update_last_called
    - delay: "00:00:02"
    - service: input_boolean.turn_on
        entity_id: >
          {% set last_called = states('sensor.last_called_alexa') %}
          {% set map = {
            "media_player.wohnzimmer": "input_boolean.light_livingroom_automation",
            "media_player.gastezimmer": "input_boolean.light_guestroom_automation",
            "": "input_boolean.light_office_automation",
            "media_player.fitnessraum": "input_boolean.light_fitnessroom_automation",
            "media_player.schlafzimmer": "input_boolean.light_bedroom_automation",
            "media_player.bad_yvonne": "input_boolean.light_bedroom_automation"
          {{ map[last_called] }}
    - service: notify.alexa_media
        target: "{{states('sensor.last_called_alexa')}}"
          type: tts
        message: "Die Lichtautomatisierung im {{ states('sensor.last_called_alexa') |replace('media_player.', ' ') }} wurde wieder aktiviert"

Thanks for your support
To be accurate: No guarding needed because I will not have more echos :slight_smile:
Enough is enough

What if the sensor.last_called_alexa shows unknown or unavailable?

exactly, its for guarding the unexpected, not the expected :wink:

Yees, you caught me.
Never had that case but its for sure possible and in order to accurate guidelines to be guarded. :blush:
Edit: But this case is interesting.
What could be a good else. ?
switching on any input_boolean would cause confusion
Here, I would have a log entry but the user simply recognizes that nothing happens an try again.
What I’m missing is an error handler at the end where I can inform the user that something was wrong or call last_called_alexa again
2nd edit: Ok, i can implement a condition upfront and check the sensor.
3 edit: Ok choose would be the right way.
in case of an empty sensor - initiate a notify else do the work
ergo - no need for an else based guarding