Syntax for template with trigger

My sensors are split out into a sensors.yaml file with a declaration in my config.yaml like:

sensor: !include sensors.yaml

So for the template sensors in that sensors.yaml file, a typical entry looks like:

  • platform: template
    sensors:
    some_sensor:
    friendly_name: ‘Some Sensor’
    unit_of_measurement: ‘DemiNanoKiloTons’
    value_template: “{{ (states(‘sensor.whatever’) | round(0)) }}”

Etc

I’m trying to add a sensor that simply takes the value of another sensor once daily, the example given in the documentation is like:

Example configuration entry

template:

  • trigger:
    • trigger: time_pattern

      This will update every night

      hours: 0
      minutes: 0
      sensor:

    Keep track how many days have past since a date

    • name: “Not smoking”
      state: ‘{{ ( ( as_timestamp(now()) - as_timestamp(strptime(“06.07.2018”, “%d.%m.%Y”)) ) / 86400 ) | round(default=0) }}’
      unit_of_measurement: “Days”

I’ve tried a bunch of stuff, but I keep getting syntax errors when I config check it, I am guessing perhaps I have to define the trigger BEFORE the sensor: definition, and that therefore I’d have to split this out into a separate file because my config.yaml has “sensor:” then the include, so I can’t define the trigger first as everything in sensors.yaml is a "sensor: " entry as a result of the way I’ve structured my config.

Can I just confirm I have that right? Does what I’ve said seem sane, and can anyone tell me any other way I could do it to keep everything in the sensors.yaml file (without having to rewrite/restructure that file) for config brevity/neatness - is there any possible way to define the “trigger:” AFTER a “sensor:” definition has already begun as my config structure seems to require?

Please format code blocks properly.

The current format (introduced nearly 4 years ago) for Template sensors needs to be under the top-level key template. As such, it cannot be placed in your sensors.yaml file, which you have set up to use sensor as the top-level key.

In you configuration.yaml file, add the line template: !include templates.yaml and create a file in the config folder called templates.yaml. Add all your “new format” template entities, like this trigger-based sensor, there.

1 Like