Help needed with using time from a sensor

New to HA and I can’t seem to get my head around this one, or find any topic here on how to correct/change so I can use this to trigger an automation.

sensor:
  - platform: template
    sensors:
      wake_up_lights_richard_time_starts:
        friendly_name: 'Lights Start'
        icon_template: mdi:alarm-on
        value_template: {{ (((state_attr('input_datetime.wake_up_lights_richard_set_time' , 'timestamp')) - (states('input_number.wake_up_lights_start_richard_minutes') | int * 60))|timestamp_custom('%H:%M:%S', false)) }}

When I try to use this sensor in an automation (e.g. at: wake_up_lights_richard_time_starts), it doesn’t work (value is 14:31:00 for example). I assume that it is being seen as a string or incorrect somehow; but stumped as to how I can correct this. Any help would be appreciated :slight_smile:

  1. As stated in the docs, your sensor needs to be assigned a timestamp device class.
  2. Templates must be enclosed in quotes or prefaced by a multi-line quote symbol ( > or |).
  3. Make sure you include the entity’s domain in the at: of your time trigger:
trigger:
  - platform: time
    at: sensor.wake_up_lights_richard_time_starts

FWIW, if you are starting out you should get used to using the current format for Template sensors. The legacy format you have used will work, but it lacks some of the abilities that the newer format offers and it is no longer the recommended method.

template:
  - sensor:
      - name: wake_up_lights_richard_time_starts
        icon: mdi:alarm-on
        device_class: timestamp
        state: >
          {% set time = states('input_datetime.wake_up_lights_richard_set_time') | as_datetime | as_local %}
          {% set pre_min = states('input_number.wake_up_lights_start_richard_minutes') | int(0) %} 
          {{ time - timedelta(minutes=pre_min) }}

Drew - thanks for this. I’ve copied your code into my configuration.yaml file, it checks as valid code, but returns an error on restart as follows:

Invalid config for [template]: [value_template] is an invalid option for [template]. Check: template->sensor->0->value_template. (See /config/configuration.yaml, line 230).

Line 224 is the first line “template:”

Found there was an extra " ’ ", in the line time = states(‘input_datetime.wake_up_lights_richard_set_time’’) and removed it.

In the logs, still shows an error. Pasted the code into Developer Tools->Template and it returns the following error:

AttributeError: ‘NoneType’ object has no attribute 'tzinfo’

As mentioned, I’m new to HA and don’t fully understand this.

Did try various options with the sensor assigned as timestamp class, but hadn’t realised the current format (had used some previous automations from others, clearly a bit older)

I have corrected the cause of the errors in the post above. The current format uses state: not
value_template… I missed it when converting your original.

Appreciate your help. Still get the same AttributeError: ‘NoneType’ object has no attribute 'tzinfo’

Is this possibly down to the ‘input_datetime.wake_up_lights_richard_set_time’. I have this setup as follows (no date):

input_datetime:
  wake_up_lights_richard_set_time:
    name: "Wake Up Time"
    has_date: false
    has_time: true
    icon: mdi:alarm

Yes that can cause an issue, use the following template instead:


{% set base_time = today_at(states('input_datetime.wake_up_lights_richard_set_time')) %}
{% set time = base_time + timedelta(days = 1) if now() > base_time else base_time%}
{% set pre_min = states('input_number.wake_up_lights_start_richard_minutes') | int(0) %} 
{{ time - timedelta(minutes=pre_min) }}

Drew - you are a legend! Many thanks for the help. It now makes much more sense to me and I’m slowly getting to grips with how HA code works. Still a long way to go, but this was a massive help. Works perfectly!