Restrict notifications at unsociable times (real local times)

Ok, so this isn’t exactly a project, more of a snippet but…

I have seen many people restrict notifications during unsociable hours but never seen anyone take into account the actual local time of the recipient. That is not to say no one is doing it, just that I have never seen it.

On that basis I am sharing this. It is not at all clever but as I have learnt so much from so many unexpected places on this forum I just thought someone might find it useful in general terms even though specifically the following caveats apply:

  • your notifications are handled by a script which is passed the recipient of the notification
  • you are using Google Maps Location sharing (which handily reports last_seen time in local time)

So first, discover the local time based on location:

  - platform: template
    sensors:
      my_local_time:
        friendly_name: Local time
        value_template: >
          {% set local_hour = as_timestamp(state_attr('device_tracker.google_maps_xxxxxxx', 'last_seen')) | timestamp_custom('%H') | int %}
          {% set utc_offset = local_hour - utcnow().hour %}

          {{ (now().timestamp() | int + utc_offset ) | timestamp_custom('%H:%M') }}

then, use that in your notification script:

script:
  text:
    sequence:
      - condition: template
        value_template: "{{ tell | length != 0 }}"

      - condition: or
        conditions:
          - condition: template
            value_template: >
              {{ states('sensor.' + tell + '_local_time') > '07:00' and states('sensor.' + tell + '_local_time') < '23:30' }}
    
          - condition: template
            value_template: "{{ priority_msg == 'true' }}"

      - service_template: "notify.{{ tell }}"
        data_template:
          message: "{{ message|replace('   ',' ') | replace('  ',' ') }}"

I don’t claim this is rocket science I just put it here in the hope that it might be useful to someone.

If anyone is doing something similar please post your method as I don’t doubt that there may well be a better way to do this.

Oh and one more caveat, I haven’t moved myself into a different time zone yet to test this :slight_smile: but it all checks out in the dev templating tool. Naurally if you see any errors please point them out!

1 Like

I personally find the DND setting on my iPhone works like mad… no notifications between 10PM and 7AM for me here (local time) but after 7 or if I check my phone they are all there waiting for me.

Yes, I use that on Android too but I have a Garmin watch that doesn’t respect it!
Also this is an alternative for those who like to keep DND off on their phones. I know some people need to be available at all hours for various reasons.

Um, it did! Not starting with 0.78, though:

Now it will assume the local timezone is the timezone of the HA instance. If that’s wrong, then probably the PR should not have been accepted. But it was. I really don’t know either way. All I know was it was “broken” the way it was.

If that time really is in the local timezone of the device/phone (which may not be the same as the timezone of HA), then you should probably open a new Issue against the component. In any case, the time should be reported as UTC. It’s the interpretation of what it is before the conversion that is the issue.

I wonder if there’s an easy way to get timezone from the GPS coordinates. That would probably be a better, and more generic, way of determining what timezone the device/phone is in.

I wondered that too before I wrote the code I did. Then decided it probably wasn’t feasible unles, ahem, you know python :wink:

Thanks for the heads up with 0.78 though, I am still waiting to take the plunge with the new auth system…

1 Like

Good point, actually I hadn’t considered this could be the case (well I did say I hadn’t tested by physically changing time zone). I assumed, perhaps wrongly, that Google was doing the calculation and returning the local time of the location not that which was set on the device.

I guess I was a bit vague. I did mean the timezone of where the device was. But it might also be the timezone setting in the device. Hmm, so that’s three possibilities…

So I just did a test using Google Maps Location Sharing with a phone that is in another timezone (i.e., other than where I and my HA instant are.) Somewhat surprisingly (and good for my recent change to the corresponding platform code), “local” seems to be where HA, and probably more accurately the client “scraping” code in locationsharinglib, is running. I guess this kind of makes sense. When I look at that device on the Google Maps website, it shows the time locally to me (i.e., my web browser client.) And since locationsharinglib seems to be scraping that website, it would make sense that the time is local to HA.

1 Like