Esphome time and sensor based automation

Hi all,

I’m trying to setup a infrared heater in the bathroom with a temp/humidity sensor (AM2302) and some conditions for it to turn on/off. One of those conditions is time (i.e the hour). I’m failing in getting useable info from the time component. I’m open for alternative like nested if’s or conditionals, i’m not very skilled in this code to know what’s possible.

time:
  - platform: homeassistant
    id: ha_time
    timezone: Europe/Amsterdam
    on_time_sync:
      then:
        - logger.log: "Synchronized ha clock"
  - platform: sntp
    id: sntp_time
    timezone: Europe/Amsterdam
    on_time_sync:
      then:
        - logger.log: "Synchronized sntp clock"
    
sensor:
  - platform: dht
    model: AM2302
    pin: D5
    temperature:
      name: "Badkamer Temperatuur"
      accuracy_decimals: 1
      on_value_range:
        - below: 20
          then:
            - logger.log: "temp below"
            - if:
                condition:
                  or:
                    - lambda: 'return id(ha_time).now().hour > 23;'
                    - lambda: 'return id(sntp_time).now().hour > 23;'
                then:
                  - logger.log: "Turning on heated mirror"
                  #- switch.turn_on: R4
                else:
                  - logger.log: "Or condition not valid"
                  - lambda: !lambda |-
                      time_t currTime = id(sntp_time).now().timestamp;
                      ESP_LOGW("main", "Current Time: %f", currTime);
                      currTime = id(ha_time).now().timestamp;
                      ESP_LOGW("main", "Current Time: %f", currTime);
        - above: 22
          then:
            - logger.log: "temp above"
            - if:
                condition:
                  or:
                    - lambda: 'return id(ha_time).now().hour > 23;'
                    - lambda: 'return id(sntp_time).now().hour > 23;'
                then:
                  - logger.log: "Turning off heated mirror"
                  #- switch.turn_on: R4
                else:
                  - logger.log: "Or condition not valid"
                  - lambda: !lambda |-
                      time_t currTime = id(sntp_time).now().timestamp;
                      ESP_LOGW("main", "Current Time: %f", currTime);
                      currTime = id(ha_time).now().timestamp;
                      ESP_LOGW("main", "Current Time: %f", currTime);
    humidity:
      name: "Badkamer Luchtvochtigheid"
      accuracy_decimals: 0
      on_value_range:
        - below: 60
          then:
            - switch.turn_on: R1
            - delay: 500ms
            - switch.turn_off: R1
            - logger.log: "Turning off Fan"
        - above: 62
          below: 65
          then:
            - switch.turn_on: R2
            - delay: 500ms
            - switch.turn_off: R2
            - logger.log: "Turning on Fan (low)"
        - above: 67
          then:
            - switch.turn_on: R3
            - delay: 500ms
            - switch.turn_off: R3
            - logger.log: "Turning on Fan (high)"
    update_interval: 10s

I would love to hear suggestions from the real coders/expert, i’m lost.

Thanks for your time.

1 Like