Help Improving Occupancy

Hi,

I have a fairly complicated occupancy method that Im looking to enhance. It uses a Bayesian sensor for every room, along with a template sensor to track how long each motion sensor has been on.

Im looking to solve two problems:

  1. Last changed also occurs when the sensors move from ‘on’ to ‘off’. In my template sensor I only really want to count the time since the sensor was last on.

  2. I want to ‘reset’ the occupancy somehow when i set the alarm. When the alarm is set most of the sensors are already off, so that isnt an issue, but the timers are generally still withing the periods where they are showing occupancy. What I want is to make sure that the next sensor triggers occupancy on, but when the alarm is set the occupancy shows as off… the most basic way to do this would be to add 900 seconds to the timers (as a one off) so that the 15 minute time included in a Bayesian has expired but Im pretty sure this is impossible… so im looking for a clever work around.

Any ideas appreciated.

Bayesian:

  - platform: 'bayesian'
    name: 'Bedroom Occupied'
    prior: 0.25
    probability_threshold: 0.85
    observations:
      - entity_id: 'binary_sensor.bedroom_main_motion_sensor'
        prob_given_true: 1
        prob_given_false: 0
        platform: 'state'
        to_state: 'on'
      - entity_id: 'sensor.bedroom_main_motion_time'
        prob_given_true: 0.95
        platform: 'numeric_state'
        below: 900

timer:

  bedroom_main_motion_time:
    friendly_name: "Bedroom Last Motion"
    value_template: '{{(as_timestamp(now())-as_timestamp(states.binary_sensor.bedroom_main_motion_sensor.last_changed))|round(1)}}'

maybe include alarm mode in the bayesian ?