Automating on state change, escape character issue?

I’m trying to automate on the change of a state. From any state to the state below but I can’t get it to work. I’m guessing that it’s a problem of escaping characters?

HTTP/1.1 200 OK Content-Type: application/json Content-Length: 110 Connection: keep-alive {"result":[{"uri":"extInput:cec?type=recorder&port=3&logicalAddr=1","source":"","title":"CoreELEC"}],"id":103}

Any help would be appreciated :slight_smile:

can you provide a bit more information please. Is everything you posted the full state, or is it the json only? Also, post the entity that has this information in the developer tools state page. That will tell me where the information is.

This is the whole state as seen in dev tools:

So basically if the state changes to this I want to trigger the automation :slight_smile:

Ok, so you’re going to run into issues with your full state being json. States are limited to 255 characters, and your state will be chopped off while producing an error.

Please post the configuration for sensor.test. We can adjust it to avoid that situation.

This is the sensor below:

sensor:
  - platform: command_line
    name: test
    command: curl -i -H 'X-Auth-PSK:password' -d '{"method":"getPlayingContentInfo","id":103,"params":[],"version":"1.0"}' http://192.168.0.251/sony/avContent
    scan_interval: 1
    value_template: '{{ value }}'

is there information in the JSON that you’re specifically looking for?

The only thing I really need to know is this part:

"title":"CoreELEC"

ok, give me a moment

change your value template to

value_template: >
  {{ value | regex_findall('"title":"([a-zA-Z0-9 ]+)"') | first | default(none) }}

Make sure you do not use a single line template. You have to use the > and put the template on the next line. I.e. Copy what I wrote, do not change it.

The state of your sensor will now be the title only.

Hmm, state is reporting “none”.
Maybe my syntax is wrong:

sensor:
  - platform: command_line
    name: test
    command: curl -i -H 'X-Auth-PSK:password' -d '{"method":"getPlayingContentInfo","id":103,"params":[],"version":"1.0"}' http://192.168.0.251/sony/avContent
    scan_interval: 1
    value_template: >
     {{ value | regex_findall('"title":"([a-zA-Z0-9 ]+)"') | first | default(none) }}

You’ll never get none as a state. It will be unavailable or the title.

This is what dev tools looks like now:

That makes no sense, the result should be unavailabe. Are you using a custom integration for command_line?

aside from that, can you post the result of the actual command from CLI? It doesn’t make sense that it’s working for me and not for you.

I don’t think I am.

How would I test using CLI, or do you mean using CMD in Windows?

How did you create the command and test it?

I used powershell and then moved it over to HA :slight_smile:

Yeah, then do the call now and post the formatted result exactly as it’s written (from powershell)

This is from Powershell using invoke-webrequest:

StatusCode        : 200
StatusDescription : OK
Content           : {"result":[{"uri":"extInput:cec?type=recorder&port=3&logicalAddr=1","source":"","title":"CoreELEC"}],"id":103}
RawContent        : HTTP/1.1 200 OK
                    Connection: keep-alive
                    Content-Length: 110
                    Content-Type: application/json
                    
                    {"result":[{"uri":"extInput:cec?type=recorder&port=3&logicalAddr=1","source":"","title":"CoreELEC"}],"id...
Forms             : {}
Headers           : {[Connection, keep-alive], [Content-Length, 110], [Content-Type, application/json]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 110

I’ve must’ve made a mistake when pasting your code earlier because now it works :slight_smile:

Thank you so much for the help!! :slight_smile:

1 Like