My custom template binary sensors (created via the GUI) are not updating correctly while entities are available

Dear all,

My goal is the create a custom binary sensor which passes the value of a motion sensor only when the light are on of the living room. (So I would like to use the motion sensor only when the lights are ‘on’).

I have tried several different implementation for the template but do not seem to get it working. I made an automation which works well, but within the automation I can not set a binary sensor

I would like to get some support why my implementation is not working as expected:

I tried several different variations

  • with and without quotes in the “logic statement”
  • with “state” and “value_template”
  • legacy and new convention for binary sensors
  • as a triggered and state binary sensor
  • restarted home assistant
  • removed “and” logic
  • placed “(” and “)” to group logic
  • in developer tool > templates it is working as expected

PS Most probably it is something very stupid, but apparently I overlook it completely.

I list two used variations below:

binary_sensor:
  - platform: template
    sensors:
      motion_radar:
        device_class: occupancy
        value_template: >
          {{ is_state('binary_sensor.tuya_motion_radar_occupancy', 'on') and is_state('light.spotjes_wk', 'on') }}

Triggered Variation:

template:
  - trigger:
    - platform: state
      entity_id: binary_sensor.tuya_motion_radar_occupancy
      to: "on"
    - platform: state
      entity_id: binary_sensor.tuya_motion_radar_occupancy
      to: "off"
    - platform: state
      entity_id:
        - light.spotjes_wk
      to: "on"
    - platform: state
      entity_id:
        - light.spotjes_wk
      to: "off"
      for:
        hours: 0
        minutes: 2
        seconds: 0
    binary_sensor:
      - name: "Adapted Motion Radar"
        device_class: occupancy
        state: >
          {% if is_state("binary_sensor.tuya_motion_radar_occupancy", "on") and is_state("light.spotjes_wk", "on") %}
            on
          {% else %}
            off
          {% endif %}

It seems that none of my binary sensor helpers created via the GUI are working. If I place the same code into configuration.yaml (by including binary_sensors.yaml) it starts working as expected (for the ones created in the yaml file and not via the GUI)

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

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
template: !include binary_sensor.yaml

Updated the title of the post to match current issue

Welcome to the forums!

I don’t understand how you could create the above code via UI? Or is the code above only that one manually created in Yaml?

It is explained here : Template - Home Assistant

So you can initiate the creation via the UI using the steps as explained on the page, you have to add manually the code for the logic.

And what did you type in?Just the Jinja code or the whole thing? Latter would explain why it didn’t work.

This is most likely wrong, the template integration can create binary sensors, but there’s also a binary_sensor integration, so this is at least confusing. What is in the binary_sensors.yaml, templates or binary sensors?

Also, you talk of adding things in the gui, but the examples you post are pure yaml, not intended for the gui (where you only post the jinja2 template for the value, not the entire definition).

And also note, if you put definitions in an include file like above, you need to remove the topmost level in the file: that is already stated outside the file. So the quoted line assumes what is in the file is what belongs under template: (so that should no longer be in the file at top level).

If you put it in yaml, then you need to check the config for errors first (which I assume would have given errors from what you posted), and if there are no errors you need to restart. If you edit template sensors in the GUI that is not needed, but then you need only supply the value template, not all the rest.

Thanks! That was my mistake. Adding only the jinja code did the trick…

Thanks as well ! Understood that if you include a file you need to take care of that. As indicated this was working properly.
Like you also suggested I just needed to add the jinja and not the whole yaml code. Rookie mistake.

Naming the include file that contains template definitions binary_sensor.yaml is still an accident waiting to happen and will likely lead to more weird errors that will be hard to spot.