Date/Time Entry

So let me start by saying I’m not a prgrammer. I can noodle thru the basics and that’s about it.

I use input_booleans as “locks” in HA to limit actions. I would like to be able to set a Date/Timer input to do some additional items with but as much as I google, experiment and poke around I can’t find out how.

What I would like to do is:

  1. Set a random time within 30 to 45 min of a certain time. (ie. at 8:30 set a time between 9:00 and 9:15) Which I can do with the random node.
  2. Take that time and place it into my input_datetime entity.

Any assistance would be great.

Use a function node where you convert your start datetime value to microseconds with Date.parse(msg.payload), which will give you a value in microseconds since new year 1970.
To this you can add and subtract microseconds, so adding 60x60x1000 will increase the value one hour.

Use Math.floor(Math.random()*max) to get a random number between 0 and max.
Add the numbers up and return the message.

var newmsg = { payload: ( (Date.parse(msg.payload)) + (30*60*1000) + (((Math.floor(Math.Random()*15)) *60*1000) ) };
return newmsg 

Now use the value to set you input_datetime by setting it to a J: expression with the value {“timestamp”: msg.payload }

The new time node in the home assistant nodes is made for this scenario. Create an input time helper, use that entity in the node. Set it to state and check the randomize box.

That’s perfect! Works like a charm.

Just out of curiosity, is there a simple way to go the other way? Have the flow update the HA datetime_boolean?

Just use a call service node. You can use load example data to see the json formatting for the data entry. Btw load example is not working on quite a few nodes so load example won’t always work.

I see I need to update my configuration :slight_smile:

What would I put in the Data field to pass the time onto the input boolean? I’ve tried $now() but It places the UTC time and not my local. I’ve confirmed that my HA time is correct for my timezone.

What I’m thinking is having something like time > time formatter (to add time) > call service (input time into boolean).

If you click load example data, it will load the options in the data field. Then choose the 3 dots. You can only use one option at a time.

Also you’ll see visual editor at top of the second page, this will help you format json with a fill in the blank style.

1st let me say the help as been WONDERFUL but it’s the last piece that has me stumped.

I’ve done the load example data and the flow works great. But the part that I can’t grasp is how to replace the “2019-04-20 05:04:20” sample data with what’s being sent in from my “time” node.

Any experts out there that can help?

If the value is in msg.payload use

"datetime": "{{payload}}"