How to add time to existing datetime helper

I’ve done a decent amount of searching and haven’t found quite what I’m looking for, so hoping for some help here. What I’m trying to do is set up a script where, when run, it adds an hour to the existing value of a datetime helper - so, if the current value is 6pm, when run it changes that to 7pm. However, as written, the value is set to an hour from now, not an hour from the current value. I’m quite certain I’ve got something wrong in the code, so I’m hoping someone can point out what that is.

Here’s what I’ve got:

alias: Extend Side Yard Activity
sequence:
  - service: input_datetime.set_datetime
    metadata: {}
    data: 
      datetime: "{{ (state_attr('input_datetime.side_yard_activity_end', 'timestamp') + 3600) | timestamp_custom ('%Y-%m-%d %H:%M:%S') }}"
    target:
      entity_id: input_datetime.side_yard_activity_end
mode: single
icon: mdi:clock-time-one

Thanks in advance!

What about


      datetime: {{ today_at(states('input_datetime.side_yard_activity_end')) + timedelta(hours=1) }}

?

1 Like

That got me closer, though I error out with (I think because I’m including the date and not just the time):

Error: Error rendering data template: ValueError: could not convert str to datetime: '2024-01-05 17:42:35'

However, that gave me the syntax I couldn’t figure out (HA newbie here obviously!), and what did end up working was:

datetime: "{{ (as_timestamp(states('input_datetime.side_yard_activity_end')) + 3600)|timestamp_custom('%Y-%m-%d %H:%M:%S') }}""
1 Like

It’s as simple as this:

    data: 
      timestamp: "{{ state_attr('input_datetime.side_yard_activity_end', 'timestamp') + 3600 }}"

Reference

input_datetime.set_datetime

Hi, similar problem here, NOTHING seems to work

I’m trying to adjust the time of an input_datetime entity via a Mushroom card. I’m attempting to subtract 15 minutes from the current time set on the entity, but I’m running into an error.

type: custom:mushroom-template-card
primary: ''
secondary: ''
icon: mdi:minus
tap_action:
  action: call-service
  service: input_datetime.set_datetime
  target:
    entity_id: input_datetime.bedroom_blind_tilt
  data:
    time: >
      {{ (as_timestamp(states('input_datetime.bedroom_blind_tilt')) - 900) | timestamp_custom('%H:%M:%S', false) }}

Error I am getting:

Invalid time specified: {{ (as_timestamp(states('input_datetime.bedroom_blind_tilt')) - 900) | timestamp_custom('%H:%M:%S', false) }} for dictionary value @ data['time']

Entity attributes:

  • has_date: false
  • has_time: true
  • editable: true
  • hour: 7
  • minute: 0
  • second: 0
  • timestamp: 25200
  • friendly_name: Bedroom Blind Tilt

The entity input_datetime.bedroom_blind_tilt is set to store only time.

I am trying to understand why this error occurs despite the format appearing correct and the calculations seemingly accurate. Does anyone have insights on how to properly format the time adjustment, or is there a better method to accomplish this within a Mushroom card?

If you test it in the template editor under the dev tools, you’ll get a better error.

The timestamp functions neer full date times.

You probably want this. Note the extra today_at.

{{ (as_timestamp(states('input_datetime.bedroom_blind_tilt') | today_at) - 900) | timestamp_custom('%H:%M:%S', false) }}

Have changed it to:

  data:
    time: >
      {{ (as_timestamp(states('input_datetime.bedroom_blind_tilt') | today_at) - 900) | timestamp_custom('%H:%M:%S', false) }}

clicking the minus button still returns

"Invalid time specified: {{ (as_timestamp(states('input_datetime.bedroom_blind_tilt') | today_at) - 900) | timestamp_custom('%H:%M:%S', false) }}
 for dictionary value @ data['time']"

:frowning:

Paste your template into the template editor and report the output here.

Also show the state of the helper.

If the current time will become negative when subtracting 900 s, you’re going to have a problem…

input to template editor:

{{ states('input_datetime.bedroom_blind_tilt') }}

{{ (as_timestamp(states('input_datetime.bedroom_blind_tilt'') | today_at) - 900) | timestamp_custom('%H:%M:%S', false) }}

output:

Result type: string

07:00:00

04:45:00

This template updates at the start of each minute.

This template listens for the following state changed events:

  • Entity: input_datetime.bedroom_blind_tilt

Tested different approach here and created a script. with some refining, below template worked:

{{ (now().replace(hour=states('input_datetime.uchylenie_zaslon_w_sypialni').split(':')[0] | int, minute=states('input_datetime.uchylenie_zaslon_w_sypialni').split(':')[1] | int, second=states('input_datetime.uchylenie_zaslon_w_sypialni').split(':')[2] | int) - timedelta(minutes=15)).strftime('%H:%M:%S') }}

Pasting exactly the same into template card service caller still responds with Invalid time specified, so my assumption is there service caller is not able to parse templates, which is a bummer, but at least I can get it to work with the script

Are the two single quotes after tilt intentional here?

I think I know what your problem is: You’re setting this from a card, meaning it’s the frontend doing the parsing. While that field supports Jinja templates, it’s only evaluated on the server.

This would confirm the above: scripts are evaluated server-side.