ESPHome pet feeding timer light

My boy has a fish he needs to feed once every 24 hours. He can’t ever remember if he did or not. So using a nodemcu, 2 ws2811 pixels, and a button. I made this ESPhome script that will keep the light green for 16 hours, then over 8 hours fade from green to red.

the code can be adapted to any feeding cycle though.

esphome:
  name: fish_reminder
  platform: ESP8266
  board: nodemcuv2

wifi:
  networks:
  - ssid: "Internet"
    password: "xxx"
  - ssid: "Internet2"
    password: "xxx"
  manual_ip:
    static_ip: 192.168.1.33
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    dns1: 192.168.1.1
  ap:
    ssid: "Fish reminder"
    password: "xxx"

captive_portal:

logger:

api:

ota:

substitutions:
  friendly_name: Fish reminder

globals:
  - id: redv
    type: float
    restore_value: no
    initial_value: '0'
  - id: greenv
    type: float
    restore_value: no
    initial_value: '1'
  - id: dir
    type: int
    restore_value: no
    initial_value: '1'

binary_sensor:
  - platform: homeassistant
    name: $friendly_name reset
    entity_id: input_boolean.fish_reminder
    on_press:
      - script.stop: fade_loop
      - script.stop: fade_red
      - script.execute: fade_reset
      - script.wait: fade_reset
      - script.execute: fade_red

light:
  - platform: fastled_clockless
    chipset: WS2811
    pin: D3
    num_leds: 2
    rgb_order: BRG
    id: fish_light
    name: "$friendly_name light"
    restore_mode: ALWAYS_OFF
    default_transition_length: 0s

sensor:
  - platform: wifi_signal
    name: "$friendly_name WiFi signal"
    update_interval: 60s
  - platform: uptime
    name: "$friendly_name uptime"

text_sensor:
  - platform: version
    name: "$friendly_name ESPHome version"

script:
  - id: fade_red
    then:
      - light.turn_on:
          id: fish_light
          transition_length: 0s
          brightness: 1.0
          red: 0.0
          green: 0.0
          blue: 1.0
      - delay: 1s
      - light.control:
          id: fish_light
          red: 0.0
          green: 1.0
          blue: 0.0
      - delay: 1s
      - light.control:
          id: fish_light
          red: 0.0
          green: 0.0
          blue: 1.0
      - delay: 1s
      - light.control:
          id: fish_light
          red: 0.0
          green: 1.0
          blue: 0.0
      - delay: 2s
      - light.control:
          id: fish_light
          transition_length: 5s
          red: 0.0
          green: 1.0
          blue: 0.0
      - delay: 16h
      - script.execute: fade_loop
  - id: fade_loop
    then:
      - light.control: 
          id: fish_light
          red: !lambda |-
            return id(redv);
          green: !lambda |-
            return id(greenv);
          blue: 0.0
      - delay: 150s
      - if:
          condition:
            lambda: |-
              return id(greenv) > 0;
          then:
            - lambda: |-
                if (id(dir) == 1) {
                  if (id(redv) < 0.05) {
                    id(redv) += 0.05;
                  } else if (id(redv) >= 0.99) {
                    id(redv) = 1;
                    id(dir) = 0;
                  } else {
                    id(redv) += 0.01;
                  }
                } else {
                  if (id(greenv) > 0.05) {
                    id(greenv) -= 0.01;
                  } else {
                    id(greenv) = 0;
                  }
                }
            - script.execute: fade_wait
  - id: fade_wait
    then:
      - script.wait: fade_loop
      - script.execute: fade_loop
  - id: fade_reset
    then:
      - lambda: |-
          id(redv) = 0;
          id(greenv) = 1;
          id(dir) = 1;
1 Like