Command_line & REST Switch

Hello,

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 ) }}"

command_on and command_off do not support templates.

You could create two shell commands (that do support templates) and use them as the turn_on and turn_off actions in a template switch.

1 Like

Could you explain more, or is there any way to get the currentmillis without using formula?

No, you have to use a template. Which the command line swtich does not support. So…

Create two shell commands. Use the curl commands you already have.

shell_command:
  home_door_four_on: "curl -k  https://api.ttlock.com/v3/lock/unlock?clientId=xxxxx&accessToken=xxxxx&lockId=xxxxx&date={{ (now().timestamp() | int * 1000 ) }}"
  home_door_four_off: "curl -k  https://api.ttlock.com/v3/lock/lock?clientId=xxxxx&accessToken=xxxxx&lockId=xxxxx&date={{ (now().timestamp() | int * 1000 ) }}"

Test if these work by calling them in the Developer Tools / Services page. If they do work then create your template switch using the shell commands:

switch:
  - platform: template
    switches:
      home_door_four:
        turn_on:
          service: shell_command.home_door_four_on
        turn_off:
          service: shell_command.home_door_four_off

If you can get state feedback from the switch with a command you can add that to the switch too. Read the page I linked to.

1 Like