Use restful response in conditions?


is there a way to use a response of a restful call in an automation condition?

Use case: I have a “Nuki Lock” and want to create an automation, which locks the lock if it is unlocked. The “Nuki Lock” has a door sensor which tells, if the door is open or not. I have integrated this using the rest API as a sensor, but it happens, that the value of the sensor is not up-to-date, when the time-based automation triggers for locking the lock.

I don’t want to increase the scan interval for this sensor too much since this might use too much power of the device.

My plan: I want to create a time-based automation (e.g. each 10 minutes) with condition “Lock is unlocked” and “Door sensor says: closed” (based on an immediate Restful call.

Hope, I’ve explained my problem properly and looking forward for some ideas :slight_smile:

How about if door has been closed for 10 minutes but is unlocked, then lock it. Much simpler and no repeated unnecessary time pattern triggers. The 10 minutes should give your sensor time to update too.

- platform: state
  entity_id: binary_sensor.your_door_sensor
  to: 'off' # assuming off == closed
    minutes: 10
- condition: state 
  entity_id: binary_sensor.lock
  state: 'off' # assuming off = unlocked
 service: ...<lock your door here>

The other option is to create a template lock that combines your lock sensor and lock into one. When the lock or unlock service is called you can update the rest sensor in the actions.

Here’s an example of updating a sensor in a template switch actions I use:

- platform: template
      friendly_name: Adaptive DRC
      value_template: "{{ is_state_attr('sensor.cinema_amp', 'adaptive_drc', true ) }}"
      - service: rest_command.cinema_adaptive_drc_on
      - delay:
          seconds: 1
      - service: homeassistant.update_entity  ####### <-------- Here this bit ######
        entity_id: sensor.cinema_amp
      - service: rest_command.cinema_adaptive_drc_off
      - delay:
          seconds: 1
      - service: homeassistant.update_entity  ####### <----------and again ##########
        entity_id: sensor.cinema_amp

This ensures the switch state is quickly updated in home assistant. You could easily do the same thing in your template lock.

1 Like

Thank you for your reply. This was very useful…

1 Like