I have a esphome device working as a distance-measurement-device that i need to send initial data on startup and then only upon meeting a threshold.
I use the the code below today but the problem is that on device restart, the device doesn’t send any data until the threshold is met. So sensor state will forever be unavailable until anything comes in sight.
@jackjourneyman Thank you for replying.
The thing is that I need the ESP-device to handle this on it own so that it’s not sending data via WiFi on every single difference in measurement. Which tend to be a lot with a ultrasonic sensor. I only want it to send data when necessary.
Worth mentioning is also that I haven’t written the code.
I’m not very good at coding ESP32s. I found it somewhere online and tweaked it for my purpose.
No, native API isn’t the solution.
I have a working code. I just need it to send data upon device restart.
The ESP-device (sensor) needs to update Home Assistant almost instantly so the update interval can’t be more then a second, preferably a lot less. Sending data that often is not a problem to bandwidth for as long as this sensor is alone in the network. But it’s not. Another problem with sending all live data from the sensor wound be that it is not all accurate and it might give Home Assistant the false impression that the object is closer or further away then it is for just a second. But that can potentially lead to a failure that this sensor is created to prevent. That’s why I also need the filtering in the code.
I really don’t understand your concerns. Any technical reasons/links you could provide which support your vague claims?
We run over a 100 of esphome nodes and some (like pulse counters) delivery quite astonishing amount of data without any notacible impact on HA or the network itself. We also have ultra sonic sensors in use and can even stream high-res video content over the same (old) wifi
Just wonder if we have a x-y problem here.
The XY problem is a communication problem encountered in help desk, technical support, software engineering, or customer service situations where the question is about an end user’s attempted solution rather than the root problem itself. The XY problem obscures the real issues and may even introduce secondary problems that lead to miscommunication, resource mismanagement, and sub-par solutions.
And just in case (not clear at all yet with the little information you gave) the goal is to achieve some (very) resilent setup - you don’t want to rely on HA or WIFI (no guaranteed airtime!) and - in best case - have all logic on the esphome node (sensor and actors)
How are you sending the data at all? Our esphome based ultrasonic sensors always send the first value after start up/connection which seems to be the default behavior with native api
It’s still total unclear to me (and probably many others - hence no one replies?) what your goal is or what the problem is that you are trying to solve. You don’t describe/picture what the problem is you are trying to overcome nor what your config/yaml looks like. Without being rude it really looks like the typical x-y problem we have here
The goal is totally irrelevant. I don’t need to explain what the sensor is for in this case, I provided the information you need.
A sensor that has the value “unknown” at startup is the problem.
The workaround is the wave in front of the sensor so that the filter threshold is met and and trigger it to send data.
Without the filter in the code it send data on startup but I need the filter in the code (I have been running the sensor without it for a long time) and the problem I have then is that sensor value fluctuating to much that an object that is in front of the sensor al of a sudden is gone for about a second.
The filter has, as mentioned, helped with this and works very well.
Posting big pictures like that can seem a bit rude, Yes!
And so you did it again.
If it’s no solution. Fine. It has taken more time then it was worth already.
And it wasn’t a nice chat either.
We can’t know as you did not provide the minimum information necessary to evaluate this (full yaml). More over you claimed in one of your post that the native “isn’t the solution” which can be interpreted that you don’t use it.
Nope, not even the basics. Classic X-Y problem…
That’s totally different from everything you told us so far.
Please don’t be sad but instead wasting more time trying helping you I’d rather use it smarter and help 10 others that know how to ask a good question
You actually wasted all our time by not providing necessary information even when asked multiple times.
Good luck
PS: Minimum two possible solutions are floating in my head
@Grepen with all due respect, you need to re-evaluate how you behave on these forums. The first reply to you was not rude, yet you claimed it was. Also @orange-assistant’s 2nd reply to you wasn’t rude… everything after that is up for debate. As an outside observer, the rudeness didn’t appear until after you mentioned rude replies in both instances.
I think the answer was in the first response you got. The question was why you wrote your own filter instead of using the standard delta filter. Had you looked it up you would have seen it. It probably does not contain the bug you have in your filter lambda.
It is ok that you know little about the code, but then you should not dismiss any answer you get. If you do not understand the intent of the answer, ask.
the problem with your code is that the first measurement is stored in the static variable, but not returned. This:
last_value = x;
return {};
Should be this:
last_value = x;
return {x};
But most likely, had you asked what the first post was about, you would have done this (I have not tried it, but is unlikely it contains the same bug):