Triggering automation based on sensor time

Hello all

I am currently triggering an automation using a template as follows:
value_template: "{{ (now() ).strftime('%H:%M') == states('sensor.time_01') }}"

This is working fine but I have been thinking about what would be more efficient in terms of processing power and resources etc.

Would it be more efficient to create a timestamp class sensor converting this to a template sensor of type timestamp and then using that to trigger the automation or does it make no difference at all?

For example like this:

- platform: template
  sensors:
     time_01_timestamp:
       device_class: timestamp
       unique_id: "time_01_timestamp"
       friendly_name: "Time 01 - Timestamp"
       value_template: "{{ states('sensor.time_01') }}"

your existing trigger is not super expensive other than that it needs to poll to check the trigger. I don’t think your alternative changes the resource load meaningfully at all

You can also just set the at: option of a time trigger to that of your sensor.

Although, it looks like your sensor only provides time. In that case you can also make a template sensor by passing the value through today_at and use that.

As for efficiency, I think all of your options come down to the same.

I tried that but for some reason it didn’t work. The sensor itself is created from a json API call response and is not marked as a timestamp in its device-class (I don’t know if that’s possible when creating the sensor in the API call anyway)

That means that I cannot use it to trigger the automation. I have already tried.

If it’s a REST sensor, you can fix that. Share the config and we can help.

Otherwise, if it’s from an integration that you can’t control, create a template sensor (UI, under Helpers) that copies the time into a timestamp-class sensor, then trigger off that.

1 Like

It is indeed a REST sensor.

- resource_template: >-
    https://www.redactedasitcontainsthekey.com

  headers:
    Content-Type: "application/json"
  scan_interval: 86400
  sensor:
    - name: "time_01"
      unique_id: 76800001
      value_template: "{{ value_json.session01 }}"

Then make sure it comes out of the value_template in valid ISO8601 format and add device_class: timestamp.

What I get is “8:15” which I guess is not the correct ISO8601 format. I see “rendered invalid timestamp: 08:15” in the logs now (after adding device_class:timestamp).

If that’s a 24h time coming in, try:

value_template: "{{ today_at(value_json.session01).isoformat() }}"
1 Like

You’re amazing mate. Sorted.
That did the trick. It just seems cleaner now :slight_smile:

1 Like