Portainer switch with api and command line?

I’m trying to create a switch to start and stop a container. I was able to figure out the right curl post command to start and stop the container but I’m struggling to figure out how to add additional information to the switch such as whether is on or off. This is what I have right now:

  - platform: command_line
    switches:
      my_container:
        friendly_name: "My Container"
        command_on: "/usr/bin/curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "endpointId=2" -H "X-API-Key:SUPER_SECRET_KEY" http://192.168.86.124:9000/api/stacks/52/start"
        command_off: "/usr/bin/curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "endpointId=2" -H "X-API-Key:SUPER_SECRET_KEY" http://192.168.86.124:9000/api/stacks/52/stop"
        command_state: "??????"
        value_template: '???'

And I can get the information with this command:

$ curl -X GET http://192.168.86.124:9000/api/stacks/52 -H "X-API-Key:SUPER_SECRET_KEY"       

{
   "Id":52,
   "Name":"chromium",
   "Type":2,
   "EndpointId":2,
   "SwarmId":"",
   "EntryPoint":"docker-compose.yml",
   "Env":[
      
   ],
   "ResourceControl":{
      "Id":110,
      "ResourceId":"2_chromium",
      "SubResourceIds":[
         
      ],
      "Type":6,
      "UserAccesses":[
         
      ],
      "TeamAccesses":[
         
      ],
      "Public":false,
      "AdministratorsOnly":true,
      "System":false
   },
   "Status":2,
   "ProjectPath":"/data/compose/52",
   "CreationDate":1700836369,
   "CreatedBy":"ignacio",
   "UpdateDate":1700836587,
   "UpdatedBy":"ignacio",
   "AdditionalFiles":null,
   "AutoUpdate":null,
   "Option":null,
   "GitConfig":null,
   "FromAppTemplate":false,
   "Namespace":"",
   "IsComposeFormat":false
}

How can I use that to have the following states:

  • on/off (Status==1 when on and 2 when off)
  • time running
  • friendly_name (in this case chromium)

Thanks for the help!

For a start, you can copy the json output to Dev Tools/Templates and play with it.


At the 3 points, i removed a part for a shorter screenshot.

The result:

So maybe try:

        command_state: "your curl GET command"
        value_template: "{{ value_json.Status == '1' }}"