EspHome Automation Using time as condition

I have my bathroom fans connected with sonoffs and DHTs and they turn on/off automatically based on the humidity. Everything works great. However, some times the fans turn on in the middle of the night and that gets annoying. I am trying to find an automation that restricts the fan turning on by time. How can I add a time condition where the fan only turns on between 7 am and 10 pm?

Any help would be appreciated. Thanks,

  - platform: dht
    pin: GPIO3 
    model: DHT11
    temperature:
      name: "${devicename} Temperature"
      #id: ${devicename}_temperature
    humidity:
      name: "${devicename} Humidity"
      #id: ${devicename}_humidity
      on_value_range:
       - above: 66.0
          then:
            - switch.turn_on: switch_relay
        - below: 65.0
          then:
            - switch.turn_off: switch_relay
    update_interval: 60s

First add this to get time from Home Assistant:

# Example configuration entry
time:
  - platform: homeassistant
    id: homeassistant_time

Then, you’ll need to add a reference to this in the automation. Here is a link to the explanation of the time component in ESPHome. It contains ways to use the time component in automation.

Cheers!
DeadEnd

Thanks for your reply but I already checked that documentation. I can easily use the Time component to implement the automation using only the time parameter. BUT what I want to do it combine both parameters (time and humidity) to turn on/off the fan. I hope this makes sense.

So what you have to do is use the ID of the time sensor to reference it in your on_value_range automation. I believe you’ll want to use an if statment:

I don’t have experience doing this, so this is just my guess - but should be close (check indentation… did some copy pasting):

  - platform: dht
    pin: GPIO3 
    model: DHT11
    temperature:
      name: "${devicename} Temperature"
      #id: ${devicename}_temperature
    humidity:
      name: "${devicename} Humidity"
      #id: ${devicename}_humidity
      on_value_range:
       - above: 66.0
          then:
            - if:
              condition:
                lambda: 'return id(homeassistant_time).hour > 22;' #After 10pm
              then:
                - switch.turn_on: switch_relay
        - below: 65.0
          then:
            - switch.turn_off: switch_relay
    update_interval: 60s

You’d probably need to add a second if statement for the time being before 8am for example… or however you want to do the timing… this should be close and I think you can get it working from there…

Cheers!
DeadEnd

1 Like

Yes, this is exactly the information I was looking for. I will try it later. Thanks again for your help!!

I have checked the documentation and have tried different ways but it does not compile because of the ‘if’ ‘condition’ under the on_value_rage. Any ideas?

The error is:

Cannot have two actions in one item. Key ‘if’ overrides ‘then’

  - platform: dht
    pin: GPIO3 
    model: DHT11
    temperature:
      name: "${devicename} Temperature"
      #id: ${devicename}_temperature
    humidity:
      name: "${devicename} Humidity"
      #id: ${devicename}_humidity
      on_value_range:
        - above: 66.0
          then:
            - if:
                condition:
                  lambda: |-
                    return id(homeassistant_time).hour < 22;
              then:
                - switch.turn_on: switch_relay
        - below: 65.0
          then:
            - switch.turn_off: switch_relay
    update_interval: 60s

Remove the then under the above - I expect that is what it is telling us.

      on_value_range:
        - above: 66.0
          - if:

DeadEnd

I really appreciate your help and your persistence. This is how I finally got it working.

Thanks again for your help!

  - platform: dht
    pin: GPIO3 
    model: DHT11
    temperature:
      name: "${devicename} Temperature"
      #id: ${devicename}_temperature
    humidity:
      name: "${devicename} Humidity"
      #id: ${devicename}_humidity
      on_value_range:
        - above: 66.0
          then:
            - if:
                condition:
                  lambda: |-
                    return id(homeassistant_time).hour < 22;
                then:
                  switch.turn_on: switch_relay
        - below: 65.0
          then:
            - switch.turn_off: switch_relay
    update_interval: 60s

Ah!
I didn’t see that the then for the switch.turn_on was not indented into the if statement.
Good Catch - apologies I didn’t take the time to get my indentations correct
… shoddy help… but at least I got you on the right road :slight_smile: .

Congrats!
DeadEnd

Yes, you definitively did and I am thankful for that. Thanks!

Sorry to bother you again but I have been pulling my hair because it doesn’t compile. I am sure that’s something obvious to a fresh pair of eyes.

time:
  - platform: homeassistant
    id: ha_time

sensor:
  - platform: dht
    pin: GPIO3 
    model: DHT11
    temperature:
      name: "${devicename} Temperature"
      #id: ${devicename}_temperature
    humidity:
      name: "${devicename} Humidity"
      #id: ${devicename}_humidity
      on_value_range:
        - above: 66.0
          then:
            - if:
                condition:
                  - lambda: 'return id(ha_time).hour < 22;'
                then:
                  switch.turn_on: switch_relay
        - below: 65.0
          then:
            - switch.turn_off: switch_relay
    update_interval: 60s
  

I get the following error:

Compiling /data/fan_1/.pioenvs/fan_1/src/main.cpp.o
src/main.cpp: In lambda function:
src/main.cpp:326:23: error: 'class esphome::homeassistant::HomeassistantTime' has no member named 'hour'
return ha_time->hour < 22;
^
src/main.cpp:327:3: warning: control reaches end of non-void function [-Wreturn-type]
});
^
*** [/data/fan_1/.pioenvs/fan_1/src/main.cpp.o] Error 1
========================== [ERROR] Took 14.00 seconds ==========================

Okay, that error is telling us that the time returned from Home Assistant does not define the hour by itself… I will have to look and see if I can tell what is returned from Home Assistant… I thought that would work…

Let me dig through GitHub and see if I can figure out what is returned…

DeadEnd

… Okay, the documentation isn’t great - but I think you need to add .now() after the ID and before the .hour… but this is just my educated guess…

 - lambda: 'return id(ha_time).now().hour < 22;'

This tells it to pull the hour from the current time… previously we had not referenced what part of the time component we wanted I guess…

Let me know if that works!

4 Likes

You’re the Man!!! That did it! Thank you again! I truly appreciate your help on this. Now my wife won’t get mad at me for waking up in the middle of the night when the fan goes off at 3:00 am. :slight_smile:
Thanks again

Hi!
< 22 mean 21
And if I want to do somthing at 21:30 how it will look like?

I think that something like:

- lambda: 'return id(ha_time).now().hour = 21 && id(ha_time).now().minute = 30;'

I am not 100% sure on the syntax. Sorry

1 Like

yes. the syntax is right. thank U

1 Like

@laca75tn this may (or considering the age of this thread might have already) turn on the fan after midnight when hour cycles back to 0. if so, you’d need a second condition to check if hour is also greater than say 6 in the morning

Hi all, i understand this an old post but better late than never… anyway I’ll jump into it. I have an esp-01s paired with a motion sensor on GPIO2 and an led on GPIO0 all set up and working in esphome. In HA I have an automation which triggers when motion is detected, it’s conditioned that it runs only before sunrise and after sunset, then it turns the led on, delays for 30 seconds after last motion detected, then turns the led off. My question is how can I hard program this automation into the esp device without pulling the time or sun state from home assistant (so it will work if wifi is down). I understand that having the condition based on the sun state working locally on the esp device is unlikely but is there a way to perhaps change the condition to a fixed time, say it only triggers after 7pm and before 7am? Is there a way to pull the time from home assistant periodically when wifi is stable and store this value so that if wifi goes down the esp can utilize the most recent saved time and calculate the current time by subtracting how many seconds have passed since the last saved time? Basically how can I store the time locally on the esp and make a local automation that conditionally utilizes the time?
here’s my current esphome yaml:

esphome:
  name: master-bath-1
  friendly_name: master bath 1

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "some_key"

ota:
  password: "some_password"

wifi:
  ssid: some_wifi_ssid
  password: some_wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "some_ssid"
    password: "some_password"

captive_portal:

sensor:
  - platform: wifi_signal # Reports the WiFi signal strength/RSSI in dB
    name: "WiFi Signal"
    id: wifi_signal_db
    update_interval: 10s
    entity_category: "diagnostic"

  - platform: copy # Reports the WiFi signal strength in %
    source_id: wifi_signal_db
    name: "WiFi Signal Percent"
    filters:
      - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
    unit_of_measurement: "Signal %"
    entity_category: "diagnostic"
    device_class: ""

binary_sensor:
  - platform: gpio
    pin: GPIO2
    name: "Motion Sensor"
    device_class: motion

output:
  - id: light_output
    platform: gpio
    pin: GPIO0

light:
  - platform: binary
    name: "Light"
    output: light_output