What is wrong with this template

When it does “work” or have no errors I just keep getting a loading symbol when I check the config. I don’t understand the "expected token ‘,’,’ " part of the error.

- platform: template
    sensors:
      sam_bt:    
        friendly_name: 'Sam BT Beacon'
        value_template: >
          {% if is_state('person.sam', 'home') and
           is_state('sensor.sam_s_beacon', 'not_home') -%}
            Home
          {% elif is_state('person.sam', 'home') and
          is_state('sensor.sam_s_beacon', 'kitchen') -%}
            kitchen
          {% elif is_state('person.sam', 'home') and
           is_state('sensor.sam_s_beacon', 'bedroom') -%}
          bedroom
          {% elif is_state('person.sam', 'home') and
           is_state('sensor.sam_s_beacon', 'living_room') -%}
          Living Room
          {% elif is_state('person.sam', 'home') and
           is_state('sensor.sam_s_beacon', 'east_bedroom') -%}
          East Bedroom
          {% elif is_state('person.sam', 'not_home') and
           is_state('sensor.sam_s_beacon', 'not_home') -%}
          Not Home   
          {%- endif %}
          
  - platform: template
    sensors:
      golf_cart_bt:    
        friendly_name: 'Golf Cart Keys'
        value_template: >
          {% if is_state('device_tracker.golf_cart_keys_e2a7', 'home') and
           is_state('sensor.golf_cart_key_beacon', 'not_home') -%}
            Home
          {% elif is_state('device_tracker.golf_cart_keys_e2a7', 'home') and
          is_state('sensor.golf_cart_key_beacon', 'kitchen') -%}
            kitchen
          {% elif is_state('device_tracker.golf_cart_keys_e2a7', 'home') and
           is_state('sensor.golf_cart_key_beacon', 'bedroom') -%}
          bedroom
          {% elif is_state('device_tracker.golf_cart_keys_e2a7', 'home') and
           is_state('sensor.golf_cart_key_beacon', 'living_room') -%}
          Living Room
          {% elif is_state('device_tracker.golf_cart_keys_e2a7', 'home') and
           is_state('sensor.golf_cart_key_beacon', 'east_bedroom') -%}
          East Bedroom
          {% elif is_state('device_tracker.golf_cart_keys_e2a7', 'away) and
           is_state('sensor.golf_cart_key_beacon', 'not_home') -%}
          Not Home    
          {%- endif %}                      

You’re missing a ' after away in the 3rd line from the bottom… and that should be not_home, not away.

You can simplify your templates considerably by using variables and rearranging the clauses:

  - platform: template
    sensors:
      sam_bt:    
        friendly_name: 'Sam BT Beacon'
        value_template: >
          {% set sam_home = is_state('person.sam', 'home') %}
          {% set beacon = states('sensor.sam_s_beacon') %}
          {% if not sam_home %}
            Not Home
          {% elif sam_home and beacon == "not_home" %}
            Home
          {% else %}
            {{ beacon | replace('_', ' ') | title }}
          {%- endif %}

  - platform: template
    sensors:
      golf_cart_bt:    
        friendly_name: 'Golf Cart Keys'
        value_template: >
          {% set tracker_home = is_state('device_tracker.golf_cart_keys_e2a7', 'home') %}
          {% set beacon = states('sensor.golf_cart_key_beacon') %}
          {% if not tracker_home %}
            Not Home
          {% elif tracker_home and beacon == "not_home" %}
            Home
          {% else %}
            {{ beacon | replace('_', ' ') | title }}
          {%- endif %}
1 Like

I missed that to :wink: :smiley:

omg… I’ve been looking over this since last night and going crazy. It’s always some little detail that takes a fresh set of eyes to spot.

maybe someone can answer this too. Whats the difference between using individual templates like the above example and doing this? I have several of these to do and not sure if one way is better or is right or wrong. Both ways do the same thing and work. Is it more for organization and keeping sensors grouped under one template opposed to individual?

- platform: template
    sensors:
      sam_bt:    
        friendly_name: 'Sam BT Beacon'
        unique_id: sam_bt_beacon
        value_template: >
          {% if is_state('person.sam', 'home') and
           is_state('sensor.sam_s_beacon', 'not_home') -%}
            Home
          {% elif is_state('person.sam', 'home') and
          is_state('sensor.sam_s_beacon', 'kitchen') -%}
            kitchen
          {% elif is_state('person.sam', 'home') and
           is_state('sensor.sam_s_beacon', 'bedroom') -%}
          bedroom
          {% elif is_state('person.sam', 'home') and
           is_state('sensor.sam_s_beacon', 'living_room') -%}
          Living Room
          {% elif is_state('person.sam', 'home') and
           is_state('sensor.sam_s_beacon', 'east_bedroom') -%}
          East Bedroom
          {% elif is_state('person.sam', 'not_home') and
           is_state('sensor.sam_s_beacon', 'not_home') -%}
          Not Home   
          {%- endif %}
          
      golf_cart_bt:
        friendly_name: 'Golf Cart BT'
        unique_id: golf_cart_key_bt
        value_template: >
          {% if is_state('device_tracker.golf_cart_keys_e2a7', 'home') and
           is_state('sensor.golf_cart_key_beacon', 'not_home') -%}
            Home
          {% elif is_state('device_tracker.golf_cart_keys_e2a7', 'home') and
          is_state('sensor.golf_cart_key_beacon', 'kitchen') -%}
            kitchen
          {% elif is_state('device_tracker.golf_cart_keys_e2a7', 'home') and
           is_state('sensor.golf_cart_key_beacon', 'bedroom') -%}
          bedroom
          {% elif is_state('device_tracker.golf_cart_keys_e2a7', 'home') and
           is_state('sensor.golf_cart_key_beacon', 'living_room') -%}
          Living Room
          {% elif is_state('device_tracker.golf_cart_keys_e2a7', 'home') and
           is_state('sensor.golf_cart_key_beacon', 'east_bedroom') -%}
          East Bedroom
          {% elif is_state('device_tracker.golf_cart_keys_e2a7', 'away') and
           is_state('sensor.golf_cart_key_beacon', 'not_home') -%}
          Not Home    
          {%- endif %}              

Basically both are “wrong” , or old way

template:
   - sensor:
        - name:

ya, I tried using that format to begin with and it was an even bigger mess.

1 Like

yeah, time to add template-sensors in !include_dir_*, haven’t got there yet, but it’s high time for me to, to organize that way

my whole config is organized in individual !includes


ok, i mend in dirs, splitting files
Splitting up the configuration - Home Assistant.

yes… they’re split up, that’s what the pictures are pointing out so i’m not sure why you keep posting the same link as if I don’t understand. Do you not see all the different files? scenes? switches? sensors? mqtt sensors? input select? etc. etc. Those are the individual files that are broken out of config.yaml
Screenshot from 2024-02-19 15-15-38

Well, why don’t you read the link ? , you might find out that i mean " Split your templates Sensors into small files ! " I.E.

/my-template-folder/
justins-room.yaml
mqtt-room.yaml
basement.yaml
Etc. Etc.

That what i link to, and mend to … so yes somehow i do get the feeling that you don’t understand, now that you mention it
The benefit of reading the link could result in you get cleaned up in you /config ( /homeassistant ) , it really looks like it’s needed

If you look closely at your forum post the string highlighting in red gives you a hint too:

Screenshot 2024-02-20 at 08-49-21 What is wrong with this template - Home Assistant Community

I have them split up exactly how I want and I don’t want them split up by room or into 25 different folders because, that’s ridiculous. Do you understand that?

I see the missing ’ I assume that’s what you’re talking about.

Yes but no. Look at the colours of the text that error produces.

I see it, im just not sure what the issue is… fixing the ’ and i get no errors and the sensors work… what exactly are you trying to point out?

That if you looked closely at what the forum highlighted you could have found the error yourself.

That the forum syntax highlighting can help you.

Nothing is wrong with either of them. It’s just a different way to organize them in your config.

there is also nothing wrong with using the “old” way of creating template sensors as you have done.

The only thing is that the “old” way won’t get any updated functionality. But if they work as you want tyhen just keep using them like they are.

But if at some point you want one to use some new functionality (state_class etc) then you can convert it at that time.

If it was me I wouldn’t worry about it.