mezz64 / pyEight

Python library to interface with the Eight Sleep API
MIT License
59 stars 15 forks source link

Bed Presence update #7

Open rafale77 opened 5 years ago

rafale77 commented 5 years ago

I am still seeing this intermittent problem on home assistant where all the data is updated except for the bed presence sensor turning off. It does not appear to be an API issue because as soon as I restart home assistant, the sensor turns off. Turning it on does not appear to be a problem either. It is very specific to getting the home assistant sensor to turn off when the API has it off.

mezz64 commented 5 years ago

Please provide a snapshot of the reported bed level the next time it happens. That is the value used internally by pyEight to figure out whether or not someone is in bed.

rafale77 commented 5 years ago

sorry, I've been out. I will provide it to you as soon as I reproduce the problem (it is intermittent)

rafale77 commented 5 years ago

Just had the problem: I am getting it down to 36% and it still shows I am in bed while I have been long out of bed. Maybe the determination should be based on the slope (derivative) of the drop over a few points? Screen Shot 2019-06-10 at 7 38 44 AM

Somehow if I restart Hass, It immediately shows the sensor to be off even though the value is 35% vs 36%? Is there a hysteresis between the on and off value?

mezz64 commented 5 years ago

I use the delta over the last 4 readings (I fetch updated data once a minute so we don't hammer the Eight API) and a threshold of being below 50% to indicate a change from in bed to out of bed. In early testing I found anything more aggressive resulted in false triggers while in bed.

In this scenario, does it ever switch to out of bed? If so, how long does it take?

rafale77 commented 5 years ago

Hmm it's interesting. I found that the detection of in bed presence is pretty good but it is the switch out which is unreliable. As you can see on the previous day, it worked pretty well. I found that in many cases, it never switch out even though the level is already very low. Strangely the absolute threshold does not work. I have observed mine to go down to 35% and still indicating the sensor to be on.

rafale77 commented 5 years ago

Screen Shot 2019-06-17 at 8 11 56 AM

see the problem here

mezz64 commented 5 years ago

It's not an absolute threshold, it only starts to check the deltas once the sensor goes below 50%. You will never see it change prior to that.

This is the portion of code in action. The fail-safe at the moment is 15% or less to switch off no matter what. Does that scenario work for you?

elif self.presence:
            if self.heating_level <= 15:
                # Failsafe, very slow
                self.presence = False
            elif self.heating_level < 50:
                if self.past_heating_level(0) - self.past_heating_level(1) < 0 \
                    and self.past_heating_level(1) - self.past_heating_level(2) < 0 \
                        and self.past_heating_level(2) - self.past_heating_level(3) < 0:
                    # Values are decreasing so we are likely out of bed
                    self.presence = False
rafale77 commented 5 years ago

15% seems a bit high... and it seems like are looking for 3 consecutive value decrease it appears once it gets below 50% but what I have observed is that the value still bounces up and down (say 40-41-39-38-40) at the frequency it is being pinged so even if over time it is decreasing it still fails to untrip the presence sensor. You can see that the slope is much sharper when it is above 50. Maybe you could build a logic where one of the 3 can increase but 0-3 still is <0?