Template sensors problem with 3 conditions

I’m trying to get a template sensor to figure out which of my 3 Wifi APs my ESPHome nodes are connected to, using the new wifi_info text sensor in ESPHome. The sensor sensor.basement_bssid retruns the MAC address of the ap which the node is connected to.

  - platform: template
    sensors:
      ap_convert:
        friendly_name: "Connected AP"
        value_template: >-
          {% if states('sensor.basement_bssid', '80:2A:A8:11:95:BF') %}
            AP_AC_LITE
          {% elif states('sensor.basement_bssid', 'b4:fb:e4:46:89:53') %}
            AP_AC_PRO
          {% else states('sensor.basement_bssid', 'f0:9f:c2:cb:3a:58') %}
            AP_AC_LR
          {% endif %}
ERROR (MainThread) [homeassistant.config] Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got 'states') for dictionary value @ data['sensors']['ap_convert']['value_template']. Got "{% if states('sensor.basement_bssid', '80:2A:A8:11:95:BF') %}\n  AP_AC_LITE\n{% elif states('sensor.basement_bssid', 'b4:fb:e4:46:89:53') %}\n  AP_AC_PRO\n{% else states('sensor.basement_bssid', 'f0:9f:c2:cb:3a:58') %}\n  AP_AC_LR\n{% endif %}\n\n# {% if is_state('sensor.sensor.basement_bssid', '80:2A:A8:11:95:BF') %} #   AP_AC_LITE # {% else is_state('sensor.basement_bssid', 'b4:fb:e4:46:89:53') %} #   AP_AC_PRO # {% elif is_state('sensor.basement_bssid', 'f0:9f:c2:cb:3a:58') %} #   AP_AC_L.... (See /config/sensor/template.yaml, line 80). Please check the docs at https://home-assistant.io/components/sensor.template/

This is what I have so far, however it doesn’t work and i simply can’t get it to work. Anyone have some insights?

try is_state( instead of states(

With state instead of states i get

ERROR (MainThread) [homeassistant.config] Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got 'state') for dictionary value @ data['sensors']['ap_convert']['value_template']. Got "{% if state('sensor.basement_bssid', '80:2A:A8:11:95:BF') %}\n  AP_AC_LITE\n{% elif state('sensor.basement_bssid', 'b4:fb:e4:46:89:53') %}\n  AP_AC_PRO\n{% else state('sensor.basement_bssid', 'f0:9f:c2:cb:3a:58') %}\n  AP_AC_LR\n{% endif %}\n\n# {% if is_state('sensor.sensor.basement_bssid', '80:2A:A8:11:95:BF') %} #   AP_AC_LITE # {% else is_state('sensor.basement_bssid', 'b4:fb:e4:46:89:53') %} #   AP_AC_PRO # {% elif is_state('sensor.basement_bssid', 'f0:9f:c2:cb:3a:58') %} #   AP_AC_LR #.... (See /config/sensor/template.yaml, line 80). Please check the docs at https://home-assistant.io/components/sensor.template/

I’ve seen both state and states used in examples without really grasping if there is a difference to it :slight_smile:

is_state not state:

  - platform: template
    sensors:
      ap_convert:
        friendly_name: "Connected AP"
        value_template: >-
          {% if is_state('sensor.basement_bssid', '80:2A:A8:11:95:BF') %}
            AP_AC_LITE
          {% elif is_state('sensor.basement_bssid', 'b4:fb:e4:46:89:53') %}
            AP_AC_PRO
          {% elif is_state('sensor.basement_bssid', 'f0:9f:c2:cb:3a:58') %}
            AP_AC_LR
          {% endif %}

:man_facepalming:
Yes of course that should be is_state, however it still doesn’t work, I’m wondering if there is something else that is also missing or wrong in my code, it kinda returns the same error message if I use is_state state or states.

I have limited experience with templating so it’s not obvious to me where, but likely in the in the if statements somewhere. I see the code highlighting in the forum is not coloring the last is_state text red, not sure if that is significant(?)

Error:

ERROR (MainThread) [homeassistant.config] Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got 'is_state') for dictionary value @ data['sensors']['ap_convert']['value_template']. Got "{% if is_state('sensor.basement_bssid', '80:2A:A8:11:95:BF') %}\n  AP_AC_LITE\n{% elif is_state('sensor.basement_bssid', 'b4:fb:e4:46:89:53') %}\n  AP_AC_PRO\n{% else is_state('sensor.basement_bssid', 'f0:9f:c2:cb:3a:58') %}\n  AP_AC_LR\n{% endif %}\n\n# {% if is_state('sensor.sensor.basement_bssid', '80:2A:A8:11:95:BF') %} #   AP_AC_LITE # {% else is_state('sensor.basement_bssid', 'b4:fb:e4:46:89:53') %} #   AP_AC_PRO # {% elif is_state('sensor.basement_bssid', 'f0:9f:c2:cb:3a:58') %} #   A.... (See /config/sensor/template.yaml, line 80). Please check the docs at https://home-assistant.io/components/sensor.template/

My fault. It needs to be elif …

or else just by itself. Check the edit above.

- platform: template
    sensors:
      ap_convert:
        friendly_name: "Connected AP"
        value_template: >-
          {% if is_state('sensor.basement_bssid', '80:2A:A8:11:95:BF') %}
            AP_AC_LITE
          {% elif is_state('sensor.basement_bssid', 'b4:fb:e4:46:89:53') %}
            AP_AC_PRO
          {% else %}
            AP_AC_LR
          {% endif %}

Wow, you sir are a magician :bowing_man:
Now it works. Its been bugging me for hours. Thank you so much!

1 Like