Run automation based on phone's timezone and not on the server's

I have an automation sending me information at 9, 12, 15 and 18 o’clock. Works great except when traveling and these notifications arrive at weird hours. So is there any way to fetch the phone’s timezone so I can properly send the notifications? I travel a lot and it’s at any possible location so doing a static script is useless.

Android only… :frowning:

The geolocation sensor has an attribute for TimeZone for IOS. Can you not use that? Or are you saying you have android phones above? In that case use the “Current Time Zone” Sensor.

Damn, the sensor was disabled. Yes, it fully solves the issue. Thanks!

1 Like

Hi there, I have the exact same issue and figured out the time zone sensor already. How exactly are you using that in an automation though, to get something done based on the phone’s time? I’m trying to wrap my head around that part…

The automation runs every hour and checks what time the phone has as a condition. Basically read the phones time zone and add a sensor that has the value of the phones time based on its time zone. Update this on every start-up and whenever the phone changes time zone. Have an automation run every hour and as a condition compare the phone’s time with whatever you need.

I did it like this (5min patch after figuring the time zone thing) - just a “demo”.

python script (timezone.py):

from datetime import datetime, time
from zoneinfo import ZoneInfo
import sys

print(datetime.now().astimezone(ZoneInfo(sys.argv[1])).isoformat())

command line sensor in configuration.yaml

command_line:
  - sensor:
      name: LeoPhone DateTime
      command: python3 timezone.py "{{ state_attr("sensor.leophone_geocoded_location", "Time Zone")}}"

automation to update the newly created sensor every time the phone changes time zone or HA starts up

alias: timestamp update
description: >-
  reflect the mobile phone timestamp so notifications can be sent at the correct
  time
trigger:
  - platform: state
    entity_id:
      - sensor.leophone_geocoded_location
    attribute: Time Zone
  - platform: homeassistant
    event: start
condition: []
action:
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: homeassistant.update_entity
    target:
      entity_id: sensor.leophone_datetime
    data: {}
mode: single

automation to actually do something based on the phone’s time

alias: weather status
description: >-
  send a weather status every hour as long as the phone is in the home
  country
trigger:
  - platform: time_pattern
    hours: /1
condition:
  - condition: template
    value_template: >
      {{ states('sensor.leophone_datetime') | as_datetime | as_timestamp |
      timestamp_custom('%H:%M') == today_at("08:47").isoformat() | as_datetime |
      as_timestamp | timestamp_custom('%H:%M') }}
action:
  - service: notify.mobile_app_leophone
    data:
      title: Test Notification
      message: >-
        {{ now().strftime('%d %b %Y %H:%M:%S') }}

        this should arrive based on the phone's time
mode: single

Ah, ok. Thank you! That’s a lot more than I expected