简体   繁体   中英

Home Assistant: Command_line & REST Switch

I am trying to unlock my door through HA, it's a straightforward HTTPS link but in the last variable of the link "date=" we need to assign the current date, which I think is the one that is causing issues with my below code:

switch:
  - platform: command_line
    switches:
      home_door_four:
        command_on: "curl -k  https://api.ttlock.com/v3/lock/unlock?clientId=xxxxx&accessToken=xxxxx&lockId=xxxxx&date={{ (now().timestamp() | int * 1000 ) }}"
        command_off: "curl -k  https://api.ttlock.com/v3/lock/lock?clientId=xxxxx&accessToken=xxxxx&lockId=xxxxx&date={{ (now().timestamp() | int * 1000 ) }}"

I got this working using the rest_command and template lock integration.

NB: This is just a snippet of my code, I'm sure you can fill in the blanks, I also use a door sensor and also get the lock status from the API.

rest_command:
  unlock:
    method: POST
    content_type: "application/x-www-form-urlencoded"
    url: >
      https://api.ttlock.com/v3/lock/unlock?clientId=xxxxxxx&accessToken=xxxxxxx&lockId=xxxxxxx&date={{ as_timestamp(now()) | int * 1000 }}

Then create the lock:

lock:
  - platform: template
    name: myLock
    optimistic: true
    lock:
      service: rest_command.lock
    unlock:
      service: rest_command.unlock

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM