How do I use a sensor attribute in a Bayesian binary sensor?

I want to use sun.sun.attributes.elevation in my Bayesian binary sensor to more effectively calculate the likelihood of it actually being dark enough to turn on the living room lights.

Ideally, this would work like the value_template elevation example here

But, I don’t think the Bayesian binary sensor supports value templates. Any suggestions on how to do this?

If value template was supported this is what I want (with several other observations of course):

- platform: bayesian
  name: 'It''s dark out'
  prior: 0.5
  observations:
  - platform: 'numeric_state'
    entity_id: 'sun.sun'
    value_template: '{{ state.attributes.elevation }}'
    prob_given_true: 1
    prob_given_false: 0.25
    below: 3.75
1 Like

You can use a sensor platform: template in there …

From sensors.yaml

- platform: template
      sensors:
        sun_state:
          value_template: '{% if is_state("sun.sun", "above_horizon") %}up{% else %}down{% endif %}'
          friendly_name: 'Sun state'  
 
From binary_sensors.yaml

- platform: 'bayesian'
  name: 'in_bed'
  prior: 0.75
  probability_threshold: 0.95
  observations:
- entity_id: 'switch.pilivingroompir'
  prob_given_true: 0.25
  prob_given_false: 0.75
  platform: 'state'
  to_state: 'on'
- entity_id: 'switch.pibedroompir'
  prob_given_true: 0.65
  prob_given_false: 0.35
  platform: 'state'
  to_state: 'on'
- entity_id: 'sensor.sun_state'
  prob_given_true: 0.60
  prob_given_false: 0.40
  platform: 'state'
  to_state: 'down'
- entity_id: 'binary_sensor.bedtime'
  prob_given_true: 0.80
  prob_given_false: 0.20
  platform: 'state'
  to_state: 'true'
  
Maybe it can help.
1 Like

That might work. I’ll give that a shot, thanks!

just stumbled upon your thread while looking for some inspiration on the bayesian sensor. If you want to discriminate between different day phases (day-dusk-night-dawn) for your probability calculation, this thread might be of help: