[Solved] Automate sleeptime in weekend with set_datetime

Hello everyone,

I have an issue i can’t resolve. Also I did not find any solution in the community. I try to put the lights out at a time that the user puts in via a input_datetime. I have a seperate time in the weekend. I use the following components in configuration.yaml:

  name: Bedtijd
  has_time: true
  icon: mdi:sleep
  initial: '02:00'

  name: Bedtijd weekend
  has_time: true
  icon: mdi:sleep
  initial: '04:00'

I use the following code in automations:

- id: '1579458127123'
  alias: Test
  description: ''
  - entity_id: input_datetime.sleeptime
    platform: state
  - condition: template
    value_template: '{{ trigger.to_state.state != trigger.from_state.state }}'
  - data_template:
      time: {{ states('input_datetime.sleeptime') | timestamp_custom('%H:%M:%S', false) }}
    entity_id: input_datetime.sleeptime_weekend
    service: input_datetime.set_datetime

{{ states(‘input_datetime.sleeptime’) | timestamp_custom(’%H:%M:%S’, false) }} gives the correct time in the template editor. However it is not accepted in the time argument of input_datetime.set_datetime.

I hope anyone can help. Thanks in advance.

I think your missing quotes.

      time: "{{ states('input_datetime.sleeptime') | timestamp_custom('%H:%M:%S', false) }}"

Thanks for the reply, but that doesn’t work either.

I don’t know if this will work, because it is similar to using quotes, but you could try it like this:

time: >-
  {{ states('input_datetime.sleeptime') | timestamp_custom('%H:%M:%S', false) }}

Also thank you for the help, but unfortunately it doesn’t work. I cannot even save the automation.

Do you have the editor in YAML mode? If not, then you will need to click the 3 dots in the top-right hand corner of the Action editor and select “Edit as YAML” from the menu.

Something I tend to do to make editing actions which use data_template easier to edit is to use service_template instead of service even if I don’t actually use a template for the service name. This prevents the editor from switching to the standard mode, as service_template is only available in YAML mode, so it saves me a click every time I edit the automation.

I did have the editor in YAML mode. Using service_template in stead of data_template gives an error saying that i used two instances of “service.”

I was getting the same error as you. I had to use the timestamp attribute and change to False (True gave me the wrong time).

 time: "{{ state_attr('input_datetime.sleeptime', 'timestamp') | timestamp_custom('%H:%M:%S', False) }}"

That worked. Thank you so much. Also thanks to everyone who thought with me.