Can't find entity of binary sensor

Hi I can’t for the life of me get my binary sensor to show an entity “true/false” under states…
I’m a complete noob but I’ve tried to search for a solution for two days without finding one.
The goal is to get a state Dark/not dark to use with lighting automations.

Here is it, under /config/components/binary_sensors/binary_sensors.yaml
binary_sensor: !include_dir_merge_list /config/components/binary_sensors/ - in configuration.yaml

Everything is OK when I check configuration + all yaml configuration etc but it’s not showing up as an entity. The template {{ states(‘sensor.tradgard_pir_syd_luminance’)|int < 300}} is showing the correct state when I try it in template editor.

Dark outside = <300 lux

template:

  • binary_sensor:
    • name: “Dark outside”
      state: >
      {{ states(‘sensor.tradgard_pir_syd_luminance’)|int < 300}}

Thanks in advance!

Have you tried testing your template in Developer Tools?

You need to paste your code into forum posts as preformatted text (the button for this is now in the cogwheel menu in the toolbar) otherwise we can’t see how it’s formatted - but it looks as if the name line needs to be indented more. This works on my system:

template:
  - binary_sensor:
      - name: “Dark outside”
        state: >
            {{ states('sensor.yard_motion_sensor_light_level')|int < 300}}

…and I get the entity binary_sensor.dark_outside

Don’t forget to reload the configuration or restart HA.

By the way, for this you can also use the UI and create a threshold helper entity. No templating required, no reload required either. And it can prevent on/off/on/off/… when the light value is fluctuating around the limit:

And for both options, if you use device_class light, HA will know to show it as light or dark (albeit you need use use on for light, off for dark).

1 Like

Thank you! I will try that :slight_smile:

Sorry for late response! I found the preformatted text button finally, this is the code:

template:
  - binary_sensor:
      - name: Dark outside
        state: "{{ states('sensor.tradgard_pir_syd_luminance')|int < 300}}"

I tried just for fun the same code but in legacy format and then it worked for some reason? Would be great for the future if some of you have an answer why the old format is working but not the new one?

- platform: template
  sensors:
    dark_outside_300_lux:
      friendly_name: Dark outside 300 lux
      value_template: "{{ states('sensor.tradgard_pir_syd_luminance')|int < 300}}"

This is my configuration.yaml file right now:

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes
  
automation: !include automations.yaml
scene: !include scenes.yaml
script: !include scripts.yaml
template: !include templates.yaml

binary_sensor: !include binary_sensors.yaml
cover: !include covers.yaml
group: !include groups.yaml
sensor: !include sensors.yaml
switch: !include switches.yaml

With the code in new format I also get now “Invalid config for [binary_sensor]: required key not provided @ data[‘platform’]. Got None. (See /config/configuration.yaml, line 12).”

That came after I reverted back the binary_sensors.yaml to /config/ path and use binary_sensor: !include binary_sensors.yaml, strange it works in legacy format though?

friendly_name is not a valid key in the new format, it’s name
sorry, you are using name

looks like you have the code below in binary_sensors.yaml, it should be placed in templates.yaml as it is using the template integration.

remove the first line with template: and place it in templates.yaml (make sure the indentation matches the configuration which is already in that file)

template:
  - binary_sensor:
      - name: Dark outside
        state: "{{ states('sensor.tradgard_pir_syd_luminance')|int < 300}}"

That worked many thanks! :slight_smile:

Confusing tho for me… are all sensors and binary_sensors templates? Is it any reason to keep the “sensor.yaml” and “binary_sensor.yaml” files or should I just use the templates.yaml?

On HA website where they have info about these things they often start with “template:” integration, is that if you paste the code into “configuration.yaml”? :slight_smile: