Self learning sensor

Hi

I am interested on how do go about setting a sensor that will predict most likely time of the day that it will be on. What i am trying to accomplish is that my garage doors lights are linked to door sensors. But i out them to turn lights off when 5 min have passed. My wife wants to have them every 20 min but only need it in AM. Now i can think of multiple use cases where it would be nice that i don’t have to explicitly tell what time of the day it should be 20 min vs the default 5 min. Is there a way to program the sensor to study it’s own behavior and based on historical data turn on for 20 min when lots of in an out activities in the house and then default to usual otherwise. This seems like AI project but don’t have the experience with machine learning but not sure if any ideas on how to make the sensor know by itself when should be most active without specifically hard coding the time.

From what you are writing I believe it is much easier to add a few motion sensors and some not-tto-smart automation. But possibly you are trying to keep it relatively simple (appreciated…seen too many posts with massive text)

If ever this rises, I would love to use AI but not for predicting a combi of motion/light… that makes no sense (to me)

Taking Home Assistant to the next level. Home Assistant… Assistant? - AI-powered Machine Learning for HA - Development - Home Assistant Community (home-assistant.io)

It doesn’t seem so much “AI” as it does just an average. You would have to log the times with recorder and then use a script to go through each on/off cycle to calculate the “average time”, then you could stuff that into a helper and tie the helper to an automation to determine the best time to turn on and off. You could also do this inside a sensor entity that is triggered by any on/off by that device to keep a log of and then calculate the best times, and then tie that sensor into other automation or scripts to know when something needs to go on, stay on, etc.

Also have a look at the statistics sensors … they will ‘just’ give you their particular information and this still means you have to entangle this in your setup but there is a lot out there, just not the AI (yet)

Statistics - Home Assistant (home-assistant.io)

I agree this use case not very glamorous. Another that comes to mind. I have some bed presence sensors detection at home. I am sending MQTT messages to HA every 1-3 seconds. But sensors only need to be on transmitting at very rapid rate at the time we go to bed otherwise it can just transmit maybe every hour. That way i could make my sensors battery operated instead of having my kids trip over the cables now and then. Having sensor transmit every second only when most likely needed will save tons of batteries.

In your first post when you used the word “sensor”, did you actually mean a sensor in HA or did you mean some random device? I had assumed you meant an HA sensor. But now you wrote this:

Obviously this is a device, not an HA sensor. HA has no control over how often some random device in your house transmits updates btw. If you have a problem with how frequently it updates you should take it up with the device manufacturer, the software its running or get a new device. No amount of machine learning in HA can affect this.

So either this is an entirely unrelated issue or you were actually talking about an issue you have with some device in your house in the first post as well. In that case please share the details of the device. But also feature requests for a piece of hardware should probably be submitted to the device’s manufacturer.

Side note, if you are looking for suggestions on how to make a battery operated bed presence detector, try this one:

Or if you don’t have slats and your bed has a firm bottom then this one may work:

Although beds are a lot thicker then chairs so YMMV. I think some folks tried it in that thread though.

To answer your question about the bed sensor specifically. I did it with similar approach to the seat sensor device you quoted. I used a FSR connected to a D1 mini WeMos that i programed to send MQTT messages to a particular internal. So i am able to modify the frequency of updates as needed.

If you are using ESPHome then the use of the delta filter can considerably reduce network traffic. e.g.

sensor:
  - platform: adc
    pin: GPIO34
    attenuation: 11db
    name: "Master Bed Sensor"
    id: "master_bed_sensor"
    icon: mdi:bed
    update_interval: 0.5s
    filters:
      - sliding_window_moving_average:
          window_size: 10
          send_every: 1
      - or:
          - throttle: 180s
          - delta: 0.02

This sensor updates every 0.5 seconds. It then passes that value to an average of 10 samples, this average output updates every 0.5 seconds too (sliding window, send every 1 sample). However the use of the ORed throttle and delta filters have the following effect: The sensor will only send an update to home assistant every 3 minutes (180 sec) but if the sensor average changes above the experimentally determined noise floor (0.02V) it will send a value immediately.

This way I get a value from my bed sensor every 3 minutes, but if I get into or out of bed (or roll around in bed) I get a new value within 1 to 2 seconds (depends how much the changed signal affected the 10 sample average).

I could probably increase the delta value to send less values at night but this would slow down the initial in/out of bed response a bit too. It’s a compromise.

1 Like

Do you think this will allow it to be battery operated with reasonable lifespan?