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

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