Automation Trigger based on DateTime Sensor

Hello mates

I would appreciate it heaps if someone guides me to solve this issue that is consuming all my time to no avail, I have a DateTime sensor that gets its value daily of the astronomical night time in Australia based on my home coordinates, the sensor gets the DateTime in UTC but I managed to format it to show my local time in Australia, let me assume the following

The UTC sensor value is: 2021-07-14 11:20:00
This would convert to my local time as 2021-07-14 21:20:00

I created an input_datetime sensor to use as the automation trigger, so all I needed to set that input_datetime sensor value to the local time shown above, however, whatever I do the input_datetime sensor only gets the UTC value which would be the wrong time for the automation trigger

How can I solve this problem, please?

Cheers

An input_date stores its time and date value as UTC. When displayed in the UI, the time value is converted to your local time. When used as the reference for a Time Trigger, it will also be converted to your local time.

Thanks mate for your quick reply, When I display the input_datetime in the UI it shows the UTC time, my coordinates for home assistant are set right along with the time zone, the time stored in the sensor that I cannot use as a trigger is 12:01 PM, when I set the value of the input_datetime to that sensor, it becomes 2:01 AM in the input_datetime when I display the input_datetime along with the sensor, it shows, 2:01 AM and 12:01 PM, I have set the automation trigger to the input_datetime and will see in about two and a half hours if it will trigger and will let you know, it is 9:40 AM now

Cheers

Show me how you are doing that.

Thanks mate

I actually managed to edit the time in the sensor to make it closer so I do not have to wait and when the time arrived the automation did not trigger

The sensor data is in UTC time, however when I view the sensor on the UI, it shows the time left or passed from the sensor time, for example, in 3 minutes, but when I click on the sensor to view more info the sensor shows the local time

This is how I set the input_datetime value to the sensor value, I set it up using the Developer Tools Services page, but planning to run it in a script when I finish my testing and troubleshooting. Cheers

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.xxxx
data:
  datetime: '{{ states(''sensor.yyyy'') }}'

Hello Taras

I think I got it, I managed to transfer the local time of the sensor to the input_datetime trigger by using the following

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.xxx
data:
  datetime: '{{ as_timestamp(strptime( states(''sensor.yyy''), "")) | timestamp_local }}'

Thanks a lot for your hint that made me re-think the way I transfer the value from the sensor to the input_datetime

Cheers

That sounds like the behavior of a sensor whose device_class is timestamp. Is this sensor automatically created by an integration or have configured yourself in YAML? If it’s YAML, please post it along with a sample value (I realize you said it was in UTC but I would like to see an actual example of it).


Looks like I posted a bit too late.

Still surprised you had to convert it to local time.

You haven’t supplied strptime with a format parameter so it ultimately does nothing with the string produced by states(''sensor.yyy''). In other words, the use of strptime in that template is unnecessary.

I reviewed the documentation for Input Select and now understand that if you set the datetime option, the value must be a datetime string representing local time. If you set the timestamp option, the value must be a Unix Timestamp which is typically understood to be UTC in Home Assistant.

So if you set the timestamp option, the template can be reduced to this:

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.xxx
data:
  timestamp: "{{ as_timestamp(states('sensor.yyy')) }}"
1 Like

OP, a bit outside the box, but you know the sun entity has a number of attributes for the different phases of dusk and dawn, do you? Those are based on the location you set your HA to.

Is the “astronomical night time” you’re after different than any of the ones already provided?

1 Like

Thanks mate, I really appreciate your input, the formula I used was copied from somewhere and to be hinest I did not understand it fully, but ow I do with your feedback, I will certainly try your advice. Cheers

Hello Chris

You are certainly right, the astronomical night is when the sun elevation is 18 degrees under the horizon, however I just used the astronomical night as an example and I am not really looking for that, rather another time that cannot be calculated via the sun elevation

Cheers

1 Like

Yeh mate I am surprised too, also your suggestion did not work, I get an error when I run the script so I reverted back to the solution I came up with despite not fully understanding it, but to make you understand it a bit I will give you a clear example

Here is the value of the sensor as detected by the integration and as obtained from the developer tolls states page
2021-07-13T19:45:00+00:00 (This is a UTC time which is 10 hours before my local time)

When I view that sensor on the UI, it only shows how long left, verbos style, however double-clicking on the sensor to show more info, it shows the right local time which should be 10 hours after the time of the sensor shown above, i.e. 2021-07-14 05:45:00

Now, when I transfer the value of the sensor to the input_datetime using the equation I posted (as your equation did not work, it produced float error)

This is the equation I used
'{{ as_timestamp(strptime( states(''sensor.yyy''), "")) | timestamp_local }}'

The value of the input_datetime is as obtained from the developer tools states page
2021-07-14 05:45:00

Which is 10 hours after the value of the original sensor, and that worked great and triggered the automation at the right local time

Any hints or ideas as to why it only works this way?

Cheers

Hello Chris

Further to my previous reply, I managed to get your formyla to work, just fidlled a bit with quotes until it worked

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.yyy
data:
  timestamp: '{{ as_timestamp(states(''sensor.xxx'')) }}'

Please note that the 4 quotes surrounding the sensor are 4 single quotes and not 2 double quotes

Cheers

That was from @123 , not me :wink:

1 Like

What was the error message?

Sorry but by stating that it suggests you may not understand the rules governing the use of quotes.

Simple rule: a template’s inner quotes and outer quotes must be different.

  • If the inner quotes are single-quotes, the outer quotes should be double-quotes.
  • If the inner quotes are double-quotes, the outer quotes should be single-quotes.

Where things get more complicated (and perhaps what served to confuse you) is that the Automation Editor has a preference for using two consecutive single-quotes ('') instead of a single double-quote (") for inner quotes and a single-quote for outer quotes. In other words, it eschews the use of double-quotes in favor of doubled single-quotes. It’s perfectly valid as well but represents a formatting preference as opposed to a golden rule.

This is valid and works:

  timestamp: '{{ as_timestamp(states("sensor.xxx")) }}'

However, the Automation Editor will convert it to this:

  timestamp: '{{ as_timestamp(states(''sensor.xxx'')) }}'

Similarly, this is valid:

  timestamp: "{{ as_timestamp(states('sensor.xxx')) }}"

Nevertheless, the Automation Editor will convert it to its preferred format:

  timestamp: '{{ as_timestamp(states(''sensor.xxx'')) }}'

FWIW, I don’t use the Automation Editor and prefer to compose my automations and scripts using a text editor (VS Code) where I can use single and double-quotes according to the rules I listed above.

1 Like

You are certainly right about what confused me regarding the single and two single quotes, the developer tools template is different from the actual automation editor, I am actually a Micorosft systems background with VB programming knowledge so I do expect to get confused a lot but with my lil knowldge and your help guys I will always get it at the end

Cheers

Hello mate

Would you be able to help me out with the formula of the value of an input_datetime object that will be 7 days after the value of another input_datetime object

What I need to do is as follow

I have a fish pond in my house and the water change happens automatically via some scripts that I wrote, at the end of the script it assigns the Water Change Last Date via assigning the current datetime to the first input_datetime, now I want to assign the datetime of the next water change to the second input_datetime which will be 7 days after the last water change

Thanks a lot for your help

Cheers

Instead of a second input_datetime, why not make a Template Sensor that simply adds 7 days to the value contained by the first input_datetime?

Thanks mate for the quick reply

I think you are right as I am not using that suggested second input_datetime to trigger anything, just information, also the sensor value will not be lost if I restart HA as it will be based on the first input_datetime

Thanks a lot for the good hint

Cheers

Hello mate

Sorry to keep bothering you but I do appreciate your help

I did your idea and is working great, only one minor thing I need help with, when I view the new sensor on the UI it show the date and time which is correct but I want to show the age instead, for example in 2 days, or in two hours, in 10 seconds, and so on I thought I can use the relative_time but I just cannot get it to work, it still shows the dat and time instead of the age

Cheers