Visual editor for automations - how to get options in the 'state' dropdown for custom sensors?

I have created a sensor which derives its value from a template, to give a simple indication of whether we are currently away from home and are returning today.

I am also using the visual editor to create automations, which check this value to do things like put the heating on early. However my automations keep going wrong. When I look in the traces I think this is because I am getting the capitalisation wrong of ‘on’ and ‘off’, in different places.

I’ve seen that this problem is avoided when you use a binary sensor, as the visual editor knows what values are allowed (On, Off, Unknown, Unavailable).

Is there a way to define a binary sensor in YAML which gets its state from templating, similar to what I have already done? Or is there a way to define a set of valid values for a sensor which will show up in the dropdown of the visual editor?

sensor: 
  - platform: template
    sensors:
      presence_returning_today:
        friendly_name: Presence - returning today
        icon_template: mdi:calendar-today
        value_template: >-
          {% if states.input_datetime.return_date.state == states.sensor.date.state %}
            {{ 'on' }}
          {% else %}
            {{ 'off' }}
          {% endif %}
  1. Define a Template Binary Sensor.
  2. Consider using modern format, instead of legacy format, for all new Template entities you create.
1 Like

Thanks for those pointers. I’ve been reading around the modern format for templating; although the web seems to be full of older examples rather than the modern ones, I think I have understood it.

However, when I try to define a binary sensor within my configuration.yaml I am getting an error message on the very first line of the new bit of code. I suspect I am making a really simple error - please can you help me work this out?


template:
  binary_sensor:
    - unique_id: test_binary_sensor
      name: Test Binary Sensor
      value_template: >-
        {% if states.input_datetime.return_date.state == states.sensor.date.state %}
          {{ 'on' }}
        {% else %}
          {{ 'off' }}
        {% endif %}

This code is towards the end of my configuration.yaml, with various other declarations of sensor: binary-sensor:, etc before it. Does this matter?

The first two points are syntax errors and the last two are best practices.

  1. Leading hyphen missing from binary_sensor key word.
  2. Modern format uses state key word, not value_template.
  3. For a Template Binary Sensor, its template merely needs to report true/false and it will be interpreted as on/off.
  4. It’s preferable to get an entity’s state value using the states() function as opposed to referencing the entity’s state property directly.
template:
  - binary_sensor:
      - unique_id: test_binary_sensor
        name: Test Binary Sensor
        state: "{{ states('input_datetime.return_date') == states('sensor.date') }}"

References:

1 Like

That’s working perfectly. Thanks for taking the time to give such a detailed answer, particularly the advice about best practice :slight_smile:

1 Like