Sensor Template Troubleshooting Help

Hey all, hoping someone can help with my template issues. I’ll start by saying I know very little about templates, I’ve just started using them recently and the template I’ve come up with so far is completely stolen from someone else and I can’t really explain why some lines of code exist or what they do, I taken something that works for someone else and tried to adapt it to be my own. The best I can do is explain what I’m trying to accomplish and show you the template that half works.

I’m using ESPresence to track which room my dog is in and it works great. I was getting a lot of “Not Home” messages displayed which started me down the road of using a sensor template. I found someone else who had created a template to say if the dog tracking sensor was reporting “not home” to use the last known state of the sensor instead.

I adopted this template and got it working but then I wanted to take it a step further and incorporate the state of my iphone so when we are on dog walks, or I take the dog with me to someone’s house, that the state of the sensor reflects this.

I’ve got the room tracking working on it’s own and I have the iPhone location tracking working on its own but when I try to combine the two templates together it doesn’t work. I’m pretty sure I’ve messed up the Else and Elif portion but obviously don’t know enough to know what’s wrong. When I put the following template into the developer tools I don’t get any errors and I have the same template in my config.yaml without issues either. The room tracking portion appears to be working and I haven’t noticed any “Not Home” states but when we go for a walk it’s just displaying the last known state.

It’s really hard to test this template because one of the states depends on GPS coordinates so every time I make a small change I have to go take the dog for a walk to see if it’s working then come back home and try again. In theory what I’m trying to do should be fairly simple for someone who knows what they doing, hoping someone can help me, I’m sure I’m just missing something small.

Here’s my YAML:

template:
  - sensor:
      - name: Monty Room
        icon: mdi:dog
        state: >-
          {% if states.sensor.monty.state != 'not_home' and states.device_tracker.ted_iphone.state == 'mom' %}
            Mom's House
          {% elif states.sensor.monty.state != 'not_home' and states.device_tracker.ted_iphone.state == 'park walk' %}
            Park Walk
          {% elif states.sensor.monty.state != 'not_home' and states.device_tracker.ted_iphone.state == 'marina walk' %}
            Marina Walk
          {% elif states.sensor.monty.state != 'not_home' and states.device_tracker.ted_iphone.state == 'jamie' %}
            Jamie's House
          {% else %}  
            {% if states.sensor.monty == 'not_home' %}
              {{ states.sensor.monty.state }}
            {% elif states.sensor.monty.state == 'bedroom' %}
              Bedroom
            {% elif states.sensor.monty.state == 'living_room' %}
              Living Room
            {% elif states.sensor.monty.state == 'bed' %}
              Bed
            {% elif states.sensor.monty.state == 'back_door' %}
              Back Door
            {% elif states.sensor.monty.state == 'theatre' %}
              Theatre
            {% elif states.sensor.monty.state == 'outside' %}
              Outside
            {% elif states.sensor.monty.state == 'office' %}
              Office
            {% elif states.sensor.monty.state == 'not_home' %}
              {{ states.sensor.monty_room.state }}
            {% endif %}
          {% endif %}

Any help is appreciated.

Cheers,

Ted

I think the best way is to create a trigger based sensor. If the device tracker changes location, set the state for the dog and so on

I might look into a trigger based sensor but I’ve almost got it working now and I’d have to learn how to create trigger based sensors LOL

maybe give it a try

{% set ted_iphone = states.device_tracker.ted_iphone.state %}
{% if states.sensor.monty.state == 'not_home' %}
  {{ ted_iphone }}
    {% if states.sensor.monty.state !== 'not_home'  and ted_iphone == 'home' %}
      {{ states.sensor.monty.state }}
  {% endif %}
{% endif %}

I think the issue is your use of

In order for the ESPresence-based state to not be “not_home”, then the dog has to be in some area of “home”… but he can’t be home and walking at the Marina with Ted at the same time.

I think this should cover all the cases:

template:
  - sensor:
      - name: Monty Room
        icon: mdi:dog
        state: >-
          {% set monty = states('sensor.monty') %}
          {% set ted = states('device_tracker.ted_iphone') %}
          {% set ted_and_monty = {
            "mom": "Mom's House", "park walk": "Park Walk",
            "marina walk": "Marina Walk", "jamie": "Jamie's House" } %}
          {% if monty == 'not_home' %}
            {{ ted_and_monty.get(ted) if ted in ted_and_monty.keys() else monty }}
          {% else %}
            {{ monty.replace('_',' ')|title }}            
          {% endif %}
        availability: "{{ has_value('device_tracker.ted_iphone') and has_value('sensor.monty') }}"

Hey Drew, I tried your template in dev tools and it’s producing the correct state when in the house, will have to go for a walk to see if it works with the GPS state of my phone.

One question, the resulting string in dev tools shows the state (Room) then underneath it says:

availability: “True”

Is that supposed to be there? What’s the purpose of this and is it going to show up in the history of this sensor or be displayed on a button card along with the room state?

Thanks again for the help, if this works on our walk I’ll send you money for a case of beers LOL

Cheers,

Ted

So I added your template to my YAML and it does display availability: “True” next to the room the dog is in, anyway to get rid of that?

The availability configuration variable defines the available state of the entity. If the template returns True, the entity will be available. If the template returns any other value, the entity will be unavailable. It helps prevent errors and unusable state values when the source entities for your main template are unavailable or unknown.

It should not do that. Did you set it up exactly as shown above… on it’s own line and indented to the same depth as state?

ahhh yes it was an indentation issue, I’ve fixed it now.

I’ll let you know if it works after we’ve gone for a walk.

Cheers,

Ted

1 Like

Unfortunately your template is displaying the state “not_home”. The initial reason for using a template was to get rid of seeing the state “not home” by replacing “not home” with the last known state. Your template does not seem to do that. Is there anyway it can be added?

Cheers,

Ted

Is there any case where you want it to return “not_home”? For example, what if Ted is “home” but Monty is “not_home”?

If that’s not a concern, the following should cover the criteria to hold the previous state value.

template:
  - sensor:
      - name: Monty Room
        icon: mdi:dog
        state: >-
          {% set current = this.state | default("error",1) %}
          {% set monty = states('sensor.monty') %}
          {% set ted = states('device_tracker.ted_iphone') %}
          {% set ted_and_monty = {
            "mom": "Mom's House", "park walk": "Park Walk",
            "marina walk": "Marina Walk", "jamie": "Jamie's House" } %}
          {% if monty == 'not_home' %}
            {{ ted_and_monty.get(ted) if ted in ted_and_monty.keys() else current }}
          {% else %}
            {{ monty.replace('_',' ') | title }}            
          {% endif %}
        availability: "{{ has_value('device_tracker.ted_iphone') and has_value('sensor.monty') }}"

Hey Drew, technically there would be a case for returning “not_home”, as in if the dog got loose, but ESPresence reports back too often with not_home, when he’s obviously home, so the number of false positives would be way too high.

I just tested the previous template by going for a walk and at first it wasn’t working, then I sat down on a park bench and reviewed the code vs the states and realized that the state was “Park Walk” when the code was looking for “park walk” and when I fixed the capital letters it started working, I would not have thought states were case sensitive.

I’m going to test this new code and report back after another dog walk. My poor bulldog is getting a lot more exercise than he’s used to.

Cheers,

Ted

If those false positives tend to be transient, it should be possible to add triggers with a duration requirement so that the “not_home” state only occurs when he is not home for an extended amount of time.

Dude you’re blowing my mind LOL, I’ve been trying to perfect this silly automation for over a year now, I only learned about templates the other day and now you’re making my wildest dreams come true.

I just tested the latest version of your code and I’m not getting “not_home” anymore even when the senors reads not home, nice! Now I just need to go for a second walk and test that portion.

It would be awesome to have an additional trigger for when the not_home state occurs for a long period of time but I’ll settle for what you’ve given me so far unless you want two cases of beer ha ha ha ha

It’s probably going to take some experimenting to figure out the most reliable combination of time and other conditional logic… but it would be something like:

template:
  - trigger:
      - platform: state
        entity_id: sensor.monty
        to: 'not_home'
        for: "00:10:00"
        id: escaped
      - platform: state
        entity_id: sensor.monty
        not_to: 'not_home'
      - platform: state
        entity_id: device_tracker.ted_iphone
    sensor:
      - name: Monty Room
        icon: mdi:dog
        state: >-
          {%- set current = this.state | default("error",1) %}
          {%- set monty = states('sensor.monty') %}
          {%- set ted = states('device_tracker.ted_iphone') %}
          {%- set ted_and_monty = {
            "mom": "Mom's House", "park walk": "Park Walk",
            "marina walk": "Marina Walk", "jamie": "Jamie's House" } %}
          {%- if monty == 'not_home' and trigger.id != 'escaped' %}
            {{- ted_and_monty.get(ted) if ted in ted_and_monty.keys() else current }}
          {%- elif trigger.id == 'escaped' %}
            {{ 'not_home' if ted == 'home' else current }}
          {%- else %}
            {{- monty.replace('_',' ') | title }}            
          {%- endif %}
        availability: "{{ has_value('device_tracker.ted_iphone') and has_value('sensor.monty') }}"

The parts you may need to finesse is the for: duration on the trigger and:

{%- elif trigger.id == 'escaped' %}
  {{ 'not_home' if ted == 'home' else current }}

Does your phone pick up his BT tag? That might be a useful condition for discriminating if it’s reliable.

oh wow! Ok maybe I’ll have to test this code next. Just about to go on a walk and test the last version, one hitch I’m having, I’m trying use your code to create an apple watch complication and it just shows an error which I can’t read all I can see is " { ERROR = “U…” the rest is cut off in the complication area.

I copied all this code exactly:

          {% set current = this.state | default("error",1) %}
          {% set monty = states('sensor.monty') %}
          {% set ted = states('device_tracker.ted_iphone') %}
          {% set ted_and_monty = {
            "Mom": "Mom's House", "Park Walk": "Park Walk",
            "Marina Walk": "Marina Walk", "Jamie": "Jamie's House" } %}
          {% if monty == 'not_home' %}
            {{ ted_and_monty.get(ted) if ted in ted_and_monty.keys() else current }}
          {% else %}
            {{ monty.replace('_',' ') | title }}            
          {% endif %}
        availability: "{{ has_value('device_tracker.ted_iphone') and has_value('sensor.monty') }}"

I made sure all the proper indentation was made and also tried lining everything up, neither seemed to work. Previously I was able to copy my template exactly for the apple watch complication but for some reason your code doesn’t want to work like before. Any ideas?

Cheers,

Ted

Or can I just grab the state of this new Monty Room sensor and display that in the apple watch complication instead of using the same template???

I’ve never used apple watch, so that’s out of my wheelhouse… can you just use the sensor.monty_room entity?

Hey Yes, i was just thinking the same thing, investigating that now…

Amazing! So yes I can just use {{ states(“sensor.monty_room”) }} for the apple watch complication instead of the whole template, this is so much easier, I’m an idiot…

I just went for a walk and confirmed everything is working now, I’m going to tinker with your not_home trigger code tonight and tomorrow and do some more testing. I’ll let you know how it goes… and thanks again for the help!

I do believe the phone picks up his tag, I’m using a Blue Charm beacon on his collar. I can access the beacon using the app on my phone and make changes to it’s settings but can you get that state information back to HA? I’ve seen some iPhone automations using Bluetooth but this might be too much for Siri and myself to figure out.

Cheers,

Ted