Change of template icon lags the state change

I’m building my notification card using GitHub - royto/logbook-card: Logbook card for Home Assistant UI Lovelace. so that I can display the notifications that I sent to my script notifications How to listen to an MQTT topic in automation - #7 by 123.

I called input_text helper at the end of the script to store the state. Since, I can’t customise the icon of the input_text helper, I had to create a new template sensor that is updated from the input_helper state change.

template:
  - sensor:
      - name: "notification log"
        state: " {{ states('input_text.log_notification') }} "
        icon: > 
          {% if 'Garage' in this.state %}
            mdi:garage
          {%  elif 'Washing' in this.state %}
            mdi:washing-machine
          {% else %}
            mdi:bell
          {% endif %}
          

The idea is so that I can also show the icon along with notification by picking up a string from the notification which is a state.

Unfortunately, the icon update has a lag so the change in state of the sensor is registered first and icon later so the log card shows the icon of the previous state and not of the current state which has triggered the log activity.

I know it is such a convoluted way… unless someone can advise a neat solution.

template:
  - sensor:
      - name: "notification log"
        state: "{{ states('input_text.log_notification') }}"
        icon: >
          {% set notification = states('input_text.log_notification') %}
          {% if 'Garage' in notification  %}
            mdi:garage
          {%  elif 'Washing' in notification  %}
            mdi:washing-machine
          {% else %}
            mdi:bell
          {% endif %}

this only works if you have a fast machine. If your machine is slow, this.state will always be the previous state.

Fixed it by referring to the actual sensor to check the state

template:
  - sensor:
      - name: "notification log"
        state: " {{ states('input_text.log_notification') }} "
        icon: > 
          {% if 'Garage' in states('input_text.log_notification')  %}
            mdi:garage
          {%  elif 'Washing' in states('input_text.log_notification') %}
            mdi:washing-machine
          {% else %}
            mdi:bell
          {% endif %}

Yes, that’s what the template I provided above did that I posted 18 hours ago.

1 Like

Not sure how I missed it :thinking: but thanks again.