Using uptime sensor in template sensor trigger

I have set up some sensors as template sensors with a trigger at startup. Due to some issues with the times that some of the values used in these sensors are being updated, i now realise I need to wait maybe 10 minutes after HA startup to populate these.

I have installed the Uptime integration and so am attempting to use this but cannot work out the syntax of the trigger. I have seen value templates to calculate minutes between now and the sensor.uptime but cannot find any examples of defining this in a trigger.

My current attempt (with the startup trigger still in place) is below:

- trigger:
  - trigger: time_pattern
    # This will update every night
    hours: "0"
    minutes: "01"
  - trigger: homeassistant
    event: start
  - trigger: numeric_state
    entity_id: sensor.uptime
    value_template: "{{ now() - state.state|as_datetime | timedelta( minutes ) }}"
    above: 10
  sensor:
  - name: sun_sunrise
    device_class: timestamp
    state: >- # "{{ (state_attr('sun.sun','next_rising') | as_datetime | as_local).isoformat() }}"
      {% set day = (as_timestamp(states('sensor.date_time_iso'))
        | timestamp_custom("%Y-%m-%d", true) ) | string %}
      {% set sday = (as_timestamp(state_attr('sun.sun','next_rising'))
        | timestamp_custom("%Y-%m-%d", true) ) | string %}
      {% if day != sday %}
        {% set time = (as_timestamp(state_attr('sun.sun','next_rising'))
        | timestamp_custom("%H:%M:%S", true) ) | string %}
        {{ (strptime(day+' '+time,'%Y-%m-%d %H:%M:%S') | timestamp_local) | as_datetime | as_local).isoformat() }}
      {% else %}
          {{ as_timestamp(state_attr('sun.sun','next_rising')) | timestamp_local | as_datetime | as_local).isoformat() }}
      {% endif %}

This is how I use it:

value_template: "{{ ( now() - states('sensor.uptime')|as_datetime ).seconds > 120 }}"

another perspective, create a binary with

  - trigger:
      trigger: homeassistant
      event: start
    binary_sensor:
      - unique_id: ha_restarted
        auto_off: 100 #adapt here
        state: 'on'

and use that binary as trigger

(it is practical as standalone binary, because can be used to serve as condition in automations etc,)

1 Like

That’s fine if you only want one fixed time. Which would be most cases I guess.

Yeah, it would be even nicer if we could enter an input_number there. Not even sure if we can’t….

You can’t.

1 Like