Getting current date and time for service call in button card

Getting current date and time for call-service in a button card

I’m trying to set up a button that logs an event with my hosted BabyBuddy api.

I currently have a button card set up and working if I hard code the current date in this format: 2021-08-17T20:06:00+01:00. What I need to do is have the card pick up the current date and time in the required format whenever the it is pressed.

Here’s my card configuration:

    type: button
    tap_action:
      action: call-service
      service: babybuddy.services_changes_add
      service-data:
        child: sensor.baby
        time: 2021-08-17T20:06:00+01:00
        wet: true
        color: Green
      target: {}
    name: 'Change: Wet & Green'
    icon: mdi:human-baby-changing-table
    show_name: true
    show_state: false

The above button has been tested to successfully log a wet, green nappy in my BabyBuddy instance. But for full WAP I need the button to be automatic. My plan is to have a dashboard of all the permutations in buttons so me or my wife can just press the required button in HA and all the logging is done in BabyBuddy. It’s just this issue with the time I can’t fathom - and the new arrival is in 5 weeks (eek!)

For extra bonus points, if anyone is able to tell me how to do this and also add 15 minutes onto the time, that would be incredible (the feeds need an end time, but I’m fine with this being a static 15 minutes rather than start messing around with start/stop timers).

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'time_date'

and

1 Like

You can use a template to compute the current time (or current time plus 15 minutes) however you cannot employ templates in tap_action.

I suggest you use tap_action to call a script. Create a script containing the service call for babybuddy.services_changes_add and use a template to the current date and time for the time option (I’m unfamiliar with babybuddy and assuming the time option supports templates).

This computes the current date and time:

time: "{{ now() }}"

This adds 15 minutes to the current date and time:

time: "{{ now() + timedelta(minutes=15) }}"

If you wish, the script can be designed to support parameters that are passed to it by the tap_action.

1 Like

Thank you for the tips. I ended up creating my flow in a script and using a button card to call the service that way and then now have it working.

Glad to hear it. Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.