Creating a State

I have a ESPHome setup with a ultrasonic sensor that returns 1.5m if my car is parked and 3m if my car is not parked in the garage.

I would like to automate things if the state changes from parked to not parked and back (Turn on garage lights, etc). I’m thinking creating a custom state would be the way to go, but after googling I am quite confused as to how to proceed.

Can someone recommend me a course of action?

This is my ESPHome device’s setup:

  name: "esp-ultrasonic-1"
  friendly_name: "Garage Bay-Left"

  board: esp32dev
    type: arduino

<-- regular stuff snipped -->
- platform: ultrasonic
  trigger_pin: 32
  echo_pin: 33
  name: "Ultrasonic Sensor"
  update_interval: 5s
  timeout: 4m

Any help would be appreciated.

That sounds like a template binary sensor.

  - binary_sensor:
      - name: "Car is parked"
        state: "{{ states('sensor.ultrasonic_sensor')|float(0) < 2 }}"
        device_class: presence

I’m assuming your sensor’s entity ID as it comes into HA.

That, as with all binary sensors, will have a state of on (parked) or off (not parked). You can then create automations from that.

You could create automations directly from the state of the ultrasonic sensor (trigger when it goes from greater than 2 to less than 2), but it’s a bit tidier like this.

Thank you so much, please excuse my ignorance, I have no idea where to add the template. If I add it to the device yaml, I get a lot of errors:
Many thanks

It should be added to your configuration.yaml file in Home Assistant.

Editing Configuration.yaml

Ah yes, added it there, now I have an entity that is Away or Home, it looks like:

Does that look right ?

Yes. It’s a binary sensor, so its true states are on or off, but because you declared it with device_class: presence, the HA front end makes those to “Home” and “Away”.

Works amazing thanks guys