Having a custom state

Hello

Am just starting out on HA and looking to set a few binary sensors up and want to understand is the anyway to have a custom state

I have read Binary Sensor Entity | Home Assistant Developer Docs the support however none of the classes really suit my needs

Am using esphome

I would be looking to have (on = set and off = unset)

Is this possible and if so how would I go about doing it

Thank you for any advice and support and apologies for such a basic question

Welcome to HA!

To get something custom, you’ll need to use a template sensor:

I think you’ll eventually find yourself using template sensors for lots of random things, so definitely worth the time to understand them outside of this specific case. That being said, I really prefer to use existing device classes whenever possible, because they just integrate with everything else in Home Assistant so much better. But I completely get it; there are times where none of the existing classes work for your use case.

As said above, but to add:

Just remember that binary sensors are only on or off (the template must return true or false). They can also be unknown, but that’s a different matter. You don’t have to define a device class, but if one suits, it basically translates the state (e.g. to open or closed) and give you matching icons. That said, you can set your own icon without a device class.

Thank you for you support

Would I be right in thinking I need to understand value_template and starting with

binary_sensor:
- platform: gpio pin:
number: D8
mode: INPUT_PULLUP
inverted: yes
name: "Door_contact"
value_template 

Then working out how to do the value_template or would this be totally wrong

So I can’t use a binary sensors a with template

The template sensor approach I was referring to would be done within the Home Assistant configuration YAML file, not the ESPHome YAML (although I guess you could also do it all in ESPHome, but that makes less sense in my mind). Basically, you’d setup a generic binary sensor in ESPHome and then create ANOTHER template sensor within HA that would take the value of your ESPHome sensor and return the text state you’re after. The problem with this approach, is the second sensor is no longer binary and Home Assistant won’t really be able to do anything with it; it will just be a meaningless text value to it.

I noticed it seems that you are making a door contact sensor potentially? Is there a reason the door device class won’t work?

Sorry I was just using that as a template from another sensor I have working try figure it out

I basically have an esp connected to my alarm system to feed in when the alarm is set and upset to then control lights ect. Due to the alarm it’s a basic open / close value.

It would of been nice to have set and upset displayed over on/off however if you think it will make more issues I will stick with one of the standard device classes that will link in better with HA

Thanks again for your help

If you can only see the alarm state and can’t actually interact/change it, this probably doesn’t make much sense, but using your binary sensor and an automation, you could also incorporate this to better show your alarm system.

If you’re just displaying the value (set or unset) then the template method would be simplest. Although “on” and “off” should be just as clear. I’ve also seen “armed” and “unarmed” used in alarm systems.

If you go the template route, be sure to exclude the original entities from the Recorder so you don’t end up putting duplicate values in the database.

Binary sensors can have a device class, setting it affects how it is displayed. There should be one that fits your purpose:

But maybe, in your case, a select entity is more appropriate?

Am looking at the template method approach.
Could you show an example of what this would look like I finding it extremely difficult to find a similar template to understand

The has been lots of great information in all the replies and with my limited experience has become a little overwhelming

I have been working on this but then haven’t found out how to link this in to HA panel


value_template: >-
            {%- if is_state("binary_sensor.example ", "on") -%}
            set
            {%- else -%}
            unset
            {%- endif -%}

Thank you for this I have looked over the device class but none ideally fit my needs however if I don’t make much progress I will use the best fit

I will read over the select and try understand this more

Think of it as an enumeration: a list of values the item can have. In your case there are two values you want it to have. Technically it isn’t a binary sensor. Come to think of it: it is editable so it is not what you want. The template sensor is your best alternative to a binary sensor. There’s no limitations on what you can do with those, as long as you remember the actual value is the string you want it to show. The value template above is what you need for defining the state of the template sensor. On the top of my head, ad this to the templates of home assistant (untested):

template:
  - sensor:
      - name: "Template sensor example"
        availability: "{{ states('binary_sensor.example') != 'unavailable' }}"
        state: >-
            {%- if is_state("binary_sensor.example ", "on") -%}
            set
            {%- else -%}
            unset
            {%- endif -%}

I would do this in Home Assistant and not in the esp. You can also create a text sensor there, but I would not know how to do that without relying on timed updates, which would cause a delay in the sensor value.