Building a better ESPresense. Question about sensor templates [SOLVED]

SHORT:
Should be easy… How do I template the previous state of a sensor to make a sensor that shows the previous state of another sensor?

LONG:
Use:
ESPresense is great and all, but lets be honest, it isn’t perfect. It frequently states not_home as the state of a beacon that is sitting right next to it.
Now, this is just a polling issue obviously. I have other sensors that tell if the beacon is home or not that work reliably. So while I am in the process of changing the room names in the state from media_room to Media Room because I am picky like that I realized I can fix ESPresense at the same time by making a template sensor for each of the BLE sensors that combines other sensors with it and gets rid of the false not_home by retaining the previous room state when ESPresense is reporting not_home.

I am using Tile Sticker to track my animals as they roam about the house for a few reasons - a major one being to track which cat is on the food bowl presence sensor so that I can track who is eating. We had one pass away and couldn’t figure out who wasn’t eating. So I became kinda obsessed with creating this sensor.

Anywho, the Tile integration tells me if the Tile is home, with accuracy, so I don’t need the ESPresense sensor just guessing, I need it knowing.

My template outline. THIS IS PROBABLY NOT FORMATTED NOR EVEN WRITTEN PROPERLY > JUST THE IDEA:

{% if states.device_tracker.tile.state != 'Home' %}
  'POSSIBLY LOOSE'  #Replace for your usage - probably 'Away' for creatures that are supposed to leave the house.
#For the phones with the companion app it just makes sense to show where they are anyway.
#{{ states.sensor.phone_geocoded_location.state }} 
{% elif states.sensor.tile.state == 'media_room' %} #from the MQTT ROOM sensor
  'Media Room'
#Repeat for each room to have proper title
(% elif states.sensor.tile.state == 'not_home' %}
  'states.sensor.sensor_with_previous_mqtt_sensor_state.state' ###THIS IS WHAT I NEED HELP TO CREATE!
{% endif %}

All in all this would completely fix ESPresense for me, I just can’t figure out how to make the sensor for the previous state.

Solved it with the solution here. Took a couple more hours, but I found it.

Edit: This was edited… SEVERAL HOURS and gallons of fuel later to make sure the tracker was set to not_home as the dev tools set state option gets overridden extremely fast.

Solution:

- sensor:
  - name: "Osiris Room"
    state: >-
      {% if states.device_tracker.osiris_tile.state != 'home' and states.sensor.osiris_collar.state == 'not_home' %}
        Possibly Loose
      {% else %}
        {% if states.sensor.osiris_collar.state == 'not_home' %}
          {{ states.sensor.osiris_room.state }}
        {% else %}
          {% if states.sensor.osiris_collar.state == 'media_room' %}
            Media Room
          {% elif states.sensor.osiris_collar.state == 'living_room' %}
            Living Room
          {% elif states.sensor.osiris_collar.state == 'bedroom' %}
            Bedroom
          {% elif states.sensor.osiris_collar.state == 'laundry_room' %}
            Laundry Room
          {% elif states.sensor.osiris_collar.state == 'gateroom' %}
            Gateroom
          {% elif states.sensor.osiris_collar.state == 'kitchen' %}
            Kitchen
          {% elif states.sensor.osiris_collar.state == 'dining_room' %}
            Dining Room
          {% elif states.sensor.osiris_collar.state == 'sun_room' %}
            Sun Room
          {% elif states.sensor.osiris_collar.state == 'office' %}
            Office
          {% elif states.sensor.osiris_collar.state == 'not_home' %}
            {{ states.sensor.osiris_room.state }}
          {% endif %}
        {% endif %}
      {% endif %}

The and condition speeds up the sensor’s detection of arrival, as the bt is faster than the tile.

I used this to get to the solution:

If anyone else tries out my solution for ESPresense, let me know and let me know if it works for you.

Oh man am I glad I found your post! I’ve been going nuts trying to improve ESPresense’s accuracy and just discovered sensor templates the other day, made myself a simple template but still had the “not home” issue. I don’t understand this stuff enough to do advanced templates like you have here and you’ve gone one step further than I did with those first few lines using the states of two sensors to give another result.

I’m trying to adapt what you’ve done for my own uses but can’t wrap my head around all this. I’ll explain my setup, show you my code and if you’d be willing to help I’d totally email you the money for a case of beer LOL.

I have a Blue Charm beacon on my dog’s collar (sensor.monty) and ESPresense Base Stations throughout my house, even one outside under the deck. I was just tracking the sensor and even had it displaying in a complication on my apple watch but got a lot of the “not home” message which has driven me crazy.

You’ve given me the idea to incorporate sensor data from tracking my iphone location (device_tracker.ted_iphone) to also show when me and the dog are at someone else’s house.

Here’s my attempt at reusing your code for my purposes but something isn’t working. I’m not sure how to have multiple If’s for the combined sensor states (the first few lines of code) and then I’m not sure how you get it to display the last known state instead of “not home”.

{% if states.sensor.monty != 'not_home' and states.device_tracker.ted_iphone == 'mom' %}
  Mom's House
{% elif states.sensor.monty != 'not_home' and states.device_tracker.ted_iphone == 'jamie' %}
  Jamie's House
{% else %}
  {% if states.sensor.monty == 'not_home' %}
          {{ states.sensor.monty.state }}
        {% else %}
          {% if states.sensor.monty == 'bedroom' %}
            Bedroom
          {% elif states.sensor.monty == 'living_room' %}
            Living Room
          {% elif states.sensor.monty == 'bed' %}
            Bed
          {% elif states.sensor.monty == 'back_door' %}
            Back Door
          {% elif states.sensor.monty == 'theatre' %}
            Theatre
          {% elif states.sensor.monty == 'outside' %}
            Outside
          {% elif states.sensor.monty == 'office' %}
            Office
          {% elif states.sensor.monty == 'not_home' %}
            {{ states.sensor.monty_room.state }}
          {% endif %}
        {% endif %}
      {% endif %}

I’m so close to having the solution I’ve been working on for months, any help would be greatly appreciated.

Cheers,

Ted

Glad I could help inspire! (Kinda an ego boost that you called it advanced. I learned to do all of this either via the docs or help from the community and adapting it as you are.)
I believe your endif is misaligned. Also I think you need the .state on your sensor for each room. I’d start there.

oh you might be on to something with the missing .state! This appears to be working now in the developer tools template section, I’ll need to take the dog to someone’s house and monitor the sensor over the next 24 hours but I think I might finally be rid of seeing Not Home displayed.

And now I should be able to use the same template to improve on my apple watch complication, if you have an apple watch I highly suggest doing this as well, it’s pretty cool to be able to look down at my watch and know what room the dog is in.

I’ll report back and let you know how the testing goes.

Cheers,

Ted

I’ve got it on my Galaxy Watch. I don’t use the watch for the beacon though due to it BURNS the battery, I always have my phone anyway. I went ahead and combined the ESPresense with a ESPHome pressure sensor made from sketch paper, aluminum foil, a lamp wire, and a thin piece of whiteboard. When the pressure sensor senses that it is pressed for more than 15 seconds it records in a template sensor whichever cats are currently in the room they eat in (it’s a laundry room) I made one that reports all at the same time and 3 separate sensors to be able to track when each individual cat eats. From there I set an automation that sends me a notification on my phone if any of them go more than 48 hours without eating. We had one of our cats stop eating and pass away, we couldn’t figure out which one was sick. I decided this was the best prevention for future.

How’s that beacon btw? How large is it/how fast does it update on ESPresense?

Oh also, I use Room Assistant too. Idk why I did both but I’ve been comparing accuracy between them.

The Blue Charm beacons are great! Updates in seconds and can be configured to only update on motion to save battery. Nearly all settings can be changed including the Beacon’s TX power.

I bought the BC08 model because it’s waterproof and then glued it to my dog’s collar. It’s fairly large (twice the size of the Tile Sticker tracker I had before) but not too large for my bulldog. It would be too large on a cat or small dog but they make other models of beacons that appear to be smaller, but not waterproof, which would be fine if your cat is an indoor cat.

Cheers,

Ted

1 Like

We have the BC04P now which is a smaller diameter than the BC08, also waterproof, and has flanges with holes, so probably easier to attached to a dog’s collar.

I agree that both of these are probably too big for most cats, except maybe a big ol’ Maine coon.

For indoor cats, my favorite is the BC021. I attach it to my cats’ collars with a little metal loop thingy, but you could also use one of these AirTag holder things from Amazon: https://bluecharmbeacons.com/ibeacon-holder-for-pet-collars/

I use beacons on my three indoor cats to control their food intake. Otherwise, one of them could/can/would easily munch down ALL of the food from three different bowls. Now they each have their own bluetooth-controlled bowls that only open for the right owner cat. I used BC011s before but now switched to BC021s since they are a bit tougher. Here’s a short video of Ollie’s bowl in action:

This is awesome. I’m gonna have to get 4 of those now I have 2 dogs and for some reason all 3 cats and the other dog like to eat one of the dogs food. She grazes like a cat so it’d be nice to be able to not lock her in the bathroom when it is food time.

I should also mention the support from Blue Charm is excellent!

I submitted a support request earlier this week regarding this issue and Thomas reached out right away with some suggestions and I’m guessing he monitors these forums as well.

Thomas, I haven’t tried all of your suggestions yet, I’m not so worried about calibration now that I’ve figured out sensor templates and found a way to get rid of the “Not Home” message.

As for the template sensor, the section handling rooms appears to be working and I don’t see Not Home displayed anymore, but the more advanced first few lines of the template where it uses two sensors doesn’t seem to be working and I can’t figure out why. I just went for a walk and the dog’s sensor was “not home” and my phone showed as “park walk” but the template generated no text, just blank. Here’s my template again:

{% if states.sensor.monty != 'not_home' and states.device_tracker.ted_iphone == 'park walk' %}
            Park Walk
          {% elif states.sensor.monty != 'not_home' and states.device_tracker.ted_iphone == 'marina walk' %}
            Marina Walk
          {% elif states.sensor.monty != 'not_home' and states.device_tracker.ted_iphone == 'mom' %}
            Mom's House
          {% elif states.sensor.monty != 'not_home' and states.device_tracker.ted_iphone == 'jamie' %}
            Jamie's House
          {% else %}
            {% if states.sensor.monty == 'not_home' %}
                {{ states.sensor.monty.state }}
              {% else %}
                {% if 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 %}
            {% endif %}

I find it odd that the section of the template handling room works and the top section doesn’t. Can someone please tell me what I’ve done wrong LOL.

Cheers,

Ted

1 Like

I’m 99% sure it’s spacing issues.

I’m in the middle of cleaning right now but I’ll see if I can show you what I mean after.

Elif should line up with if

I custom made these about 7 years ago. The box is basic woodwork stuff. The (old) tech is an Arduino plus an HM-10 BLE module. If I was to rebuild them today, I would definitely use an ESP instead. Easier to code, plus I could easily add notifications/records via wifi in order to keep track of the number of times each cat was eating, etc. (as you mentioned you wanted in your earlier comment).

The code I used is completely outdated since there are now excellent standard free libraries for ESP for scanning for beacons.

The sliding door mechanism took some time to figure out. I wanted something simple that would not cost too much. Ended up doing this:

The motor is a metal gear stepper motor with a little metal arm attached and a pivot point that runs along a groove in an acrylic panel. That groove was cut using a router and gradually increasing the depth of the cut over several passes.

Been running for 7 years now without a problem. I also have a little rotating switch on the back that I can use to increase or decrease the distance range of the opening/closing. This helps to fine tune it, especially when I might move the bowls to new locations such as when we moved to our current home two years ago.

2 Likes

Ok I’ve tried lining everything up better and also adding .state to the end of everything (I think I missed some), we’ll see how that goes.

Another thing you could do is try the different pieces of your template selarately. Remove what you know works and if the remainder still doesn’t work, you’ve narrowed down where it is having problems.

So I went to my Mom’s house and tested the portion of the template that handles when I’m not home and got that to display I was at my Mom’s. Then I added the portion of the template that handles the rooms at home and the template was still working, showing I was at my mom’s. Then I went home and checked the template and it’s still showing I’m at my mom’s.

Why can I only get one portion to work at a time? How do I combine these two together and make it work like you did? I 've tried lining every up and it doesn’t seem to make a difference, I’ve also tried lining things up the exact same way that you do I it doesn’t make a difference. This is driving me crazy.

It’s very hard to test because the portion handing outside my house is triggered by GPS on my phone so I physically have to be at one of these locations to make it work. I keep changing the code a bit, taking the dog for a walk and seeing if it works then changing some more code and going for another walk… this is going to take forever to troubleshoot.

Hmm. Hopefully it helps to know I also had issues when I made this template. Iirc I think ultimately the non-room portion will work if you do “else” statements instead of “elif” but I’m not sure how to make yours into else statements since it’s more ifs than mine is.