2 Different way to use states or state_attr what is better

Hi All,

Can someone tell me what is better to use? I read when you add entities inside (’ … ') its better because it won’t give errors when its not ready.

I have many lines with states. instead of state_attr(’ … ') so if this is better I will change them.

Im not that smart to know if first option or second option is better.

old

        {% if states.remote.logitech_harmony.attributes.current_activity == "PowerOff" %}
          off
        {% elif states.remote.logitech_harmony.attributes.current_activity == "AppleTV" %}
          AppleTV
        {% elif states.remote.logitech_harmony.attributes.current_activity == "Ziggo" %}
          TV
        {% elif states.remote.logitech_harmony.attributes.current_activity == "AirPlay" %}
          Airplay
        {% elif states.remote.logitech_harmony.attributes.current_activity == "Kodi" %}
          Kodi
        {% elif states.remote.logitech_harmony.attributes.current_activity == "Domoticz" %}
          Domoticz
        {% endif %}

possible new way

        {% if state_attr('remote.logitech_harmony','current_activity') == "PowerOff" %}
          off
        {% elif state_attr('remote.logitech_harmony','current_activity') == "AppleTV" %}
          AppleTV
        {% elif state_attr('remote.logitech_harmony','current_activity') == "Ziggo" %}
          TV
        {% elif state_attr('remote.logitech_harmony','current_activity') == "AirPlay" %}
          Airplay
        {% elif state_attr('remote.logitech_harmony','current_activity') == "Kodi" %}
          Kodi
        {% elif state_attr('remote.logitech_harmony','current_activity') == "Domoticz" %}
          Domoticz
        {% endif %}

Take a look here, especially the warning. state_attr(…) is better, because it doesn’t cause errors on startup when an entity is not available.

For the example you posted, can use is_state_attr(…)

{% if is_state_attr('remote.logitech_harmony', 'current_activity', 'PowerOff' %}

Personally I’d rewrite your automation and use a variable for the current_activity attribute and then check this variable for the possible states instead of repeating is_state_attr(…)

3 Likes

thank you for the rewrite. I will update it. in meanwhile I was testing my new part.
And thanks for the link, lots of info that makes more sense to understand things also!

it will be like this… you start with ( but there was no ) at the end :slight_smile:

{% if is_state_attr('remote.logitech_harmony', 'current_activity', 'PowerOff') %}

"{{ ((states.sensor.meek_hallway_wifi_info.attributes.WiFi['Connected msec']) / 1000 / 60 / 60 ) | int  }}"

Do you know how I can do this in a better way also? :slight_smile:

"{{ ((state_attr('sensor.meek_hallway_wifi_info', 'WiFi')['Connected msec']) / 1000 / 60 / 60 ) | int  }}"
1 Like

Thanks for all the info, updates almost all the lines now :smiley:

1 Like

Hi @Burningstone,

I had the first line. You rewrite it to the second.

{% if state_attr('remote.logitech_harmony','current_activity') == "PowerOff" %}
{% if is_state_attr('remote.logitech_harmony', 'current_activity', 'PowerOff' %}

I can’t check out what different is. Output is same. Is there a reason to use the second line?
Better coding, or other reasons?
I have lot of lines like the first one, and if its better to change them also then I have something to do in my second Xmas Day haha

1 Like

There’s no difference between these two methods, I just find the second one cleaner and it was created to exactly cover this use case.
You can leave it as it is,in the end of the day that’s more about preferwnce and readability,you need to maintain your code not me, so it has to be the way that is most convenient for you.

1 Like

Thanks! Always more ways to Rome. We say here in NLD

I have mixed them up. So will update them… Everything same