Setting a MQTT sensor value to 0 if the sensor stops broadcasting

I have a door position BLE beacon broadcasting when the door is closed, and a BLE scanner capturing the rssi value and sending it to the MQTT integration on HA to use for automations. Everything works fine.

But when the door is open, the sensor stops broadcasting completely (as it should), so then the HA just keeps the last value of the rssi signal for that beacon.

What I would like instead is to make a value template for the MQTT sensor such that if the last rssi value received from the beacon is older than 10 seconds old, then the sensor value should be set at 0.

I have pasted the code I am trying below for your reference (and amusement :grin:). The code “sorta works” in that the rssi of that beacon shows as 0 but it does not change back to the live rssi value once the door is closed. In other words, the if statement is constantly being satisfied even when the door is closed.

- name: "rssiDoorBeaconClosed"
      unique_id: sensor.rssidoorbeaconclosed
      state_topic: "bluecharm/publish/BC5729052ED3"
      unit_of_measurement: "dBm"
      value_template: >-
            {% set targetUuidFound = namespace(found=False) %}
                {% for i in range(value_json.obj|count) -%}
                    {% if value_json.obj[i].uuid == "426C7565436861726D42BBBBBBBBBBBB" and targetUuidFound.found == False %}
                        {{ value_json.obj[i].rssi }}
                        {% set targetUuidFound.found = True %}
                    {%- endif %}
                {%- endfor %}
            {% set targetUuidFound = namespace(found=False) %}
#the stuff above works fine so you can ignore it for now; the stuff below is my attempt to solve my current challenge
            {% if ((as_timestamp(now()) - as_timestamp(states.sensor.rssidoorbeaconclosed.last_changed)) > 10) %}
                {{ 0 }}
            {%- endif %}

Add an else to that last if and return this value?

No, that part grabs the live rssi when the beacon is broadcasting. Since the door is now open, the beacon stops broadcasting. But the HA still thinks the beacon’s rssi is -67 (for example), and it never changes until the beacon begins broadcasting again.

I want the sensor yaml to “time out” after ten seconds when the beacon is no longer broadcasting new data, and then the code should set the beacon rssi to some artificial number like zero. My automation code can then use a 0 rssi as a trigger or condition for something.

The only way I can think of right now is to make a trigger-based template binary sensor using a time pattern trigger. In the template, you’ll need to check the last updated property.

I do something like this for a similar case where I want to check that a connection has dropped:


  - trigger:
    - platform: time_pattern
      seconds: "/10"
    binary_sensor:
      - name: "SolarAssistant Activity"
        state: >-
          {{ states.sensor.solarassistant_activity_counter.last_updated >= now() - timedelta(seconds=10) }}
        device_class: running
1 Like

Hmmmm…just looking at that, it seems like it should work.

I am not very familiar with HA, so let me figure out how to make a trigger, and I will let you know if I get it to work.

Thanks for the suggestion! :+1:

Make a template sensor for this RSSI value and adapt my example.

OK, I finally figured it out. Thanks of the brain stimulation. Something you wrote finally made me wake up!

So, my original (wrong) idea was to insert a placeholder value for the rssi of the door beacon when the “door is open” beacon was not broadcasting. Then the automation look for that artificial placeholder value to know that the door was currently closed. I was trying to accomplish this by adding a value template to the actual MQTT sensor. Finally, realized that idea was dumb, and didn’t work no matter how I tried to code it.

Then the lightbulb in my brain turned on, and I realized all I had to do was to add a value template condition to the automation, rather than add a value template to the sensor.

So, for example, I added a value template condition to the “close the door” automation to check if the door was currently open. So the door would be triggered to close only if the condition “door is now open” was met. I determined the door state with a value template condition that checked how long ago the “door is open” beacon broadcast was last seen. If it was seen less than ten seconds ago, then the door open beacon is still broadcasting, and thus the door is open; so the automation can close it now (if the automation trigger and other conditions are also met). Basically, added this condition to the door closing automation:

Thanks!

Great!

Yeah, I kept to your original requirement of a sensor, but you can totally do what you did – saves you an unnecessary entity.

1 Like