Day Counter for Homeassistant

cobshopgrow

Well-Known Member
Well, who dont like to know how many days the plants are in flower?
many ways to do it, If you have a homeassistant running anyway its quite easy to let him do the counting for you.

days.png

insert this in your configurations.yaml first.

YAML:
input_datetime:
  plant_blooming_start:
    name: Start of plant blooming
    has_date: true
    has_time: false
and this to your configurations.yaml
YAML:
sensor:
  - platform: template
    sensors:
      plant_blooming_days:
        friendly_name: 'Plant Blooming Days'
        value_template: >
          {% set bloom_start = states('input_datetime.plant_blooming_start') %}
          {% if bloom_start %}
            {% set bloom_date = strptime(bloom_start, '%Y-%m-%d') %}
            {{ ((now().replace(tzinfo=None)) - bloom_date).days }}
          {% else %}
            'unknown'
          {% endif %}
        icon_template: mdi:flower
        unit_of_measurement: 'Days'
after a restart you should have a "Plant Blooming Days" sensor counting you the days.
you can set the start date under "configurations" "devices" "helpers".

if you want a reset button for the days, like me, you need to add this to your configurations.yaml also
YAML:
input_boolean:
  reset_plant_blooming_counter:
    name: Reset Plant Blooming Counter
    initial: off
    icon: mdi:restart
and this to your automations.yaml
YAML:
- alias: "Reset Plant Blooming Days Counter"
  trigger:
    - platform: state
      entity_id: input_boolean.reset_plant_blooming_counter
      to: 'on'
  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: input_datetime.plant_blooming_start
      data:
        date: "{{ now().strftime('%Y-%m-%d') }}"
    - service: input_boolean.turn_off
      target:
        entity_id: input_boolean.reset_plant_blooming_counter
restart and you will have a "Reset Plant Blooming Days Counter" Button also which sets you the day counter to zero.
simple but usefull, hope it helps.
 
Last edited:

Billy the Mountain

Well-Known Member
Well, who dont like to know how many days the plants are in flower?
many ways to do it, If you have a homeassistant running anyway its quite easy to let him do the counting for you.

View attachment 5341648

insert this in your configurations.yaml first.

YAML:
input_datetime:
  plant_blooming_start:
    name: Start of plant blooming
    has_date: true
    has_time: false
and this to your configurations.yaml
YAML:
sensor:
  - platform: template
    sensors:
      plant_blooming_days:
        friendly_name: 'Plant Blooming Days'
        value_template: >
          {% set bloom_start = states('input_datetime.plant_blooming_start') %}
          {% if bloom_start %}
            {% set bloom_date = strptime(bloom_start, '%Y-%m-%d') %}
            {{ ((now().replace(tzinfo=None)) - bloom_date).days }}
          {% else %}
            'unknown'
          {% endif %}
        icon_template: mdi:flower
        unit_of_measurement: 'Days'
after a restart you should have a "Plant Blooming Days" sensor counting you the days.
you can set the start date under "configurations" "devices" "helpers".

if you want a reset button for the days, like me, you need to add this to your configurations.yaml also
YAML:
input_boolean:
  reset_plant_blooming_counter:
    name: Reset Plant Blooming Counter
    initial: off
    icon: mdi:restart
and this to your automations.yaml
YAML:
- alias: "Reset Plant Blooming Days Counter"
  trigger:
    - platform: state
      entity_id: input_boolean.reset_plant_blooming_counter
      to: 'on'
  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: input_datetime.plant_blooming_start
      data:
        date: "{{ now().strftime('%Y-%m-%d') }}"
    - service: input_boolean.turn_off
      target:
        entity_id: input_boolean.reset_plant_blooming_counter
restart and you will have a "Reset Plant Blooming Days Counter" Button also which sets you the day counter to zero.
simple but usefull, hope it helps.
Works like a charm, sure beats scrolling through a calendar, thanks!
 

cobshopgrow

Well-Known Member
as i needed a second counter for a second tent i made some changes to the code.
here it is for all who want it.

configuration.yaml
YAML:
sensor:
  - platform: template
    sensors:
      plant_blooming_days:
        friendly_name: 'Plant Blooming Days'
        value_template: >
          {% set bloom_start = states('input_datetime.plant_blooming_start') %}
          {% if bloom_start not in ['unknown', 'unavailable', 'None'] %}
            {% set bloom_date = strptime(bloom_start, '%Y-%m-%d') %}
            {{ ((now().replace(tzinfo=None)) - bloom_date).days }}
          {% else %}
            'unknown'
          {% endif %}
        icon_template: mdi:flower
        unit_of_measurement: 'Days'
      plant_blooming_days_2:
        friendly_name: 'Second Plant Blooming Days'
        value_template: >
          {% set bloom_start = states('input_datetime.plant_blooming_start_2') %}
          {% if bloom_start not in ['unknown', 'unavailable', 'None'] %}
            {% set bloom_date = strptime(bloom_start, '%Y-%m-%d') %}
            {{ ((now().replace(tzinfo=None)) - bloom_date).days }}
          {% else %}
            'unknown'
          {% endif %}
        icon_template: mdi:flower
        unit_of_measurement: 'Days'

input_boolean:
  reset_plant_blooming_counter:
    name: Reset Plant Blooming Counter
    initial: off
    icon: mdi:restart
  reset_plant_blooming_counter_2:
    name: Reset Second Plant Blooming Counter
    initial: off
    icon: mdi:restart

input_datetime:
  plant_blooming_start:
    name: Start of plant blooming
    has_date: true
    has_time: false
  plant_blooming_start_2:
    name: Start of second plant blooming
    has_date: true
    has_time: false
automations.yaml
YAML:
- id: '1691200000001'
  alias: "Reset Plant Blooming Days Counter"
  trigger:
    - platform: state
      entity_id: input_boolean.reset_plant_blooming_counter
      from: 'off'
      to: 'on'
  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: input_datetime.plant_blooming_start
      data:
        date: "{{ now().strftime('%Y-%m-%d') }}"
    - service: input_boolean.turn_off
      target:
        entity_id: input_boolean.reset_plant_blooming_counter
  mode: single

- id: '1691200000002'
  alias: "Reset Second Plant Blooming Days Counter"
  trigger:
    - platform: state
      entity_id: input_boolean.reset_plant_blooming_counter_2
      from: 'off'
      to: 'on'
  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: input_datetime.plant_blooming_start_2
      data:
        date: "{{ now().strftime('%Y-%m-%d') }}"
    - service: input_boolean.turn_off
      target:
        entity_id: input_boolean.reset_plant_blooming_counter_2
  mode: single

this gives you the option for 2 day counters in homeassistant.
 
Top