Input_datetime as automation trigger

Hi,

I have a script that I run after I feed my newborn. It publishes the amount of feed to mqtt and use this to record how much she is having. I also want to increment a counter to total how many feeds she is having per 24hours.

The problem is sometimes she takes a break and my wife has already input the amount into HA. I still want this new amount published but I don’t want the counter to increase as its still part of the original ‘feed’ so i have have a condition for this.

However, I cant get the trigger to work. Help would be appreciated.

script:
  feeding:
    alias: Feeding
    sequence:
      - service: mqtt.publish
        data:
          topic: sophie/feeding
          payload: "{{ amount }}"
          retain: true
      - service: input_datetime.set_datetime
        target:
          entity_id: input_datetime.feeding
        data:
          datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
      - service: notify.feeding_data
        data_template: 
          message: ", {{now().strftime('%d/%m/%Y')}}, {{now().strftime('%H:%M:%S')}}, {{ states.sensor.feeding.state }} "
automation:
  - alias: Increment Feeding Counter
    trigger:
    - platform: state
      entity_id: input_datetime.feeding
      to: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
    condition:
    - condition: template
      value_template: "{{ as_timestamp(now()) - as_timestamp(states.input_datetime.feeding.state) | int > 600 }}"
    action:
    - service: counter.increment
      entity_id: counter.feeding
automation:
  - alias: Increment Feeding Counter
    trigger:
      platform: time
      at: input_datetime.feeding
    condition:
      - "{{ as_timestamp(now()) - as_timestamp(states('input_datetime.feeding'))|int > 600 }}"
    action:
      service: counter.increment
      entity_id: counter.feeding

didn’t work :frowning:

My Config Package

sensor:
  - platform: mqtt
    state_topic: "sophie/feeding"
    icon: mdi:baby-bottle
    name: "Feeding"
    # unit_of_measurement: "ml"

input_datetime:
  feeding:
    name: Last Feed
    has_date: true
    has_time: true

counter:
  feeding:
    initial: 0
    step: 1
    name: Feeding

notify:
  - platform: file
    name: feeding_data
    filename: /config/www/sophie/feeding_data.csv
    timestamp: true

script:
  feeding:
    alias: Feeding
    sequence:
      - service: mqtt.publish
        data:
          topic: sophie/feeding
          payload: "{{ amount }}"
          retain: true
      - service: input_datetime.set_datetime
        target:
          entity_id: input_datetime.feeding
        data:
          datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
      - service: notify.feeding_data
        data_template: 
          message: ", {{now().strftime('%d/%m/%Y')}}, {{now().strftime('%H:%M:%S')}}, {{ states.sensor.feeding.state }} "

automation:
  - alias: Increment Feeding Counter
    trigger:
      platform: time
      at: input_datetime.feeding
    condition:
      - "{{ as_timestamp(now()) - as_timestamp(states('input_datetime.feeding'))|int > 30 }}"
    action:
      service: counter.increment
      entity_id: counter.feeding


  - alias: Reset Feeding Counter
    trigger:
      platform: time
      at: "00:00:00"
    action:
    - service: notify.feeding_data
      data_template: 
        message: ", {{now().strftime('%d/%m/%Y')}}, , , {{ states.counter.feeding.state }}"
    - service: counter.reset
      entity_id: counter.feeding

Define “didn’t work”?

sorry, the automation didn’t fire

Can you remove the condition (to test) - aside from rewriting the states bit I didn’t test it’s validity as you said the trigger was the problem.

Also I notice your input_datetime.feeding ‘has_date’ is set to true, is it set to today’s date?

Probably because the condition prevented execution of the action.

The condition’s logic is faulty.

Say it triggers at 13:00. Here’s what your condition is doing:

13:00 - 13:00 > 5 minutes

That can never be true.

can you fix it by chance?

Explain what you want the condition to do.

It still doesn’t fire even without condition

automation:
  - alias: Increment Feeding Counter
    trigger:
      platform: time
      at: input_datetime.feeding
    # condition:
    #   - "{{ as_timestamp(now()) - as_timestamp(states('input_datetime.feeding'))|int > 30 }}"
    action:
      service: counter.increment
      entity_id: counter.feeding

What is the input_datetime’s current value?

Im not sure - like this?

      - service: input_datetime.set_datetime
        target:
          entity_id: input_datetime.feeding
        data:
          datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

You’ve completely lost me now. Unless you need it to have a date as well set it to false.

Then set the time (and date if relevant) to 3 minutes from now on the input_datetime itself and then wait 3 minutes. There’s no need for any yaml at all.

@123 @anon43302295

Sorry guys, HA and newborn parenting don’t mix well together. Let me start again.

A Lovelace button executes the feeding script which publishes the amount of feed to Matt. A Matt sensor reads this and displays in Lovelace.

The reason the input_datetime is in the script was to record when the last feed was. Also, it persists over restarts so I could always see the last_changed value and know how long it’s been since her last feed.

I initially had the trigger of the automation set to the state of the mqtt sensor and the counter would increase with a state change. However, if my amount my child consumed over two consecutive feeds was the same then it wouldn’t increase. So I thought I could make the trigger be when the input_datetime is changed from the script.

The condition is to avoid an increase in the count of feeds if two values are published very soon after each other as this would be the same feeding episode.

I hope I have cleared up my mess and that you are still willing to help

1 Like

There’s a flaw in the logic then, because the input_datetime is being set to ‘now’ in the script, and then that’s supposed to trigger the automation.

When in fact all you need to do is add the counter.increment action to the end of the script, under a condition that the script hasn’t been run in the last 5 minutes (or whatever the time interval you want as a buffer).

Using the input_datetime to decide if the buffer time has passed won’t work, because when the script runs it will update the input_datetime, and therefore the condition will never pass because every time the condition is checked the input_datetime has only just been updated that very moment.

Ok. I’ll try tomorrow. Thanks

1 Like

@anon43302295 @123

It worked guys - thank you so much!. Out of interest, the input_datetime is only part of the script so I can work out how long its been since her last fed. Is there a better way to do it that persist over restarts?

1 Like

No, I think that’s the best way tbh.

1 Like