Create Alarm from MQTT value which is a UNIX timestamp

I have an alarm script that I’ve created many years ago and it works flawlessly on a web based server. I want to bring parts of it into Home Assistant to be able to create extra routines.

What I’ve done so far is I’ve gotten Node-Red to take the JSON that the web page creates and put it into MQTT. The JSON has the alarms in the form of normal UNIX timestamp and out of MQTT I’ve gotten it to grab a sensor and create it as a text field in the form of a sensor.

   - platform: mqtt
     name: “Google_Wake_Up_Alarm”
     state_topic: "calendaralarm/quickstart"
     value_template: '{{ value_json.alarm.unixWakeUpTime | int | timestamp_custom("%d.%m.%Y %H:%M")}}'
     icon: mdi:alarm

I want to be able to take those sensors and create timed automation with them. The Unix time changes whenever a new calendar event is created so the UNIX timestamp is able to change (this part works as expected with no issues). Each alarm (there are 3 per day) has a specific date they are supposed to happen on. So I can’t create it on just the same day each like a normal day, they are day specific and never consistently on the same day or time of day.

The way I get this output is (“Google_Wake_Up_Alarm” 16.10.2021 04:49) from 1634325840.

How do I turn this into something usable by HA and Automations?

I’d skip node red and mqtt altogether and grab the json timestamp directly using a rest sensor. Don’t format it. Then you can trigger trigger like this:

trigger: 
  platform: template
  value_template: "{{  now()|as_timestamp - states('sensor.your_unix_timestamp')|int < 0 }}"

No I’m not sure if I’ve got this right… it didnt seem to trigger but this is what I put in:

value_template: >-
  {{  now()|as_timestamp - states('sensor.google_alarm.unixGetReadyTime')|int <
  0 }}
id: WakeUp

this is the sensor:

sensor.google_alarm
Google Alarm
OK	
unixGetReadyTime: 1633689000
unixWakeUpTime: 1633689000
unixTimeToLeave: 1633692960
friendly_name: Google Alarm

You have to access entity attributes like this:

value_template: >
  {{  now()|as_timestamp - state_attr('sensor.google_alarm', 'unixGetReadyTime')|int >
  0 }}
id: WakeUp

Try the template in the developer tools template editor:

Open your Home Assistant instance and show your template developer tools.

Also I had my < and > mixed up.

ahhh… i worked it out… you were so close though… you had it the wrong way around the the time now is slightly different.

{{  state_attr('sensor.google_alarm', 'unixTimeToLeave') | int - now().timestamp() | int <= 0 }}