Feeling like an idiot... can't make a sensor work in sensors file

I am trying to make a binary sensor that detects when my sump pump runs… I have my sensors broken down by device in a sensors folder with yaml sub-files. None of my other files use binary sensors, so I suspect I have a problem with the template. All I want out of it is an on/off answer, then I can count it over time or notify if it is running to long.

Code:

template:      
  binary_sensor:
    sensors:
      sump_pump_state:
        friendly_name: "Sump pump on"
        value_template: >-
          {{ states.sensor.sonoff_basement_energy_current')|float > 1 }}

After a reboot I do not get the new sensor.

What in the world am I doing wrong?

The configuration you posted is a legacy-format Template Binary Sensor placed within the template domain and that’s invalid. You cannot use legacy-format in the template domain which is reserved for modern-format configuration.

Remove the first line containing template: and shift the remainder to the left by two spaces. Now it’s ready for insertion in configuration.yaml.

You said you were putting this in a “sensors folder”. However, this entity is member of the binary_sensor domain and not the sensor domain so don’t mix the two together.

1 Like

Are you saying that I cannot build a binary sensor anywhere other that in the configuration.yaml file?

I’m saying you created an invalid configuration for a Template Binary Sensor and also attempted to put it in the wrong place.

Look in your configuration.yaml file and tell me what it has on the lines containing the following two keys:

sensor:

binary_sensor:

Yup, replied to soon. My sensor: has an include directive and binary sensor: are indeed In the config file. I should fix that as I only have 6 or 7.

1 Like

Here is my binary sensor section in my configuration.yaml:

binary_sensor:
  - platform: ping
    name: gateway
    host: 192.168.1.1
    scan_interval: 60
  - platform: ping
    name: google
    host: 8.8.4.4
    scan_interval: 60
  - platform: command_line
    name: TotalConnectComfort HTTP Response Status
    command: response=$(curl -LIk -m 3 https://mytotalconnectcomfort/portal/ -o /dev/null -w "%{http_code}\n" -s); test "$response" -eq 302 && echo "OFF" || echo "ON"
    scan_interval: 60
    value_template: '{{ value }}

And my template: section…

template:
  - sensor:
      - name: "Gateway ping time"
        unit_of_measurement: "ms"
        state: "{{ state_attr('binary_sensor.gateway', 'round_trip_time_max')  }}"
  - sensor:
      - name: "Google ping time"
        unit_of_measurement: "ms"
        state: "{{ state_attr('binary_sensor.google', 'round_trip_time_max')  }}"
  - binary_sensor:
      sensors:
        sump_pump_state:
          friendly_name: "Sump pump on"
          value_template: >
            {{ states.sensor.sonoff_basement_energy_current')|float > 1 }}

It still does not populate or show up in states.

template:
  - sensor:
      - name: "Gateway ping time"
        unit_of_measurement: "ms"
        state: "{{ state_attr('binary_sensor.gateway', 'round_trip_time_max')  }}"
      - name: "Google ping time"
        unit_of_measurement: "ms"
        state: "{{ state_attr('binary_sensor.google', 'round_trip_time_max')  }}"
  - binary_sensor:
      - name: "Sump pump on"
        state: "{{ states('sensor.sonoff_basement_energy_current') | float > 1 }}"

Ok, I have it working now!

Content of configuration.yaml:

template:
  - sensor:
      - name: "Gateway ping time"
        unit_of_measurement: "ms"
        state: "{{ state_attr('binary_sensor.gateway', 'round_trip_time_max')  }}"
  - sensor:
      - name: "Google ping time"
        unit_of_measurement: "ms"
        state: "{{ state_attr('binary_sensor.google', 'round_trip_time_max')  }}"
  - binary_sensor:
      - name: "Sump pump state"
        state: >
          {{ states('sensor.sonoff_basement_energy_current')|float(0) > 1 }}

Content of /config/sensor/sump.yaml:

- platform: history_stats
  name: sump run count today
  entity_id: binary_sensor.sump_pump_state
  state: "on"
  type: count
  start: "{{ now().replace(hour=0, minute=0, second=0) }}"
  duration:
    hours: 24

image

So, now it works and I can get the automation written to warn me if it is running to much.

So, I really need to create /config/sensor/bin_sens.yaml for consistency. Then I have to figure out the proper syntax again. Have been running HA for years, a good thing, but it has lead to my configuration.yaml being bloated and to problems like this.

Also the config change that affected formatting still just blows my mind. I did manage to get my config/sensor/mqtt.yaml fixed day one because it was going to break things in a bad way.

I assume it still works OK but you don’t need to have two “- sensor:” lines under the template header.

just having one and then putting multiple “- name:” lines under it for each sensor like in my post above is all that’s needed.

Thanks, I wondered about that. But, since it is a reboot with each change and I have been rebooting today like mad working on this… I did not try it! :slight_smile:

It needn’t be. Look under Developer Tools / YAML. You can reload without restarting in many cases.

If you are interested, you can use an !include directive, similar to the one you currently have for the sensor domain, for the template domain. It would allow you to place Template Sensors, Template Binary Sensors, Trigger-based Template Sensors, etc in a separate file (or multiple separate files).

For example, this is what I use to support multiple files in a “templates” sub-directory.

template: !include_dir_merge_list templates/
1 Like

Thanks, that is how I do it with many of the domains already. For some reason, it did not register with my brain that templates are just another domain.