UTC to local time for a sensor to tts automation

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

I’m a Home Assistant noob, but I think I found some leads at: Help please - Trigger from a time sensor - Home Assistant Community (home-assistant.io)

Thx, perhaps time now but have no idea where in my code I’d add that.

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)

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

      message: >-
        Good morning the value is {{ states('sensor.jewish_calendar_upcoming_candle_lighting') | as_timestamp | timestamp_custom('%H:%M') }}

won’t it need converting to localtime?

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

I knew you’d be able to correct me :slight_smile: Love your work!

1 Like

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?

Per post 6

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') }}

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.

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

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?

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.