Template trigger sensor not working

Hi guys,

I’ve created a template based trigger to set a sensor (no physical sensor). But it does not work. I’ve tried everything.

I’ve checked debug logs, no nothing. Any tips welcome!

It’s located in configuration.yaml.

template:
  - trigger:
      - platform: time
        at: '11:20:00'
    sensor:
      - name: 'Energy Consumption (max)'
        unique_id: energy_consumption_max_test
        state: >
          {{ states('sensor.accumulated_consumption_xyz') }}
        state_class: measurement
        unit_of_measurement: "kWh"

What exactly doesn’t work?

  1. The sensor isn’t created.
  2. The sensor’s value is always unknown.
  3. The sensor reports a numeric value but it’s incorrect.
  4. The sensor isn’t recording long-term statistics.

Sorry for being vague:

The sensor is not created and I cannot see any entry in the Logbook.

What version of Home Assistant are you using?

Does your configuration.yaml file contain template: anywhere else or just the one as shown in your example above?

After you added the Trigger-based Template Sensor’s configuration to configuration.yaml did you restart Home Assistant or execute Reload Template Entities?

Latest and greatest:

Home Assistant 2022.11.4
Supervisor 2022.10.2
Operating System 9.3
Frontend 20221108.0 - latest

I don’t have template anywhere else.

I have some sensors in template also. But also a sensor headline after template:

template:
  - sensor:
    ...
  - trigger:
    ...
    sensor:
    ...

sensor:
   - platform: statistics
     ...

Post the contents of the entire template: section.

template:
  - sensor:
      - name: 'Next sunrise'
        unique_id: next_sunrise
        state: >
          {{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(" %H:%M") | replace(' 0', '') }}
        icon: mdi:weather-sunset-up
      - name: 'Next sunset'
        unique_id: next_sunset
        state: >
          {{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %H:%M') | replace(' 0', '') }}
        icon: mdi:weather-sunset-down
      - name: 'Accumulated consumption (int)'
        unique_id: accumulated_consumption_int
        state: >
          {{ states('sensor.accumulated_consumption_xyz') | int }}
        state_class: measurement          
      - name: 'Accumulated cost (int)'
        unique_id: accumulated_cost_int
        state: >
          {{ states('sensor.accumulated_cost_xyz') | int }}
        state_class: measurement          
      - name: 'Average power (int)'
        unique_id: average_power_int
        state: >
          {{ states('sensor.average_power_xyz') | int }}
        state_class: measurement
  - trigger:
      - platform: time
        at: '23:59:59'
    sensor:
      - name: 'Energy Consumption (max)'
        unique_id: energy_consumption_max_test
        state: >
          {{ states('sensor.accumulated_consumption_xyz') }}
        state_class: measurement
        unit_of_measurement: "kWh"
  • I copy-pasted your Template Sensor configuration into my test system.
  • I executed Developer Tools > YAML > Check Configuration and no errors were reported.
  • I executed Developer Tools > YAML > Reload Template Entities.
  • The Trigger-based Template Sensor appeared in Developer Tools > States:

  • Its value is unknown because its Time Trigger hasn’t executed yet.

It works for me so you may have missed some step or there’s something else wrong elsewhere that hasn’t been mentioned yet.

Oh hehe it showed up for me too. I was so blind looking in the Logbook (where it does not show up?) so I didn’t check if the sensor existed.

Didn’t know I could look in states.

Thank you very much for your helpfulness! :slight_smile:

You’re welcome!

Please consider marking my post above with the Solution tag so others know this topic has been resolved (and will learn where to look for newly created entities).