Help: Set custom switch entity state depending on shell_script return value

I have the following custom switch set up:

# "Template Switch" integration (for creating custom switches):
switch:
  # GUDE EPC 8211 port 06:
  - platform: template
    switches:
      gude_epc_8211_port06:
        turn_on:
          action: shell_command.gude_epc_8211_port06_poweron
        turn_off:
          action: shell_command.gude_epc_8211_port06_poweroff

This works to turn on/off, but the problem is that HA is not aware on the device’s real on/off state because it is also possible to turn on/off the physical device via a physical button.
Therefore I did set up the following shell_script command, which is returning either a value of 1 or 0, depending on the device’s on/off state:

gude_epc_8211_port06_getstate_command: "curl -s -X GET 'http://user:[email protected]/statusjsn.js?components=1' | jq '.outputs[] | select(.name == 'powerStrip10') | .state'"

Now my question is: How would I set the state on the custom switch entity depending on what value is being returned from the CURL shell script? I thought of a template helper, but I am not sure on how to code this. Can someone help me out with this?
Thanks in advance!

yeah, you could do a template sensor. you’d have to determine when you want to update that sensor. some poll frequency? here’s a general idea to get you going:

template:
  - trigger:
      - platform: time_pattern
        # This will update every minute
        minutes: /1
    action: 
       ### put your shell_command here
       - action: shell_command.getstate
         response_variable: shell_command_response
    sensor:
      - name: "name of sensor"
        state: >
           {{ jinja to parse shell_command_response }}

info about shell_command response_variable and examples here:

I’m not sure if I understand this correctly.
So the sensor in your example will automatically update the already existing switch entity state?
If so, the code would look like this?

template:
  - trigger:
      - platform: time_pattern
        # This will update every minute
        minutes: /1
    action: 
       ### put your shell_command here
       - action: shell_command.gude_epc_8211_port06_getstate
         response_variable: shell_command_response
    sensor:
      - name: "test"
        state: >
           {{ switch.gude_epc_8211_port06 = shell_command_response }}

Not sure about this but would HA automatically interpret a “1” as “on” and a “0” as “off”?

hmm… given your response, am i correct to guess that you’re not familiar with jinja and templating in home assistant? i don’t want to presume too much or also certainly don’t mean to offend if you’re quite jinja capable.

no, that won’t work. that’s not how jinja or sensor templates work. the state should get the value you want. the response_variable receives the value in a dictionary. look at the link i gave above. it explains it and also gives examples. i don’t know your script and how it returns, but to take a wild guess

template:
  - trigger:
      - platform: time_pattern
        # This will update every minute
        minutes: /1
    action: 
       ### put your shell_command here
       - action: shell_command.gude_epc_8211_port06_getstate
         response_variable: shell_command_response
    sensor:
      - name: "test"
        state: >
           {{ shell_command_response['returncode'] == 1 }}