Automation does not work when CPU wakes up if sensor goes out of range during deep sleep

Good evening everyone.

I’m here to ask for some clarification on something that is causing me problems.

I have an ESP32 Wroom which has to control voltages and temperatures and if the temperature (or voltage) thresholds are exceeded, a relay must be energized or de-energized.
Everything works perfectly until everything happens during the ESP wake cycle but if the temperature or voltage goes outside the thresholds during the deep sleep cycle, the relay does not energize, as if it does not recognize that the sensor is out theshold.
Has this happened to anyone before?? And if so, how did you solve it??

I enclose a copy of the code with the part of the sensors and automation

Thanks for any suggestion you can give me

Latest Home assistant
Latest ESPHome
ESP 32 Wroom 32 from AZDelivery

#SPECIFIC SENSORS FOR THIS ESP --------------------------------

  - platform: dallas
    address: 0xe13c1ef649a5f228
    name: "${friendly_name} Temperatura Batteria Servizi"
    id: eisbar_esp_temp_batt_serv
    unit_of_measurement: "°C"
    icon: "mdi:water-percent"
    device_class: "temperature"
    state_class: "measurement"
    accuracy_decimals: 1
    #expire_after: 10min
    on_value_range:
        - above: 25.00
          then:
            - switch.turn_on: relay1
            - switch.turn_on: all_temp_batt_servizi
            - text_sensor.template.publish:
                id: all_temp_batt_serv
                state: "ALM"
            - deep_sleep.prevent: deep_sleep_1
        - below: 24.99
          then:
            - switch.turn_off: relay1
            - text_sensor.template.publish:
                id: all_temp_batt_serv                
                state: "OK"
            - delay: 30000ms    
            - deep_sleep.allow: deep_sleep_1         

  - platform: dallas
    address: 0x203cf5f649908128  
    name: "${friendly_name} Temperatura Batteria Motore"
    id: eisbar_esp_temp_batt_mot
    unit_of_measurement: "°C"
    icon: "mdi:water-percent"
    device_class: "temperature"
    state_class: "measurement"
    accuracy_decimals: 1
    #expire_after: 10min
    on_value_range:
        - above: 40.00
          then:
            - switch.turn_on: relay1
            - switch.turn_on: all_temp_batt_motore 
            - text_sensor.template.publish:
                id: all_temp_batt_mot
                state: "ALM"
            - deep_sleep.prevent: deep_sleep_1
        - below: 39.99
          then:
            - switch.turn_off: relay1
            - text_sensor.template.publish:
                id: all_temp_batt_mot
                state: "OK"
            - delay: 30000ms    
            - deep_sleep.allow: deep_sleep_1         


  
  - platform: shtcx
    temperature:
      name: "${friendly_name} Temperatura"
      id: eisbar_esp_temperatura
    humidity:
      name: "${friendly_name} Umidità"
      id: eisbar_esp_umidita
    address: 0x70
    update_interval: 5s
    


  - platform: adc
    pin: GPIO32
    name: "${friendly_name} Tensione Pannelli Solari"
    id: pannelli_solari
    attenuation: 11db 
    update_interval: 5s 
    filters:
      - multiply: 10.57
    

  - platform: adc
    pin: GPIO33
    name: "${friendly_name} Tensione Batteria Motore"
    id: batteria_motore
    attenuation: 11db 
    update_interval: 5s 
    filters:
      - multiply: 10.57
    on_value_range:
        - above: 14.8
          then:
            - switch.turn_on: relay1
            - switch.turn_on: all_tens_batt_motore
            - text_sensor.template.publish:
                id: all_tens_batt_mot
                state: "ALM"
            - deep_sleep.prevent: deep_sleep_1
        - below: 13.5
          then:
            - switch.turn_off: relay1
            - text_sensor.template.publish:
                id: all_tens_batt_mot
                state: "OK" 
            - delay: 30000ms    
            - deep_sleep.allow: deep_sleep_1           

  - platform: adc
    pin: GPIO34
    name: "${friendly_name} Tensione Batteria Servizi"
    id: batteria_servizi
    attenuation: 11db 
    update_interval: 5s 
    filters:
      - multiply: 10.57
    on_value_range:
        - above: 14.8
          then:
            - switch.turn_on: relay1
            - switch.turn_on: all_tens_batt_servizi
            - text_sensor.template.publish:
                id: all_tens_batt_serv
                state: "ALM"   
            - deep_sleep.prevent: deep_sleep_1
        - below: 13.5
          then:
            - switch.turn_off: relay1
            - text_sensor.template.publish:
                id: all_tens_batt_serv
                state: "OK"
            - delay: 30000ms    
            - deep_sleep.allow: deep_sleep_1     


deep_sleep:
  id: deep_sleep_1
  run_duration: 2min
  sleep_duration: 8min

Nothing happens when the CPU is asleep. It is powered down.

I probably wasn’t clear in my explanation.

It is obvious that nothing happens during the deep switch cycle, but if a sensor goes out of threshold during the deep switch phase, as soon as it wakes up it should identify the out of threshold and activate the corresponding automation, which does not.
I noticed that if the out of range persists for two deep sleep cycles, then in the second cycle it recognizes the out of range and activates the automation.

To try to be more 'clear I changed the title

Another suggestion??

Thanks

https://esphome.io/components/sensor/index.html#on-value-range

With this automation you can observe if a sensor value passes from outside a defined range of values to inside a range.

The automations only trigger when the above/below values are crossed. If the sensors are already above/below those points nothing happens. All data is lost on sleep so the ESP has no idea what the values were before it went to sleep, just what they are now. Thus no crossing of the range → no trigger.

You could use an on_boot trigger instead/as well.

Or you could use the on_value trigger (triggers on every sensor update) with conditions.

2 Likes

For this kind of thing I have a preference for creating template binary sensors for each on/off range (rather than attaching them to the sensor), then I drive the actions from the binary sensor.
I find this easier to manage and debug, and gives you a summary sensor you can keep an eye on in HA.

You can mark them as internal or diagnostic once everything is working well if you want to declutter.

You could also look at moving your actions to a script or even a template switch to reduce code repetition (since they appear to be the same?).

Edit: I see now the actions aren’t quite the same (on mobile,) But maybe still review?

Or Send toggle state on wake up from deep sleep mode esp32, how to do it? - #2 by tom_l , i.e. check values on boot / wakeup (which is a good idea anyway, I guess).

NVM, saw you mentioned it

Following Tom’s suggestion I completely rewrote the automation using on_value and a lambda expression for the condition and now everything works perfectly. Below is the code of a typical automation I used.

Thanks to everybody


on_value:
        then:
          - if:
              condition:                
                lambda: 'return id(eisbar_esp_temp_batt_serv).state > 25;'
              then:
                - switch.turn_on: relay1
                - switch.turn_on: all_temp_batt_servizi
                - text_sensor.template.publish:
                    id: all_temp_batt_serv
                    state: "ALM"   
                - deep_sleep.prevent: deep_sleep_1
              else:
                - text_sensor.template.publish:
                    id: all_temp_batt_serv
                    state: "OK"
                - if:
                    condition:
                      and:                
                         - text_sensor.state:
                             id: all_temp_batt_serv
                             state: 'OK'
                         - text_sensor.state:
                             id: all_temp_batt_mot
                             state: 'OK'
                         - text_sensor.state:
                             id: all_tens_batt_serv
                             state: 'OK'
                         - text_sensor.state:
                             id: all_tens_batt_mot
                             state: 'OK'
                    then:
                      - switch.turn_off: relay1

                      - delay: 30000ms    
                      - deep_sleep.allow: deep_sleep_1 
1 Like