Grep or Sed

Am trying to parse out the value of a relay state from an output like this using sed or grep

I would like to get the state right after relay1. When I try to pipe it to grep or sed, I get extra output like this:

image

How can I simply get the state of relay 1 out of this?

curl [...] | grep "relay1" | cut -d \> -f 2 | cut -d \< -f 1

That should work. The [...] is because I cba to retype your curl command: please always include text with screenshots.

Why don’t you use a rest sensor, though?

rest:
  - resource: http://192.168.12.12/state.xml
    scan_interval: 60
    binary_sensor:
      - name: Relay 1 state
        value_template: "{{ value_json['datavalues']['relay1'] }}"
2 Likes

+1 for the REST sensor.
See Need help for pasing a xml file - #2 by tom_l for more details

Still not updating the state of the relay. I can read the status as an xml command (curl) but it still says off when it is on.

Here is my Browser return for SNMP query…

Sorry about not typing in code, will do next time. Should I also include

 json_attributes_path: "$.response.system"
        value_template: "OK"

?

Are we still on the same question here? Where did SNMP come into it, and why are you trying to set attributes?

Paste in full, formatted as code:

  • your latest sensor configuration
  • the raw XML response from curl as in your first screenshot

Just wanted to show an alternative response from the controller…

rest:
  - resource: http://192.168.12.12/state.xml
    scan_interval: 10
    binary_sensor:
      - name: Relay 1 State
        value_template: "{{ value_json['datavalues']['relay1'] }}"
curl -X GET http://192.168.12.12/state.xml 
<?xml version="1.0" encoding="utf-8" ?>
<datavalues>
<digitalInput1>0</digitalInput1>
<digitalInput2>0</digitalInput2>
<digitalInput3>0</digitalInput3>
<digitalInput4>0</digitalInput4>
<relay1>1</relay1>
<relay2>0</relay2>
<relay3>0</relay3>
<relay4>0</relay4>
<vin>12.5</vin>
<register1>0</register1>
<oneWireSensor1>x.x</oneWireSensor1>
<oneWireSensor2>x.x</oneWireSensor2>
<oneWireSensor3>x.x</oneWireSensor3>
<oneWireSensor4>x.x</oneWireSensor4>
<utcTime>1686155299</utcTime>
<timezoneOffset>-14400</timezoneOffset>
<serialNumber>00:0C:C8:04:38:0F</serialNumber>
<downloadSettings>1</downloadSettings>

Is that the complete XML? It’s not complete, with no </datavalues>. Given that it is in the screenshot, I guess you’ve just missed copying it accidentally?

If that’s right, I don’t see why my rest sensor shouldn’t work. Do you see the entity and if so, what state is it in? Anything in the logs?

curl -X GET http://192.168.12.12/state.xml 
<?xml version="1.0" encoding="utf-8" ?>
<datavalues>
<digitalInput1>0</digitalInput1>
<digitalInput2>0</digitalInput2>
<digitalInput3>0</digitalInput3>
<digitalInput4>0</digitalInput4>
<relay1>1</relay1>
<relay2>0</relay2>
<relay3>0</relay3>
<relay4>0</relay4>
<vin>12.5</vin>
<register1>0</register1>
<oneWireSensor1>x.x</oneWireSensor1>
<oneWireSensor2>x.x</oneWireSensor2>
<oneWireSensor3>x.x</oneWireSensor3>
<oneWireSensor4>x.x</oneWireSensor4>
<utcTime>1686161903</utcTime>
<timezoneOffset>-14400</timezoneOffset>
<serialNumber>00:0C:C8:04:38:0F</serialNumber>
<downloadSettings>1</downloadSettings>
</datavalues>           

The entity is relay 1 and it is 1 (on) but that doesn’t show up in the GUI of Home Assistant. Logs are:

Logs are:

2023-06-07 14:21:44.388 ERROR (SyncWorker_9) [homeassistant.components.command_line.utils] Command failed (with return code 127): “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:44.390 ERROR (MainThread) [homeassistant.components.command_line.switch] Command failed: “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:44.580 ERROR (SyncWorker_5) [homeassistant.components.command_line.utils] Command failed (with return code 127): “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:44.582 ERROR (MainThread) [homeassistant.components.command_line.switch] Command failed: “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:44.765 ERROR (SyncWorker_2) [homeassistant.components.command_line.utils] Command failed (with return code 127): “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:44.767 ERROR (MainThread) [homeassistant.components.command_line.switch] Command failed: “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:44.987 ERROR (SyncWorker_6) [homeassistant.components.command_line.utils] Command failed (with return code 127): “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:44.989 ERROR (MainThread) [homeassistant.components.command_line.switch] Command failed: “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:45.498 ERROR (SyncWorker_10) [homeassistant.components.command_line.utils] Command failed (with return code 127): “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:45.500 ERROR (MainThread) [homeassistant.components.command_line.switch] Command failed: “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:45.687 ERROR (SyncWorker_9) [homeassistant.components.command_line.utils] Command failed (with return code 127): “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:45.689 ERROR (MainThread) [homeassistant.components.command_line.switch] Command failed: “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:45.938 ERROR (SyncWorker_5) [homeassistant.components.command_line.utils] Command failed (with return code 127): “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:45.940 ERROR (MainThread) [homeassistant.components.command_line.switch] Command failed: “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:46.451 ERROR (SyncWorker_2) [homeassistant.components.command_line.utils] Command failed (with return code 127): “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:46.455 ERROR (MainThread) [homeassistant.components.command_line.switch] Command failed: “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:46.619 ERROR (SyncWorker_6) [homeassistant.components.command_line.utils] Command failed (with return code 127): “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:46.622 ERROR (MainThread) [homeassistant.components.command_line.switch] Command failed: “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:48.021 ERROR (SyncWorker_7) [homeassistant.components.command_line.utils] Command failed (with return code 127): “curl -X GET http://192.168.12.12/state.xml?relay1State=0”
2023-06-07 14:21:48.024 ERROR (MainThread) [homeassistant.components.command_line.switch] Command failed: “curl -X GET http://192.168.12.12/state.xml?relay1State=0”

- platform: command_line
  switches:
    furnace_reset:
      friendly_name: "Furnace Reset"
      command_on: “curl -X GET http://192.168.12.12/state.xml?relay1State=1”
      command_off: “curl -X GET http://192.168.12.12/state.xml?relay1State=0”

This is getting frustrating. You’re now showing us a switch that’s broken that wasn’t part of the question.

Delete or disable the switch and see if the test sensor works.

I deleted the switch but the sensor still does not work. It says off even when the relay is on.

rest:
  - resource: http://192.168.12.12/state.xml
    scan_interval: 10
    binary_sensor:
      - name: Relay 1 State
        value_template: "{{ value_json['datavalues']['relay1'] }}"

The log…

2023-06-07 17:26:26.226 WARNING (SyncWorker_2) [homeassistant.loader] We found a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2023-06-07 17:26:26.227 WARNING (SyncWorker_2) [homeassistant.loader] We found a custom integration coway which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2023-06-07 17:26:26.227 WARNING (SyncWorker_2) [homeassistant.loader] We found a custom integration fullykiosk which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2023-06-07 17:26:26.228 WARNING (SyncWorker_2) [homeassistant.loader] We found a custom integration alexa_media which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2023-06-07 17:26:33.902 WARNING (MainThread) [homeassistant.components.emulated_hue.config] Emulated Hue running in legacy mode because type has been specified. More info at https://goo.gl/M6tgz8
2023-06-07 17:27:02.702 WARNING (MainThread) [homeassistant.components.media_player] Error retrieving proxied image from http://192.168.12.197:8060/query/icon/562859

the states via terminal xml request

 [core-ssh ~]$ curl -X GET http://192.168.12.12/state.xml
<?xml version="1.0" encoding="utf-8" ?>
<datavalues>
<digitalInput1>0</digitalInput1>
<digitalInput2>0</digitalInput2>
<digitalInput3>0</digitalInput3>
<digitalInput4>0</digitalInput4>
<relay1>1</relay1>
<relay2>0</relay2>
<relay3>0</relay3>
<relay4>0</relay4>
<vin>12.5</vin>
<register1>0</register1>
<oneWireSensor1>x.x</oneWireSensor1>
<oneWireSensor2>x.x</oneWireSensor2>
<oneWireSensor3>x.x</oneWireSensor3>
<oneWireSensor4>x.x</oneWireSensor4>
<utcTime>1686173389</utcTime>
<timezoneOffset>-14400</timezoneOffset>
<serialNumber>00:0C:C8:04:38:0F</serialNumber>
<downloadSettings>1</downloadSettings>
</datavalues>          

the state on the GUI never changes from OFF

image

Ok, now we’re getting somewhere.

Can you try changing the binary_sensor: line to sensor: please — what does that sensor state then show?

Now it works! Thank you! Does this (rest) work as a switch? Should I use body_on?

Assuming your furnace supports it, your command_line switch might work if you swap out the “smart quotes” in the on/off declarations for real quotes.

image

First one is as they should be; the second two are “smart”, presumably copied from badly-formatted text on the forum somewhere? Also need to replace the ones at the ends of those lines.

Thank you once again. The switch command works now as seen below:

command_line:
  - switch:
      name: "furnaceReset"
      command_on: "/usr/bin/curl -X GET http://192.168.12.12/state.xml?relay1State=1"
      command_off: "/usr/bin/curl -X GET http://192.168.12.12/state.xml?relay1State=0"
      command_state: "curl -k http://192.168.12.12/state.xml"
      value_template: "{{ value_json['datavalues']['relay1'] }}"

The only outstanding issue (not a bid deal but curiosity is getting the better of me) is why the state of the on/off switch is not updated in the GUI?