Do I have to create my own custom sensor to perform a digitalRead(pinNumber)?

I want to connect a specific pin to read a value and publish it. The equivalent to:

digitalRead(pin_D5)
sensor:
  - platform: gpio
    id: 'my_sensor'
    pin: D5
    on_value:
      then:
        - !lambda return x;

Do I have to create my own custom sensor to achieve this?

Thanks for your feedback!

No.

Digitalread() is just a GPIO Binary Sensor, as you have done. The device_class tells HA how to interpret it - e.g:

binary_sensor:
  - platform: gpio
    pin: GPIO4
    name: "Sunroom PIR"  
    device_class: motion

It will be published via the api or mqtt depending on your config.

Is it that none of the available device classes match? That’s probably more of a HA issue then.

I don’t need to create a binary sensor but a sensor that is going to provide a specific value.

What sensor are you trying to read? Both digitalread() and binary_sensor only read whether a pin is high or low.

You are correct, I was confused. What I wanted to do is read an analog pin and of course there is a way to do that using the adc sensor. Thanks for pointing me in the right direction.