Input_Datetime set time in a script

Hi all,
I have a script that is called from another script and an automation to set the time in an input_datetime. I can get it to work in an automation (there are plenty of examples, but it isn’t really an option) but not in the script. I’m getting a service call error, and I’m having trouble figuring out the exact configuration syntax.

Error executing script script.sleep_timer_set_time. Invalid data for call_service at pos 1: extra keys not allowed @ data['data_template']

Here are my basic elements:

input_datetime:
  sleep_timer_stop_time:
    has_time: true
    has_date: false

input_number: 
  sleep_timer_duration:
    name: 'Sleep Timer Duration'
    initial: 20
    min: 5
    max: 60
    step: 5
    unit_of_measurement: "minutes"

  sleep_timer_set_time:
    sequence:
    - service: input_datetime.set_datetime
      data:
        entity_id: input_datetime.slaapkamer_sleep_timer_stop_time
        data_template:
          time: '{{(as_timestamp (now()) + (states("input_number.sleep_timer_duration") | int) *60) | timestamp_custom ("%H:%M:%S", true)}}'

I’m pretty sure the template is correct and that the problem is the yaml format of my service call. Thanks for your help!

If you got it working in an automation, why don’t you use the service call for the script.
It’s simply the same.

Always had the impression that it was different…anyway I did solve it with your suggestion and in the process found that the main mistake was in the entity name of the Input_datetime, which was wrong in the script. Result is now:

  sleep_timer_set_time:
    sequence:
    - service: input_datetime.set_datetime
      entity_id: input_datetime.sleep_timer_stop_time
      data_template:
        time: >
          {{(as_timestamp (now()) + (states("input_number.sleep_timer_duration") | int) *60) | timestamp_custom ("%H:%M:%S", true)}}
1 Like

Don’t use concatenations with the plus sign. See here

He doesn’t concatenate, he adds seconds to a timestamp.

VDRainer, you’re correct. Adding times together first which are in timestamp format, thus numbers that can be added together, which is not a concontenations. The input_datetime even though fully invisible and not used requires seconds - which makes it rather more complicated for the filters.

What about using the C style time, then adding the seconds for the event.

Later it may take a bit of calculation to get represented in a human readable form.

Please fill me in on what C style time is?

>>> from time import time as t
>>> t()
1563338029.7351367
>>> from datetime import datetime as d
>>> d.timestamp(d.now())
1563338138.556667

So from there it is possible to add a time in future. It might be useful to read the python documentation :wink:

Templates use jinja, not python. Jinja does not have access to time, only datetime. Jinja is limited in what it can do with datetime objects in jinja.