Setting entity status values

Summary:
I want to be able to set a entity state and as much as I have looked I cannot figure out how to change the state of a garage door entity status.

Details:
I have a garage door that can be opened in more than one way, including Home Assistant.
When it is opened in a way other than Home Assistant I have an ultrasonic sensor to detect if the door is open and want to be able to change the state from “Closed” to “Open”.

I can set it in the “Developer Tools”->STATES and can change the cover.garage_door entity to state open or closed with no problem. I just do not know how to do it from the ultrasonic sensor.

sensor:
  - platform: ultrasonic
    trigger_pin: GPIO4 # D2 
    echo_pin: GPIO5    # D1
    name: "Garage Door Distance Sensor"
    id: garage_distance_sensor
    update_interval: 60s
    internal: false
    filters:
    #  The ultrasonic sensor returns null over ~4 feet.
    #  I mount the sensor above the door, have tested and know the following works.
    #  I just cannot figure out how to get the cover.garage_door status updated.
      lambda: |-
        if (x>0) {
          return COVER_OPEN; 
        } else {
          return COVER_CLOSED; 
        }

What platform is your cover?

I am running Home Assistant on a Raspberry Pi and the ultrasonic is ESPHome running on a ESP8266.

What platform is your cover?

Like what make? which integration?

Pick one :slight_smile:

Typically you’d create a binary sensor from the ultrasonic sensor (to get open/closed).

Then you’d use that binary sensor to drive the state of a cover.

I am thinking a Template Cover - Home Assistant is likely to work, but finding out what @SmartMyHome is using so far.

1 Like

Let me clarify my question…

I want to be able to change the cover.garage_door state to OPEN or CLOSED based on the sensor reading.

When using the HA garage door switch there is no problem with the state, it gets changed automatically with no problem. But when my wife uses her garage door remote (that came with the garage door and I still want to be able to use) I want the ultrasonic to update the door state.

ESPHome is one of the integrations.

My suggestion will probably work. It’s basically the same as what I do with my blinds which I also have a remote for.

Personally I’d go for an ESPHome cover if the garage door relay and distance sensor are on the same esp (this would typically be more reliable), or a HA cover if the garage door and distance sensor need to talk to each other via HA.

More info about hardware and software would help us help. Especially as Nick has mentioned, whether you are using a HA or a ESPHome cover, and which one. But also the details of your cover - does it have other sensors for open/closed like endstops?

Right, so you have an esphome cover, thanks for clarifying.

Which one, there are 8 types, perhaps just post your yaml

I am still not getting the question I have across, so let me try again.

I have a working ESPHome garage door set up. It works fine. It has a single relay which closes for 1 sec and then opens up again. That is the trigger for both opening and closing the door.

When using HA to open and close the door, it works perfectly. The relay opens and closes and the garage door goes from Closed to Open or Open to Closed.

image

image

However… if my wife opens the garage door with her older garage door relay, the garage door is open but HA shows it as closed, because HA does not know it has been opened The ultrasonic sensor detection reads 1 when closed and 0 when open because of a logic I put in.

image

How do I change the status of the variable cover.garage_door when the sensor detects the change?

You cannot programmatically change the state of an entity except from the entity’s code. That is why I keep asking you how your existing garage door is implemented. You have got as far as telling us that it is esphome. Now post your yaml.

2 Likes

We understand your question.

We just aren’t sure if it’s the right one.

This is why we are asking you probing questions.

2 Likes

Here is all the yaml file, excluding api, wifi, etc.

esphome:
  name: garage-door

esp8266:
  board: esp01_1m

sensor:
  - platform: ultrasonic
    trigger_pin: GPIO4 # D2 
    echo_pin: GPIO5    # D1
    name: "Garage Door Distance Sensor"
    id: garage_distance_sensor
    update_interval: 60s
    internal: false
    on_value_range:  # My attempt to change the state, but its not working.
      - above: 0
        then:
          - logger.log: "Setting to open based on ultrasonic ----------------"
          - cover.template.publish:
              id: garage_door
              state: OPEN
      - below: 0
        then:
          - logger.log: "Setting to Closed based on ultrasonic ----------------"
          - cover.template.publish:
              id: garage_door
              state: CLOSED


switch:
  - platform: gpio
    pin: GPIO14  #D5
    name: "Garage Door Open Switch"
    id: garage_switch
  - platform: gpio
    pin: GPIO12  #D6
    name: "Garage Door Close Switch"
    id: close_switch

cover:
  - platform: template
    name: "Garage Door"
    id: garage_door
    open_action:
         - logger.log: '------- Logging Opening'
         - switch.turn_on: garage_switch
         - delay: 1s
         - switch.turn_off: garage_switch
    close_action:
        - logger.log: "----- Logging Closing"
        - switch.turn_on: garage_switch
        - delay: 1s
        - switch.turn_off: garage_switch
    stop_action:
      - logger.log: "----- Logging Stop"
      - switch.turn_on: garage_switch
      - delay: 1s
      - switch.turn_off: garage_switch
    optimistic: True
    assumed_state: false

# Enable logging
logger:

Looks kinda right. Are you getting the logging of “Setting to open…”?

Could you try the lambda version?

state: !lambda 'return COVER_OPEN;'

Unfortunately it looks like the “on_value_range” is not working.
I put logging into the “above 0” and “below 0” sections, but the works “Setting to xxx based on ultrasonic…” is not getting onto the log.

Here is a copy of the log. I triggered the open button and then the close button, but as you can see the ultrasonic settings are not working.

[20:38:26][D][ultrasonic.sensor:036]: 'Garage Door Distance Sensor' - Distance measurement timed out!
[20:38:26][D][sensor:126]: 'Garage Door Distance Sensor': Sending state nan m with 2 decimals of accuracy
[20:39:26][D][ultrasonic.sensor:040]: 'Garage Door Distance Sensor' - Got distance: 0.84 m
[20:39:26][D][sensor:126]: 'Garage Door Distance Sensor': Sending state 0.83658 m with 2 decimals of accuracy
[20:39:42][D][cover:076]: 'Garage Door' - Setting
[20:39:42][D][cover:086]:   Command: OPEN
[20:39:42][D][main:405]: ------- Logging Opening
[20:39:42][D][switch:013]: 'Garage Door Open Switch' Turning ON.
[20:39:42][D][switch:056]: 'Garage Door Open Switch': Sending state ON
[20:39:42][D][cover:170]: 'Garage Door' - Publishing:
[20:39:42][D][cover:176]:   State: OPEN
[20:39:42][D][cover:186]:   Current Operation: IDLE
[20:39:43][D][switch:017]: 'Garage Door Open Switch' Turning OFF.
[20:39:43][D][switch:056]: 'Garage Door Open Switch': Sending state OFF
[20:39:59][D][cover:076]: 'Garage Door' - Setting
[20:39:59][D][cover:086]:   Command: CLOSE
[20:39:59][D][main:416]: ----- Logging Closing
[20:39:59][D][switch:013]: 'Garage Door Open Switch' Turning ON.
[20:39:59][D][switch:056]: 'Garage Door Open Switch': Sending state ON
[20:39:59][D][cover:170]: 'Garage Door' - Publishing:
[20:39:59][D][cover:178]:   State: CLOSED
[20:39:59][D][cover:186]:   Current Operation: IDLE
[20:40:00][D][switch:017]: 'Garage Door Open Switch' Turning OFF.
[20:40:00][D][switch:056]: 'Garage Door Open Switch': Sending state OFF
[20:40:26][D][ultrasonic.sensor:040]: 'Garage Door Distance Sensor' - Got distance: 0.84 m
[20:40:26][D][sensor:126]: 'Garage Door Distance Sensor': Sending state 0.83692 m with 2 decimals of accuracy

I still think this would work just fine.

Just need to build a binary sensor from the Distance sensor and pop it in the value_template. Then it should manage it’s own state.

And weave in any other sources of open/closed detection.

But I don’t have much more to add to your preffered solution, so good luck finding something that works for you.

Another option to consider is a reed sensor connected to something like a shelly. I use this setup for my garage door and it works like a champ.