Single custom button card with toggle switch

Hello All,

I have two separate standard buttons that execute a single command each to set PUSH alerts ON and OFF for a Reolink NVR:

rest_command:
  reolink_push_on:
    url: http://192.168.1.55/api.cgi?cmd=SetPushV20&user=[USER]&password=[PASSWORD]
    payload: '[{"cmd":"SetPushV20","param":{"Push":{"enable":1}}}]'
  reolink_push_off:
    url: http://192.168.1.55/api.cgi?cmd=SetPushV20&user=[USER]&password=[PASSWORD]
    payload: '[{"cmd":"SetPushV20","param":{"Push":{"enable":0}}}]'

Currently, every command (reolink_push_on and reolink_push_off) are included into two different buttons, and they work fine. The PUSH stated is displayed in a separate sensor.reolink_push entity entry in lovelace:

sensor:
  - platform: rest
    resource: http://192.168.1.55/api.cgi?cmd=GetPushV20&user=[USER]&password=[PASSWORD]
    name: Reolink Push
    value_template: '{{ value_json[0].value.Push.enable | regex_replace(find="0", replace="Deshabilitado", ignorecase=False) | regex_replace(find="1", replace="Habilitado", ignorecase=False) }}'
    method: GET

In order to optimize my lovelace dashboard look&feel, what I would like to do is to have one single HACS custom button card with a toggle switch, for it to enable/disable PUSH alerts on a Reolink NVR and display its state with a color or icon change. So far, I have coded the following for the single button:

type: custom:button-card
entity: sensor.reolink_push
icon: mdi:cellphone-message
show_name: false
color: rgb(0, 122, 193)
state:
  - value: Enabled
    color: rgb(248, 216, 89)
tap_action:
[???]

I have been able to do other “standard” custom buttons with no issues, but I could code them with standard toggle switches or automation toggles. However, I am not sure how to complete the code for these specific two REST commands above in the first piece of code for the button to be handled as a toggle switch. I have been reading the documentation related to Command Line Switches or RESTful ones, but I am not sure how to do it for this specific scenario. Any help on how to convert the two rest_commands into a switch or other alternative, please?

Trying to move forward on my own while getting a response, I have included the following in config.yaml in order to create a command_line switch:

switch:
  - platform: command_line
    switches:
      sw_reolink_push:
        command_on: "curl -H "Content-Type: application/json" -X POST -d '[{"cmd":"SetPushV20","param":{"Push":{"enable":1}}}]' http://192.168.1.55/api.cgi?cmd=SetPushV20&user=[USER]&password=[PASSWORD]"
        command_off: "curl -H "Content-Type: application/json" -X POST -d '[{"cmd":"SetPushV20","param":{"Push":{"enable":0}}}]' http://192.168.1.55/api.cgi?cmd=SetPushV20&user=[USER]&password=[PASSWORD]"

However, I receive the following error when checking configuration before restarting:

Error loading /config/configuration.yaml: while parsing a block mapping in “/config/configuration.yaml”, line 158, column 9 expected , but found ‘’ in “/config/configuration.yaml”, line 158, column 31. FYI, line 158 is the command_on line.

What I don’t understand is that if I check the command_on line in this curl tester web, the command is executed properly. Any advice?

You have quote issues.

Try

switch:
  - platform: command_line
    switches:
      sw_reolink_push:
        command_on: 'curl -H "Content-Type: application/json" -X POST -d ''[{"cmd":"SetPushV20","param":{"Push":{"enable":1}}}]'' http://192.168.1.55/api.cgi?cmd=SetPushV20&user=[USER]&password=[PASSWORD]'
        command_off: 'curl -H "Content-Type: application/json" -X POST -d ''[{"cmd":"SetPushV20","param":{"Push":{"enable":0}}}]'' http://192.168.1.55/api.cgi?cmd=SetPushV20&user=[USER]&password=[PASSWORD]'

That being said, instead of a command_line switch with curl, you should probably use

try this and see how you like it:

Thank you @koying! Now, I don’t get any validation errors upon checking configuration. However, it looks like curl command is not issued properly for unknown reasons. If I try the command in my docker terminal, the command response is related with some sort of credential issue. It is like it wasn’t getting credentials from the short/quick URL form. When I use such syntax in REST command as stated earlier, everything works.

In order to move forward, now I am trying the RESTful switch as suggested. The body_on and body_off strings are the same ones that I have on the REST commands and sensors that I have been using so far. However, after including the following code in my config.yaml, there is no related entity in the entities list:

switch:
  - platform: rest
    resource: http://192.168.1.55/api.cgi?cmd=SetPushV20&user=[USER]&password=[PASSWORD]
    state_resource: http://192.168.1.55/api.cgi?cmd=GetPushV20&user=[USER]&password=[PASSWORD]
    body_on: '[{"cmd":"SetPushV20","param":{"Push":{"enable":1}}}]'
    body_off: '[{"cmd":"SetPushV20","param":{"Push":{"enable":0}}}]'
    is_on_template: '{{ value_json[0].value.Push.enable | regex_replace(find="0", replace="Deshabilitado", ignorecase=False) | regex_replace(find="1", replace="Habilitado", ignorecase=False) }}'
    headers:
      Content-Type: application/json

Any advice?

Thank you, @finity. Will try that if I cannot achieve it using the standard HA options, and will definitely have a look at it for future reference.

Finally, I could do it! The one I managed to make it work is the command_line option. The problem was related to quotes missing in the URL part. After doing some tests in terminal, it seems URLs with variables or ampersands are not being handled correctly. In order to make it work, I had to use the following code:

switch:
  - platform: command_line
    switches:
      sw_reolink_push:
        command_on: 'curl -H "Content-Type: application/json" -X POST -d ''[{"cmd":"SetPushV20","param":{"Push":{"enable":1}}}]'' "http://192.168.1.55/api.cgi?cmd=SetPushV20&user=[USER]&password=[PASS]"'
        command_off: 'curl -H "Content-Type: application/json" -X POST -d ''[{"cmd":"SetPushV20","param":{"Push":{"enable":0}}}]'' "http://192.168.1.55/api.cgi?cmd=SetPushV20&user=[USER]&password=[PASS]"'

Thank you @koying for your reply, as it helped me to move forward and find the right path on the specific issue!

Having said that, I would love to know how to fix the RESTful Switch option. Adding quotes in the same way I did for the command_line option does not fix the issue. This is the error I get in the logs:

Logger: homeassistant.components.rest.switch
Source: components/rest/switch.py:116
Integration: RESTful (documentation, issues)
First occurred: 19:13:36 (1 occurrences)
Last logged: 19:13:36

No route to resource/endpoint: http://192.168.1.55/api.cgi?cmd=SetPushV20&user=[USER]&password=[PASS]

Such URL responses when curling from terminal, so endpoint can be reached.