When creating IoT projects on ESP8266 etc, getting UTC is fairly trivial but time zone and DST are challenging. For most projects ntp is also overkill so this little automation provides the local time of the Home Assistant installation to any MQTT client.
The automation publishes the local time at 5 minutes past each hour to the topic homeassistant/localtime with a payload of an (almost) ISO date/time format YYYY-MM-DDThh:mm:ss.sss[±]00:00 (note the ‘Z’ is missing before the time zone info).
It will also respond to the topic homeassistant/localtime/request.
Edit: fixed blueprint syntax and shortened title
Note: After importing the blueprint, when you Create an Automation from it, you need to enter a description to make the ‘Save’ button appear. (Caused me some confusion).
Hopefully no more edits.
blueprint:
name: MQTT Local Time
description: Publishes local time via MQTT
domain: automation
trigger:
- platform: mqtt
topic: homeassistant/localtime/request
- platform: time_pattern
minutes: '5'
action:
- service: mqtt.publish
data:
topic: homeassistant/localtime
payload_template: "{{ now().isoformat() }}"
Yes, initially I shared it as a traditional automation but as this is a the Blueprint repository, I thought it appropriate to make it accessible as such. As a traditional automation, the end user would have to be comfortable editing automations.yaml (and either have SMB/ssh/File Editor set up) - so really just ease of use for those not so familiar with Home Assistant. And it’s more about the concept than the implementation
The timing is primarily for DST changes - they tend to happen on the hour so scheduling 5 minutes after would update all subscribed devices without too much delay.
A user certainly can do all that or they could use a Blueprint. Or they could hand roll. Options.
Not quite - I first posted it to the the Blueprint repository as a normal automation in the hope it might be useful to someone and then noticed there was a ‘formal’ way of sharing Blueprints so converted it. My bad.
I didn’t realise Blueprints had to use (all?) the Blueprint features…
As I said before, I initially shared it as a simple automation - now that you’ve replied with the same yaml [MQTT Local Time], then both options are available.
Hmmm… Taras’ replies seem to be disappearing so the above link is broken.
This is the standard automation that I started with if you prefer to just paste it into automations.yaml:
- id: 'localtime'
alias: MQTT Local Time
description: 'Publishes local time via MQTT'
mode: single
trigger:
- platform: mqtt
topic: homeassistant/localtime/request
- platform: time_pattern
minutes: '5'
condition: []
action:
- service: mqtt.publish
data:
topic: homeassistant/localtime
payload_template: "{{ now().isoformat() }}"
Thanks for work and contributions. I imported your automation, and it worked great. Then I inadvertently deleted. When I reinstalled it I get the following error that I can not figure out. Any help would be greatly appreciated!
Message malformed: extra keys not allowed @ data[‘0’]