Template sensor does not show up as an entity

For good 5 hours can’t have this working.
I’ve came across few posts with similar issue, but none of them actually worked.

in configuration.yaml I have few inclusions

sensor: !include_dir_merge_list sensors/
rest: !include_dir_merge_list REST/

template: 
  sensor: !include sensor_template.yaml  #this includes template sensor file
  binary_sensor: !include binary_sensor_template.yaml
mqtt:
  sensor: !include sensor_mqtt.yaml
  binary_sensor: !include binary_sensor_mqtt.yaml

So, template sensors seems to be properly included through the file sensor_template.yaml

in sensor_template.yaml i have test sensor:

- name: test1
  state: "{{ states('sensor.fridge_power') | float }}"

No declaration

-sensor:

because it is already declared in confiuration.yaml.
Though I’ve tried with and without that declaration. No diff.

But for a reason unknown to me, this sensor does not appear as an entity at all!
image

Other inclusions like mqtt work fine.

additional info:
If I create a helper of the type “template” thorough the UI, everything works fine. Prob is only when template sensor is created in yaml.
btw. where the HA stores these template helpers? can’t find them in any yaml in config dir.

Any idea is highly appreciated :slight_smile:
I’m not new in HA but seems not long enough…

I think you need to include them like this since the “sensor”, “binary_sensor”, “trigger” are list items, not a dictionary:

template: 
  - sensor: !include sensor_template.yaml  #this includes template sensor file
  - binary_sensor: !include binary_sensor_template.yaml

did try, does not help :frowning:
on top of it, mqtt inclusions in the next line of my config file, work fine. even without leading dash.

just to add:
if I create them in an “old” way, with

platform: template
 - sensors:
   friendly_name: test1
   value_template: ...

it works OK.

That’s because MQTT configuration is a dictionary, not a list. It’s supposed to work that way and is different by design.

I don’t use !include for template sensors like that so the suggestion was untested. I use !include_dir_merge_list and put all of my template config yaml files in their own subfolder.

also it matters where you put the “sensor_template.yaml” file. using include like that the file needs to be in the root config directory.

You gave me an idea where to look.
I removed inclusion “template” from config file and jsut added reference

binary_sensor: !include binary_sensor_template.yaml

then created senosr in that file, but using “old” format.
by old format I mean using friendly_name, value_template (instead of “state”)
and it works!

Apparently for some reason template can’t be included by declaration !include in the config.

Can someone confrim this?

I can 100% confirm that you can use an !include for the template integration by using a merged directory list command.

I have not confirmed right now that you can split the individual list entries (sensor, binary_sensor, trigger) using !include for specific files.

But I think I tried to do it when the template integration was introduced and I think I remember it working then.

Yes, your statement is 100% correct. With individual include or dir_merge include, it works.

But seems that template specifically can’t be included. e.g.

template: !include xxxxx

btw. with your solution I can’t have new tempalte format working. Did you ever manage to have it? Or, this way only “old” format must be used.

Thanks for your time, needless to say!

I right now in my system have the template integration split using an !include directive:

template: !include_dir_merge_list template_sensors/

then in my config directory I have a sub-directory called “template_sensors” that contains all of my yaml files.

for example I have a file called 'template_binary_sensors.yaml in that directory above. It contains the following (truncated for example only):

######## TRGGER BASED BINARY SENSORS  #################
  - trigger:
      - platform: state
        entity_id: binary_sensor.i_am_in_bed
        to: 'on'
        for:
          minutes: 2
        id: in_bed
      - platform: state
        entity_id: binary_sensor.i_am_in_bed
        to: 'off'
        for:
          minutes: 2
        id: out_of_bed
    binary_sensor:
      - name: "I'm In Bed Delayed"
        state: >
          {% if trigger.id == 'in_bed' %}
            on
          {% else %}
            off
          {% endif %}

###################  STATE BASED BINARY SENSORS  #######################################################################        
        
  - binary_sensor:
      - name: Everyone Home is in Bed
        unique_id: everyone_home_is_in_bed
        state: >
          {% set wife_in_bed = is_state('binary_sensor.wife_is_in_bed', 'on') %}
          {% set i_am_in_bed = is_state('binary_sensor.i_am_in_bed', 'on') %}
          {% set wife_not_home = not is_state('person.wife', 'home') %}
          {% set me_not_home = not is_state('person.me', 'home') %}
          {{ (wife_in_bed  or wife_not_home) and (i_am_in_bed or me_not_home) }}
.
.
.

I also have a template_sensors.yaml file with similar contents that also starts with “- sensor:” as the first line.

the “- trigger:”, “- binary_sensor:” and “- sensor:” is required on the first lines of those includes since they are in the !include for the template integration.

Thanks for sharing you code.

I tried to replicate your setup but no success.

this is my case:

template: !include_dir_merge_list template_sensors/

Then I have folder in config/ like this
image

and finaly, in the file binary_sensor_template I have

- binary_sensor:
  - name: "Watching Amazon"
    unique_id: "watching_amazon"
    state: "{{ is_state('sensor.philips_tv_app', 'Prime Video') }}"
    #device_class: "running"

  - name: "Watching YouTube"
    unique_id: "watching_youtube"
    state: "{{ is_state('sensor.philips_tv_app', 'YouTube') }}"
    device_class: "running"

Check configuration does not report any error.

But none of these two binary sensors appear as an entity.

I confirm originating sensor sensor.philips_tv_app is OK and returns proper value.

Hmmm…I’m not sure what the problem could be. I don’t see anything obviously wrong.

Have you checked the homeassistant.log file for any errors there?

The config checker is notorious (for me at least) for not always reporting problems.

@finity thanks for your time anyway.
I give up, but only for the time being :slight_smile:

For the record:
Issue is resolved.

Stupid enough to add

template: 

declaration in my config.yaml at the very end, some time ago.
And of course, forgot about it. And by the rule, never scroll till the end to check…

1 Like