Adding a random time to input_datetime (for lighting)

I am trying to add a random amount of time to an input_datetime time field.
(see Add random_at: to platform: time)
I would like to do something like the following to add between 20 minutes and two hours to the current time

action:
  service: input_datetime.set_datetime
  entity_id: input_datetime.lights_off
  data_template:
    time: "{{ now() | as_timestamp() | int + (range(1200, 7200)|random) | timestamp_custom('%H:%M:%S', False) }}"

however this doesn’t seem to work
I can run

time: "{{ now() | as_timestamp() | int | timestamp_custom('%H:%M:%S', False) }}"
or
time: "{{ (range(10, 3600)|random) | timestamp_custom('%H:%M:%S', False) }}"

which both work fine - what am I doing wrong please (I have looked at the templating and the jinja docn, as well as other template that add to temperatures ( such as {{ states(‘sensor.temperature’) | float + 1 }} ) and am stuck. Thanks

Try putting parentheses around (now()…random)) and see if that helps

action:
  service: input_datetime.set_datetime
  entity_id: input_datetime.lights_off
  data_template:
    time: "{{ (now().timestamp() + range(1200, 7200) | random) | timestamp_custom('%H:%M:%S', False) }}"

FWIW, the int filter isn’t needed.

1 Like

much neater too, thanks.
I’d tried the int + random as it was shown in the documentation for adding a static value to the temperature, obviously the timestamp is an int but it seemed to pipe it to something to add a value needed the | int + … | clearly the parentheses are required - it is not easy to debug - the developer tools log gives some info.