Help with using input_datetime in on_time

Hi

I’ve automated my cat feeder but have been trying to make it more robust and more configurable from home assistant. As such I’ve got various flags for enabling morning/evening feeds and most recently I’ve tried to use the datetime helpers in homeassistant (specifically the time element) for controlling the time for the morning/evening feed. I read a few bits and pieces that mentioned using a text sensor for the input_datetime in the esphome script, and I’d even managed to get the hours and minutes from the time, but then I fell down after trying various ways to cast this to an int for the on_time i.e this bit.

time:
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      - hours: 7
        minutes: 05
        seconds: 45

Any help would be much appreciated :slight_smile:

Andy

1 Like

Personally I would just use two HA automation… it mush easer and cleaner… 2 automations one for morning and one for evening… you don’t need helpers unless you would like to change the times through the front end.

Putting schedules in ESPHome can be hard to manage in the future.

Hi @Blacky

I’ve got a hybrid approach so the point of my internal schedule is to allow my cat feeder to feed regardless of network or home assistant. I use properties in home assistant to control these values in the esphome code, and I can set default values failing being able to get the values from HA at all.

But essentially in order to control the times out of the esphome code I wanted a way to provide them from the HA UI.

This is my current code, I’ve removed my previous attempts with the input_datetime but essentially I had some global variables with default for morning_hour, morning_minute, evening_hour, evening_minute using a text sensor for the date time. This all worked fine, but then casting those to int to use in the on_time hours and minutes, just errorred regardless of what I tried.

I’m not sure if this is the best way to do this or if there is some other way, but using the datetime helper for on_time to me feels like a fairly typical use case.

Here is my code anyway if you’re interested:

esphome:
  name: cat-feeder
  platform: ESP8266
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "password"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Cat-Feeder Fallback Hotspot"
    password: "password"

captive_portal:

text_sensor:
  # Expose ESPHome version as sensor.
  - platform: version
    name: Cat Feeder ESPHome Version
  # Expose WiFi information as sensors.
  - platform: wifi_info
    ip_address:
      name: Cat Feeder IP
    ssid:
      name: Cat Feeder SSID
    bssid:
      name: Cat Feeder BSSID

# Sync time with Home Assistant.
time:
  - platform: homeassistant
    id: homeassistant_time
    # timezone: Europe//London
    on_time:
      - hours: 7
        minutes: 05
        seconds: 45
        then:
          - if:
              condition:
                and:
                  - binary_sensor.is_off: morning_feed_complete
                  - binary_sensor.is_off: disable_next_feed
                  - binary_sensor.is_on: morning_feed_enabled
              then:
                - switch.turn_on: motor
                - homeassistant.service:
                    service: input_boolean.turn_on
                    data:
                      entity_id: input_boolean.morning_feed_complete
              else:
                - if:
                    condition:
                        - binary_sensor.is_on: disable_next_feed
                    then:
                      - homeassistant.service:
                          service: input_boolean.turn_off
                          data:
                            entity_id: input_boolean.disable_next_feed
          - homeassistant.service:
              service: input_boolean.turn_on
              data:
                entity_id: input_boolean.morning_feed_complete
          - homeassistant.service:
              service: input_boolean.turn_off
              data:
                entity_id: input_boolean.evening_feed_complete
      - hours: 17
        minutes: 35
        seconds: 45
        then:
          - if:
              condition:
                and:
                  - binary_sensor.is_off: evening_feed_complete
                  - binary_sensor.is_off: disable_next_feed
                  - binary_sensor.is_on: evening_feed_enabled
              then:
                - switch.turn_on: motor
                - homeassistant.service:
                    service: input_boolean.turn_on
                    data:
                      entity_id: input_boolean.evening_feed_complete
              else:
                - if:
                    condition:
                        - binary_sensor.is_on: disable_next_feed
                    then:
                      - homeassistant.service:
                          service: input_boolean.turn_off
                          data:
                            entity_id: input_boolean.disable_next_feed
          - homeassistant.service:
              service: input_boolean.turn_on
              data:
                entity_id: input_boolean.evening_feed_complete
          - homeassistant.service:
              service: input_boolean.turn_off
              data:
                entity_id: input_boolean.morning_feed_complete

    
# Sensors with general information.
sensor:
  # Uptime sensor.
  - platform: uptime
    name: Cat Feeder Uptime

  # WiFi Signal sensor.
  - platform: wifi_signal
    name: Cat Feeder WiFi Signal
    update_interval: 60s

switch:
  - platform: gpio
    pin: GPIO3
    name: "Dispenser Motor"
    id: motor
    on_turn_on:
      - delay: 60000ms
      - switch.turn_off: motor
    # Todo do an on turn off that restarts the motor if it has run for < 10 seconds
  - platform: restart
    name: Cat Feeder Restart

binary_sensor:
  - platform: gpio
    pin:
      number: 5
      mode: INPUT_PULLUP
      inverted: False
    name: "Dispenser Rotating"
    on_release:
      then:
        - switch.turn_off: motor
  
    # Run conditions
  - platform: homeassistant
    id: morning_feed_enabled
    name: "Morning Feed Enabled"
    entity_id: input_boolean.morning_feed_enabled
  - platform: homeassistant
    id: morning_feed_complete
    name: "Morning Feed Complete"
    entity_id: input_boolean.morning_feed_complete
  - platform: homeassistant
    id: evening_feed_enabled
    name: "Evening Feed Enabled"
    entity_id: input_boolean.evening_feed_enabled
  - platform: homeassistant
    id: evening_feed_complete
    name: "Evening Feed Complete"
    entity_id: input_boolean.evening_feed_complete
  - platform: homeassistant
    id: disable_next_feed
    name: "Disable Next Feed"
    entity_id: input_boolean.disable_next_feed

Hey @mcinnes01, did you ever figure this out? I was looking into trying to do the same thing.

not a bad decision. But the time is hard-coded, and by triggering you can skip a feed. How about changing the time for more flexibility? Setting the time without a home assistant :wink: ? I can give an example

I would like to learn how to make time configurable - so user can set time.

I am trying the same thing. Only I am attempting to use input date_time to start and stop a hot water recirculation pump. My problem is that it comes out of HA in text like this ‘00:00:00’ in a way that esphome doesn’t see as a number. For my purposes I just need to parse out the hour and the minute into a variable. I have tried just about every printf, sprintf, stoi and atoi I can find to ham handedly fix it. I must be doing something very wrong here. I could just do a number input from HA, but I really want to understand how to fix this. I just need to store the first two into

  - id: off_time_hour ##variable to hold hour recirc stops
    type: int
  - id: on_time_hour ##variable to hold hour recirc starts
    type: int
  - id: off_time_min ##variable to hold minute recirc stops
    type: int
  - id: on_time_min ##variable to hold minute recirc starts
    type: int

image

I have a fallback time set, but it would be better if this is written to the storage on a successful startup and retrieval from home assistant. The idea in my config was that I feed the cat’s before that time and hence the morning or evening completed would dismiss that fallback option. Likewise the skip feed would also take affect. So the only time this would use the hardcoded time is if home assistant has stopped calling the feeder to come on (or skip) altogether.

@CGriff In terms of the time, you are trying to set a time type to an int, which will never work. Without any decent helper functions for parsing time or dates you will need to do that yourself. If you set it as a string, then either substring or split by ‘:’ (if there is a funciton for that), then you can access the hours, minutes, seconds.

I chose to use the hard code backup time and then switch my completed and skip inputs in home assistant as the date/time stuff in ESPHome wasn’t playing nicely. If anyone can suggest how to get ESPHome to read an input_time variable and act upon it, I would be very interested.

Yea, I gave up and just used an input text for the hour and minute. I was able to kill the ‘:’ but wound up with strings of code I don’t really understand (cut/paste example) and C functions that didn’t seem supported in ESPhome. I was just in over my head. I will keep playing with it though. The date_time is just so clean on the HA interface.