I recently created an automation to announce the time of an upcoming event. The upcoming time is in UTC so tts is just announcing it as such. How would I have tts read it back as my local time instead?
The script is below:
alias: Jewish calendar demo
description: ''
trigger:
- platform: time
at: '12:00:00'
condition: []
action:
- service: tts.google_translate_say
target:
entity_id: media_player.entryway_wifi
data:
message: >-
Good morning the value is {{
states('sensor.jewish_calendar_upcoming_candle_lighting') }}
mode: single
hebom
(Christiaan)
April 29, 2022, 8:15pm
2
Thx, perhaps time now but have no idea where in my code I’d add that.
hebom
(Christiaan)
April 29, 2022, 8:58pm
4
I’ve done some experimenting and came up with a sollution for the action:
service: tts.google_translate_say
data:
entity_id: media_player.entryway_wifi
message: >-
{% set offset = 2 %}
Good morning the value is
{{ (as_timestamp(states('sensor.jewish_calendar_upcoming_candle_lighting')) + (offset * 3600 )) | timestamp_custom("%H:%M") }}
You can change offset into any number you need.
You could also look at my project if you need it to be said in a more human way Human spoken time - Share your Projects! - Home Assistant Community (home-assistant.io)
nickrout
(Nick Rout)
April 29, 2022, 10:56pm
5
I think you could use as_local
to convert. Adding 3 hours is only a hack which will not survive the switch to or from DST. Templating - Home Assistant
123
(Taras)
April 30, 2022, 1:28am
6
message: >-
Good morning the value is {{ states('sensor.jewish_calendar_upcoming_candle_lighting') | as_timestamp | timestamp_custom('%H:%M') }}
nickrout
(Nick Rout)
April 30, 2022, 1:29am
7
won’t it need converting to localtime?
123
(Taras)
April 30, 2022, 1:32am
8
timestamp_custom
converts a Unix timestamp to local time by default.
Filter timestamp_custom(format_string, local=True, default) converts an UNIX timestamp to its string representation based on a custom format, the use of a local timezone is default . If that fails, returns the default value, or if omitted the unprocessed input value. Supports the standard Python time formatting options.
1 Like
nickrout
(Nick Rout)
April 30, 2022, 1:32am
9
I knew you’d be able to correct me Love your work!
1 Like
123
(Taras)
April 30, 2022, 1:38am
10
You were right about using as_local
but it would be employed for converting a datetime object from UTC to local.
In the following screenshot, if I were to remove as_local
the resulting time would be UTC as opposed to local (19:26 instead of 15:26).
1 Like
Thank you. So how would I need to update my script accordingly?
I replaced it with updating message but now it says the value is none even though the value is a specific date and time for the sensor result.
message: >-
Good morning the value is {{
states('sensor.jewish_calendar_upcoming_candle_lighting') | as_timestamp
| timestamp_custom('%H:%M') }}
123
(Taras)
May 1, 2022, 6:06pm
14
If it doesn’t have the date and time in a format understood by as_timestamp
then it will fail to be converted to a timestamp.
Post an example of the sensor’s value.
2022-05-07T02:24:00+00:00
Above is value.
123
(Taras)
May 1, 2022, 6:11pm
16
as_timestamp
can easily convert that string to a timestamp.
Copy-paste this into the Template Editor:
Good morning the value is {{ states('sensor.jewish_calendar_upcoming_candle_lighting') | as_timestamp | timestamp_custom('%H:%M') }}
Let me know what it reports.
It reports same as what tts says:
Good morning the value is None
123
(Taras)
May 2, 2022, 12:28am
18
That’s the result you get if the sensor doesn’t exist or has a value that cannot be converted to a timestamp.
Ok so how to convert this result correctly since it includes both date and time can it just ignore the date and read the time and it so how?
nickrout
(Nick Rout)
May 2, 2022, 1:59am
20
Do the template in the developer tools bit by bit. Start with
{{ states('sensor.jewish_calendar_upcoming_candle_lighting') }}
Then try
{{ states('sensor.jewish_calendar_upcoming_candle_lighting') |as_timestamp}}
and so forth, until you can see what may be wrong.