How to set helper time based on a condition

I would like to set the time of a helper in a script depending on the weekday. I can’t seem to find the approach to this. My current attempt is as follows:

service: input_datetime.set_datetime
data_template:
  entity_id: input_datetime.jal_oeffnungszeit_morgen
  value: >
    {% if now().weekday() in (6,7) %} 
      09:00
    {% else %}
      08:00
    {% endif %}

Any thoughts?

I think the time needs to be in the format “09:00:00”.

Also you could use the binary workday sensor in your expression which would give you the ability to allow for holidays as well as weekends (which is what I assume day 6 & 7 are).

Correct. Also the entity id should go under target and data_template only needs to be data

https://www.home-assistant.io/integrations/input_datetime/#services

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.jal_oeffnungszeit_morgen
data:
  value: >
    {% if now().weekday() in (6,7) %} 
      09:00:00
    {% else %}
      08:00:00
    {% endif %}

There are example on the page I linked to.

If you wish, you can reduce the template to this:

data:
  time: "0{{ '9' if now().weekday() in (6,7) else '8' }}:00:00"

EDIT

Correction. Option’s name is time

Thanks for your feedback. I actually also had tried %H:%M:%S but also got / am still getting:


I was thinking that I need to somehow convert to timestamp or something?

Does the input_datetime you created store date and time or just time?

If it is just time then the one-line template I posted should work.

Thanks, yes the helper is set to only time. Unfortunately, I am still getting the error posted above. For reference, the full yaml for your suggestion is as follows:

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.jal_oeffnungszeit_morgen
data:
  value: 0{{ '9' if now().weekday() in (6,7) else '8' }}:00:00

I made a mistake in my previous example (and I have now corrected it). The option’s name is time and not value.

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.jal_oeffnungszeit_morgen
data:
  time: "0{{ '9' if now().weekday() in (6,7) else '8' }}:00:00"
1 Like

Your example works! Thank you, that was very helpful :smiley:

1 Like