Combining sensor.x and switch.x button-card

Trying to get sensor data to know the initial (and subsequent) states of a relay for a toggle switch using button-card. My sensor data looks like this:

 - resource: http://192.168.12.11/state.xml
    scan_interval: 32
    sensor:
      - name: Master Bedroom Chimney Fan
        value_template: "{{ value_json['datavalues']['relay1state'] }}"

and my switch data looks like:

- switch:
      name: "Master Bedroom Chimney Fan"
      command_on: "/usr/bin/curl -X GET http://192.168.12.11/state.xml?relay1=1"
      command_off: "/usr/bin/curl -X GET http://192.168.12.11/state.xml?relay1=0"
      command_state: curl -X GET http://192.168.12.11/state.xml

Is there a way to use the sensor data (which is correct in showing the intial and current state) for the icon and use a toggle action (or equivalent) using the switch call?

Here is my vutton-card code thus far:

type: custom:button-card
entity: switch.master_bedroom_chimney_fan
icon: mdi:lamp
color_type: card
color: rgb(255,0,15)
aspect_ratio: .8/.2
state:
  - value: 'on'
    color: rgb(255, 0, 0)
  - value: 'off'
    color: rgb(0,225,0)
tap_action:
  action: toggle
triggers_update: all
show_state: false

As of now, not even toggle switches the state of the relay, all it does is turns it on if it is off, but I cannot turn it off pressing the same button. Also, the initial or subsequent states of the relay are never updated (i.e. change in color).

Why aren’t you using the value_template to assign the switch’s state the proper value from the state.xml like you have with the sensor’s?

I don’t use the command line integration very often, but it should be something like:

command_line:
- switch:
    name: "Master Bedroom Chimney Fan"
    command_on: "/usr/bin/curl -X GET http://192.168.12.11/state.xml?relay1=1"
    command_off: "/usr/bin/curl -X GET http://192.168.12.11/state.xml?relay1=0"
    command_state: curl -X GET http://192.168.12.11/state.xml
    value_template: "{{ value_json['datavalues']['relay1state'] }}"

If for some reason that doesn’t work, I think you can use value_template to overwrite the state of the switch using the state of the sensor:

command_line:
- switch:
    name: "Master Bedroom Chimney Fan"
    command_on: "/usr/bin/curl -X GET http://192.168.12.11/state.xml?relay1=1"
    command_off: "/usr/bin/curl -X GET http://192.168.12.11/state.xml?relay1=0"
    value_template: "{{ states('sensor.master_bedroom_chimney_fan') }}"

Thanks for your response, but neither seems to work. I had tried your first suggestion before, but it didn’t work. I was trying to use is_state or, as someone suggested, a template but I am new to this and am getting a bit confused on templates and their uses.

 value_template: "{{ states('sensor.master_bedroom_chimney_fan') }}"

will give the correct state if I place it in developer tools-> template and see the output. The problem is any switch that I use does not give a correct initial state and the slider switch card included in overview has its initial state of the relay as off, even if on. Further, when I turn the switch on, it changes the relay state to on, but slides back to off on the slider switch card.

Could the return of states be the problem? That is, states returns a string, should I convert it to true/false?