this post was submitted on 01 Nov 2024
10 points (100.0% liked)

homeassistant

12069 readers
33 users here now

Home Assistant is open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiasts. Perfect to run on a Raspberry Pi or a local server. Available for free at home-assistant.io

founded 1 year ago
MODERATORS
10
Assistance creating a sensor (lemmy.thewooskeys.com)
submitted 2 weeks ago* (last edited 2 weeks ago) by [email protected] to c/homeassistant
 

Hi.

This works fine in the template editor:

{% if states('sensor.apollo_mtr_1_cca750_presence_target_count') | int > 0 %}
  Detected
{% elif is_state('binary_sensor.apollo_msr_2_c7bfe8_radar_zone_1_occupancy', 'on') %}
  Detected
{% elif is_state('binary_sensor.apollo_msr_2_c7bfe8_radar_zone_2_occupancy', 'on') %}
  Detected
{% elif is_state('binary_sensor.apollo_msr_2_c7bfe8_radar_zone_3_occupancy', 'on') %}
  Detected
{% else %}
  Not Detected
{% endif %}

But when I try to create a binary_sensory from it in configuration.yaml, I get "'binary_sensor' is undefined":

template:
  - binary_sensor:
    - name: "Lounge Presence"
      state: >-
        {% if states('sensor.apollo_mtr_1_cca750_presence_target_count') | int > 0 %}
          Detected
        {% elif is_state('binary_sensor.apollo_msr_2_c7bfe8_radar_zone_1_occupancy', 'on') %}
          Detected
        {% elif is_state('binary_sensor.apollo_msr_2_c7bfe8_radar_zone_2_occupancy', 'on') %}
          Detected
        {% elif is_state('binary_sensor.apollo_msr_2_c7bfe8_radar_zone_3_occupancy', 'on') %}
          Detected
        {% else %}
          Not Detected
        {% endif %}
      device_class: presence

I'm probably using incorrect syntax or something. Can anyone help me with this?

top 11 comments
sorted by: hot top controversial new old
[–] [email protected] 1 points 2 weeks ago (1 children)

I think you can make a template from the helpers page, see if that works

[–] [email protected] 1 points 2 weeks ago

Thanks for the suggsetion @Matt The Horwood.

It looks like a "Combine the state of several sensors" or a "Group" helper only allow you to combine sensors of the same type.

I created a binary sensor from a template using the helper, but it also returns "'binary_sensor' is undefined" when I try to access its value in the template editor with {{ states(binary_sensor.lounge_presence) }}

[–] NeoNachtwaechter 1 points 2 weeks ago (1 children)

Try it without "template:" like

binary_sensor:

  • name: "Lounge Presence" [...]
[–] [email protected] 1 points 2 weeks ago (1 children)

Thanks for the suggestion, @NeoNachtwaechter.

But doing that makes configuration.yaml invalid: screenshot of alert "invalid config"

[–] just_another_person 1 points 2 weeks ago* (last edited 2 weeks ago) (1 children)

Give us the plain English of what you think this is supposed to do first 😉

Your error suggests a yaml formatting issue.

[–] [email protected] 1 points 2 weeks ago (1 children)

If any of these 4 things happen, set the sensor to "Detected", otherwise set it to "Not Detected":

  1. MTR presence target count is greater than 0 (zero)
  2. MSR zone 1 occupancy is "on"
  3. MSR zone 2 occupancy is "on"
  4. MSR zone 3 occupancy is "on"

As I said, this is working correctly in the template editor. The problem is that I can't make a sensor out of it.

[–] just_another_person 1 points 2 weeks ago (1 children)

Okay, but the post being responded to threw an error that certainly seems like a yaml formatting error.

If you simply deleted the "template" portion, and then changed the "binary_sensor" line without correcting the spacing in the lines after that, you'd get this error. Yaml is strict about leading spaces.

[–] [email protected] 1 points 2 weeks ago (1 children)

What I did was de-indent the binary_sensor section so it was at the same heirarchy as the template section, like this:

But I realize now that I have to remove the hypen before "binary_sensor". When I do, I Studio Code Server still shows a couple errors:

It says I'm missing the "entities" and "platform" properties, and that "state" is not an allowed property.

[–] just_another_person 1 points 2 weeks ago (1 children)

Move everything back two spaces. You may need a yaml linter. Your problem is just the format from what I'm seeing.

[–] [email protected] 1 points 2 weeks ago* (last edited 1 week ago)

I think Studio Code Server is a linter. It's telling me what the syntax problems are - I just don't know how to fix them. Now that I removed the hypehn, it's not telling me there are any spacing/formatting issues. But it currently says I'm missing the "entities" and "platform" properties, but I don't know what the values should be. And It also says "state" is not an allowed property, but that's the property that holds all the important stuff about this sensor - so how can I remove it?

[–] [email protected] 1 points 1 week ago

I found the solution. configuration.yaml needed to have the following syntax, with the defined values being true or false (instead of "Detected" or "Not Detected"):

template:
  - binary_sensor:
      - name: "Lounge Presence"
        state: >-
          {% if states('sensor.apollo_mtr_1_cca750_presence_target_count') | int > 0 %}
            true
          {% elif is_state('binary_sensor.apollo_msr_2_c7bfe8_radar_zone_1_occupancy', 'on') %}
            true
          {% elif is_state('binary_sensor.apollo_msr_2_c7bfe8_radar_zone_2_occupancy', 'on') %}
            true
          {% elif is_state('binary_sensor.apollo_msr_2_c7bfe8_radar_zone_3_occupancy', 'on') %}
            true
          {% else %}
            false
          {% endif %}
        device_class: presence