esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
293 stars 36 forks source link

Analog Threshold Binary Sensor should validate thresholds #3149

Open TBobsin opened 2 years ago

TBobsin commented 2 years ago

The problem

Hello, i am testing the new function with an Wemos using the analogue input. The first problem is: The State of the binary Sensor is flipping at every reading even if the Value is constant. If i try to stabilise/flatten the Sensor with filter: delayed_on: 6s but the Sensor is still flipping its state. Second Problem is: The State of lower Sensor is false. in my case the Sensor "state off" and "state heat" is on if the Voltage is above the given Values. I think it shoud be off if the value is out of range.

if input voltage is 1v "state hot" is flipping "state heat" is on "state off" is on

if input voltage is 0.5V "state hot" off "state heat" is flipping "state off" is on

The input is connected to an Voltage divider with three Resistors see simple scheme below. VCC R

R +> --v-------- to Analogue in of Wemos R C 100n | | GND

I also tried to use an Capacitor. But the Electronic seems not to be the problem because the Voltage Reading is stable. (see log)

Which version of ESPHome has the issue?

2022.3.0

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

does not matter

What platform are you using?

ESP8266

Board

Wemos D1

Component causing the issue

Analog Threshold Binary Sensor

Example YAML snippet

My config is
  - platform: adc
    pin: A0
    name: "Testsensor"
    update_interval: 5s
    id: Volt

  - platform: analog_threshold
    name: "state off"
    sensor_id: Volt
    threshold:
      upper: 0
      lower: 0.26
  - platform: analog_threshold
    name: "state heat"
    sensor_id: Volt
    threshold:
      upper: 0.27
      lower: 0.78
  - platform: analog_threshold
    name: "state hot"
    sensor_id: Volt
    threshold:
      upper: 0.79
      lower: 0.9

Anything in the logs that might be useful for us?

17:43:35    [I] [ota:105]   
Boot seems successful, resetting boot loop counter.
17:43:35    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.81348 V with 2 decimals of accuracy
17:43:35    [D] [binary_sensor:036] 
'state hot': Sending state ON
17:43:40    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.81445 V with 2 decimals of accuracy
17:43:40    [D] [binary_sensor:036] 
'state hot': Sending state OFF
17:43:45    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.81348 V with 2 decimals of accuracy
17:43:45    [D] [binary_sensor:036] 
'state hot': Sending state ON
17:43:50    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.81348 V with 2 decimals of accuracy
17:43:50    [D] [binary_sensor:036] 
'state hot': Sending state OFF
17:43:55    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.81445 V with 2 decimals of accuracy
17:43:55    [D] [binary_sensor:036] 
'state hot': Sending state ON
17:44:00    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.81445 V with 2 decimals of accuracy
17:44:00    [D] [binary_sensor:036] 
'state hot': Sending state OFF
17:44:05    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.81445 V with 2 decimals of accuracy
17:44:05    [D] [binary_sensor:036] 
'state hot': Sending state ON
17:44:10    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.81445 V with 2 decimals of accuracy
17:44:10    [D] [binary_sensor:036] 
'state hot': Sending state OFF
17:44:15    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.81445 V with 2 decimals of accuracy
17:44:17    [D] [binary_sensor:036] 
'state hot': Sending state ON
17:48:31    [D] [binary_sensor:036] 
'state hot': Sending state OFF
17:48:35    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.42578 V with 2 decimals of accuracy
17:48:35    [D] [binary_sensor:036] 
'state heat-cool': Sending state ON
17:48:40    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.42578 V with 2 decimals of accuracy
17:48:40    [D] [binary_sensor:036] 
'state heat-cool': Sending state OFF
17:48:45    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.42578 V with 2 decimals of accuracy
17:48:45    [D] [binary_sensor:036] 
'state heat-cool': Sending state ON
17:48:50    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.42578 V with 2 decimals of accuracy
17:48:50    [D] [binary_sensor:036] 
'state heat-cool': Sending state OFF
17:48:55    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.42578 V with 2 decimals of accuracy
17:48:55    [D] [binary_sensor:036] 
'state heat-cool': Sending state ON
17:49:00    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.42578 V with 2 decimals of accuracy
17:49:00    [D] [binary_sensor:036] 
'state heat-cool': Sending state OFF

### with Filter delayed_on/Off 6s (5s readings)
18:10:01    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.28320 V with 2 decimals of accuracy
18:10:06    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.28223 V with 2 decimals of accuracy
18:10:07    [D] [binary_sensor:036] 
'state heat-cool': Sending state ON
18:10:11    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.28320 V with 2 decimals of accuracy
18:10:16    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.28223 V with 2 decimals of accuracy
18:10:17    [D] [binary_sensor:036] 
'state heat-cool': Sending state OFF
18:10:21    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.28223 V with 2 decimals of accuracy
18:10:26    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.28223 V with 2 decimals of accuracy
18:10:27    [D] [binary_sensor:036] 
'state heat-cool': Sending state ON
18:10:32    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.28320 V with 2 decimals of accuracy
18:10:36    [D] [sensor:124]    
'Jura Testsensor': Sending state 0.28220 V with 2 decimals of accuracy
18:10:37    [D] [binary_sensor:036] 
'state heat-cool': Sending state OFF

Additional information

No response

TBobsin commented 2 years ago

I could see the same problem with an Dallas Sensor

TBobsin commented 2 years ago

I upgraded to version 2022.5.0 the error is still there, see last lines of the log.

INFO Reading configuration /config/esphome/espjura.yaml...
INFO Starting log output from espjura using esphome API
INFO Successfully connected to espjura
[12:32:13][I][app:102]: ESPHome version 2022.5.0 compiled on May 19 2022, 12:17:48
[12:32:13][C][wifi:491]: WiFi:
[12:32:13][C][wifi:353]:   Local MAC: 30:83:98:A2:A5:1F
[12:32:13][C][wifi:354]:   SSID: 'TIBOLILE'[redacted]
[12:32:13][C][wifi:355]:   IP Address: 192.168.159.20
[12:32:13][C][wifi:356]:   BSSID: 24:5A:4C:1A:CF:29[redacted]
[12:32:13][C][wifi:358]:   Hostname: 'espjura'
[12:32:13][C][wifi:360]:   Signal strength: -61 dB ▂▄▆█
[12:32:13][C][wifi:364]:   Channel: 6
[12:32:13][C][wifi:365]:   Subnet: 255.255.255.0
[12:32:13][C][wifi:366]:   Gateway: 192.168.159.1
[12:32:13][C][wifi:367]:   DNS1: 192.168.159.10
[12:32:13][C][wifi:368]:   DNS2: 0.0.0.0
[12:32:13][C][logger:274]: Logger:
[12:32:13][C][logger:275]:   Level: DEBUG
[12:32:13][C][logger:276]:   Log Baud Rate: 115200
[12:32:13][C][logger:277]:   Hardware UART: UART0
[12:32:13][C][uptime.sensor:031]: Uptime Sensor 'Internal Uptime'
[12:32:13][C][uptime.sensor:031]:   State Class: 'total_increasing'
[12:32:13][C][uptime.sensor:031]:   Unit of Measurement: 's'
[12:32:13][C][uptime.sensor:031]:   Accuracy Decimals: 0
[12:32:13][C][uptime.sensor:031]:   Icon: 'mdi:timer-outline'
[12:32:13][C][template.sensor:023]: Template Sensor 'Jura Uptime Timestamp'
[12:32:13][C][template.sensor:023]:   Device Class: 'timestamp'
[12:32:13][C][template.sensor:023]:   State Class: ''
[12:32:13][C][template.sensor:023]:   Unit of Measurement: ''
[12:32:13][C][template.sensor:023]:   Accuracy Decimals: 0
[12:32:13][C][template.sensor:024]:   Update Interval: never
[12:32:13][C][template.text_sensor:021]: Template Sensor 'Jura Uptime'
[12:32:13][C][template.text_sensor:021]:   Icon: 'mdi:clock-start'
[12:32:13][C][switch.gpio:050]: GPIO Switch 'Jura Relais'
[12:32:13][C][switch.gpio:051]:   Pin: GPIO12
[12:32:13][C][switch.gpio:073]:   Restore Mode: Restore (Defaults to OFF)
[12:32:13][C][template.switch:058]: Template Switch 'Jura An'
[12:32:13][C][template.switch:059]:   Restore State: NO
[12:32:13][C][template.switch:060]:   Optimistic: YES
[12:32:13][C][template.switch:058]: Template Switch 'Jura aufheizen'
[12:32:13][C][template.switch:059]:   Restore State: NO
[12:32:13][C][template.switch:060]:   Optimistic: YES
[12:32:13][C][template.switch:058]: Template Switch 'Jura abkühlen'
[12:32:13][C][template.switch:059]:   Restore State: NO
[12:32:13][C][template.switch:060]:   Optimistic: YES
[12:32:13][C][esp8266_pwm:022]: ESP8266 PWM:
[12:32:13][C][esp8266_pwm:023]:   Pin: GPIO13
[12:32:13][C][esp8266_pwm:024]:   Frequency: 1000.0 Hz
[12:32:13][C][esp8266_pwm:025]:   Inverted: YES
[12:32:13][C][esp8266_pwm:022]: ESP8266 PWM:
[12:32:13][C][esp8266_pwm:023]:   Pin: GPIO2
[12:32:13][C][esp8266_pwm:024]:   Frequency: 1000.0 Hz
[12:32:13][C][esp8266_pwm:025]:   Inverted: YES
[12:32:13][C][uptime.sensor:031]: Uptime Sensor 'Jura Uptime Seconds'
[12:32:13][C][uptime.sensor:031]:   State Class: 'total_increasing'
[12:32:13][C][uptime.sensor:031]:   Unit of Measurement: 's'
[12:32:13][C][uptime.sensor:031]:   Accuracy Decimals: 0
[12:32:13][C][uptime.sensor:031]:   Icon: 'mdi:timer-outline'
[12:32:13][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Jura Taster'
[12:32:13][C][gpio.binary_sensor:016]:   Pin: GPIO0
[12:32:13][C][light:104]: Light 'Jura BlueLED Light'
[12:32:13][C][light:106]:   Default Transition Length: 1.0s
[12:32:13][C][light:107]:   Gamma Correct: 2.80
[12:32:13][C][light:104]: Light 'Jura LaserLED Light'
[12:32:13][C][light:106]:   Default Transition Length: 1.0s
[12:32:13][C][light:107]:   Gamma Correct: 2.80
[12:32:13][C][adc:062]: ADC Sensor 'Jura Testsensor'
[12:32:13][C][adc:062]:   Device Class: 'voltage'
[12:32:13][C][adc:062]:   State Class: 'measurement'
[12:32:13][C][adc:062]:   Unit of Measurement: 'V'
[12:32:13][C][adc:062]:   Accuracy Decimals: 2
[12:32:13][C][adc:067]:   Pin: GPIO17
[12:32:13][C][adc:094]:   Update Interval: 5.0s
[12:32:13][C][wifi_info:013]: WifiInfo Mac Address 'Jura Wifi MAC'
[12:32:13][C][version.text_sensor:021]: Version Text Sensor 'Jura ESP Version'
[12:32:13][C][version.text_sensor:021]:   Icon: 'mdi:wrench'
[12:32:13][C][status:034]: Status Binary Sensor 'Status Jura'
[12:32:13][C][status:034]:   Device Class: 'connectivity'
[12:32:13][C][analog_threshold.binary_sensor:033]: Analog Threshold Binary Sensor 'state off'
[12:32:14][C][analog_threshold.binary_sensor:034]:   Sensor 'Jura Testsensor'
[12:32:14][C][analog_threshold.binary_sensor:034]:     Device Class: 'voltage'
[12:32:14][C][analog_threshold.binary_sensor:034]:     State Class: 'measurement'
[12:32:14][C][analog_threshold.binary_sensor:034]:     Unit of Measurement: 'V'
[12:32:14][C][analog_threshold.binary_sensor:034]:     Accuracy Decimals: 2
[12:32:14][C][analog_threshold.binary_sensor:035]:   Upper threshold: 0.00000000000
[12:32:14][C][analog_threshold.binary_sensor:036]:   Lower threshold: 0.25999999046
[12:32:14][C][analog_threshold.binary_sensor:033]: Analog Threshold Binary Sensor 'state heat-cool'
[12:32:14][C][analog_threshold.binary_sensor:034]:   Sensor 'Jura Testsensor'
[12:32:14][C][analog_threshold.binary_sensor:034]:     Device Class: 'voltage'
[12:32:14][C][analog_threshold.binary_sensor:034]:     State Class: 'measurement'
[12:32:14][C][analog_threshold.binary_sensor:034]:     Unit of Measurement: 'V'
[12:32:14][C][analog_threshold.binary_sensor:034]:     Accuracy Decimals: 2
[12:32:14][C][analog_threshold.binary_sensor:035]:   Upper threshold: 0.27000001073
[12:32:14][C][analog_threshold.binary_sensor:036]:   Lower threshold: 0.77999997139
[12:32:14][C][analog_threshold.binary_sensor:033]: Analog Threshold Binary Sensor 'state hot'
[12:32:14][C][analog_threshold.binary_sensor:034]:   Sensor 'Jura Testsensor'
[12:32:14][C][analog_threshold.binary_sensor:034]:     Device Class: 'voltage'
[12:32:14][C][analog_threshold.binary_sensor:034]:     State Class: 'measurement'
[12:32:14][C][analog_threshold.binary_sensor:034]:     Unit of Measurement: 'V'
[12:32:14][C][analog_threshold.binary_sensor:034]:     Accuracy Decimals: 2
[12:32:14][C][analog_threshold.binary_sensor:035]:   Upper threshold: 0.79000002146
[12:32:14][C][analog_threshold.binary_sensor:036]:   Lower threshold: 0.89999997616
[12:32:14][C][restart:022]: Restart Switch 'Jura Restart'
[12:32:14][C][restart:022]:   Icon: 'mdi:restart'
[12:32:14][C][captive_portal:088]: Captive Portal:
[12:32:14][C][web_server:129]: Web Server:
[12:32:14][C][web_server:130]:   Address: espjura:80
[12:32:14][C][mdns:084]: mDNS:
[12:32:14][C][mdns:085]:   Hostname: espjura9
[12:32:14][C][ota:085]: Over-The-Air Updates:
[12:32:14][C][ota:086]:   Address: espjura:8266
[12:32:14][C][ota:089]:   Using Password.
[12:32:14][C][api:138]: API Server:
[12:32:14][C][api:139]:   Address: espjura:6053
[12:32:14][C][api:143]:   Using noise encryption: NO
[12:32:14][C][wifi_signal.sensor:009]: WiFi Signal 'Jura WiFi Signal'
[12:32:14][C][wifi_signal.sensor:009]:   Device Class: 'signal_strength'
[12:32:14][C][wifi_signal.sensor:009]:   State Class: 'measurement'
[12:32:14][C][wifi_signal.sensor:009]:   Unit of Measurement: 'dBm'
[12:32:14][C][wifi_signal.sensor:009]:   Accuracy Decimals: 0
[12:32:14][C][wifi_info:009]: WifiInfo IPAddress 'Jura Wifi IP'
[12:32:14][C][wifi_info:011]: WifiInfo SSID 'Jura Wifi SSID'
[12:32:14][C][sntp:050]: SNTP Time:
[12:32:14][C][sntp:051]:   Server 1: 'ntp'
[12:32:14][C][sntp:052]:   Server 2: '192.168.159.10'
[12:32:14][C][sntp:053]:   Server 3: '2.pool.ntp.org'
[12:32:14][C][sntp:054]:   Timezone: 'CET-1CEST,M3.5.0,M10.5.0/3'
[12:32:14][C][debug:049]: Debug component:
[12:32:14][C][debug:054]:   Free space on heap 'Jura Heap Free'
[12:32:14][C][debug:054]:     State Class: ''
[12:32:14][C][debug:054]:     Unit of Measurement: 'B'
[12:32:14][C][debug:054]:     Accuracy Decimals: 0
[12:32:14][C][debug:054]:     Icon: 'mdi:counter'
[12:32:14][C][debug:055]:   Largest free heap block 'Jura Heap Max Block'
[12:32:14][C][debug:055]:     State Class: ''
[12:32:14][C][debug:055]:     Unit of Measurement: 'B'
[12:32:14][C][debug:055]:     Accuracy Decimals: 0
[12:32:14][C][debug:055]:     Icon: 'mdi:counter'
[12:32:14][D][debug:061]: ESPHome version 2022.5.0
[12:32:14][D][debug:065]: Free Heap Size: 20040 bytes
[12:32:14][D][debug:093]: Flash Chip: Size=1024kB Speed=40MHz Mode=DOUT
[12:32:14][D][debug:250]: Chip ID: 0x00A2A51F
[12:32:14][D][debug:251]: SDK Version: 2.2.2-dev(38a443e)
[12:32:14][D][debug:252]: Core Version: 3.0.2
[12:32:14][D][debug:253]: Boot Version=31 Mode=1
[12:32:14][D][debug:254]: CPU Frequency: 80
[12:32:14][D][debug:255]: Flash Chip ID=0x001640D8
[12:32:14][D][debug:256]: Reset Reason: Software/System restart
[12:32:14][D][debug:257]: Reset Info: Software/System restart
[12:32:16][D][sensor:124]: 'Jura Testsensor': Sending state 0.28418 V with 2 decimals of accuracy
[12:32:21][D][sensor:124]: 'Jura Testsensor': Sending state 0.28418 V with 2 decimals of accuracy
[12:32:22][D][binary_sensor:036]: 'state heat-cool': Sending state ON
[12:32:26][D][sensor:124]: 'Jura Testsensor': Sending state 0.28418 V with 2 decimals of accuracy
[12:32:27][D][text_sensor:067]: 'Jura Uptime': Sending state '31s'
[12:32:27][D][sensor:124]: 'Jura Uptime Seconds': Sending state 30.52200 s with 0 decimals of accuracy
[12:32:31][D][sensor:124]: 'Jura Testsensor': Sending state 0.28418 V with 2 decimals of accuracy
[12:32:33][D][sntp:074]: Synchronized time: 2022-05-19 12:32:31
[12:32:33][D][sensor:124]: 'Jura Uptime Timestamp': Sending state 1652956288.00000  with 0 decimals of accuracy
[12:32:33][D][binary_sensor:036]: 'state heat-cool': Sending state OFF
[12:32:33][D][sensor:124]: 'Jura WiFi Signal': Sending state -60.00000 dBm with 0 decimals of accuracy
[12:32:36][D][sensor:124]: 'Jura Testsensor': Sending state 0.28418 V with 2 decimals of accuracy
[12:32:40][D][sensor:124]: 'Jura Heap Free': Sending state 22976.00000 B with 0 decimals of accuracy
[12:32:40][D][sensor:124]: 'Jura Heap Max Block': Sending state 18120.00000 B with 0 decimals of accuracy
[12:32:40][D][sensor:124]: 'Jura Loop Time': Sending state 4264.00000 ms with 0 decimals of accuracy
[12:32:41][D][sensor:124]: 'Jura Testsensor': Sending state 0.**28418** V with 2 decimals of accuracy
[12:32:42][D][sensor:124]: 'Internal Uptime': Sending state 45.86300 s with 0 decimals of accuracy

### here you can see the value is stable but the state is changing:
[12:32:42][D][binary_sensor:036]: 'state heat-cool': Sending `state ON`
[12:32:46][D][sensor:124]: 'Jura Testsensor': Sending state 0.**28418** V with 2 decimals of accuracy
[12:32:51][D][sensor:124]: 'Jura Testsensor': Sending state 0.**28418** V with 2 decimals of accuracy
[12:32:52][D][binary_sensor:036]: 'state heat-cool': Sending `state OFF`
[12:32:56][D][sensor:124]: 'Jura Testsensor': Sending state 0.**28418** V with 2 decimals of accuracy
[12:33:01][D][sensor:124]: 'Jura Testsensor': Sending state 0.**28418** V with 2 decimals of accuracy
[12:33:02][D][binary_sensor:036]: 'state heat-cool': Sending `state ON`
jesserockz commented 2 years ago

Your upper and lower thresholds are backwards... There probably should be a config validation to enforce this

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Hohenloherin commented 2 years ago

@TBobsin Did the change of upper and lower thresholds solve your issue?

TBobsin commented 2 years ago

Hello, jesserockz commented on 20 May Your upper and lower thresholds are backwards... I changed the Values. The first Problem is solved. The result doesn't flip any more. if input voltage is 1v "state hot" is flipping is solved by changing the values "state heat" is on "state off" is on

if input voltage is 0.5V "state hot" off "state heat" is flipping is solved by changing the values "state heat" is on "state off" is on

second problem is not solved

I have updated the esphome version to 2022.10.2 There is still the problem, that the thresholds not correct. If an Value is outside the range it should not be true.

if input voltage is 1v "state hot" is off <- ok, because 1V is above 0.99 "state heat" is on <- not ok because not in range of definition (1V is more than 0.78) "state off" is on <- not ok because not in range of definition (1V is more than 0.26)

if input voltage is 0.5V "state hot" off <- ok "state heat" is on <- right state because 0.5V is above 0.26 an lower 0.78 "state off" is on <- should be off because the input value is outside Definition

My code

  - platform: adc
    pin: A0
    name: "Testsensor"
    update_interval: 5s
    id: Volt
  - platform: analog_threshold
    name: "state off"
    sensor_id: Volt
    threshold:
      upper: 0.26
      lower: 0
  - platform: analog_threshold
    name: "state heat"
    sensor_id: Volt
    threshold:
      upper: 0.78
      lower: 0.26
  - platform: analog_threshold
    name: "state hot"
    sensor_id: Volt
    threshold:
      upper: 0.9
      lower: 0.79
ssieb commented 2 years ago

I think you need to re-read the description of how it works because you are misunderstanding it. The threshold values are for creating hysteresis, not for defining the range.

patfelst commented 2 years ago

Please note if you use the invert filter, which I need to do when setting the device class to garage_door, the flip "bug" reappears. In this case you DO need to swap the upper and lower thresholds. i.e. this yaml code works ok:

sensor:
  - platform: esp32_hall
    name: "ESP32 Hall Sensor"
    id: magnet
    update_interval: 500ms
    internal: True
    filters:
      - median:
          window_size: 4
          send_every: 4

  - platform: analog_threshold
    name: "Garage Door Magnet"
    sensor_id: magnet
    device_class: garage_door
    filters:
      - invert: # to invert garage open/close, you need to swap the upper and lower thresholds
    threshold:
      upper: 140 # magnet is far from ESP32, magnet value has to be below to transition garage door to open
      lower: 170 # magnet is close to ESP32, magnet value has to be above to transition garage door to closed
    on_press: 
      then:
        # magnet is far from ESP32, Garage door is open
    on_release: 
      then:
        # magnet is close to ESP32, Garage door is closed