I like to have sensor with latest game played from Discord sensor so I tried to make SQL sensor with this query but I don’t know ho to correctly escape colon
- platform: sql
queries:
- name: DimmuBoy#1658 Latest
query: "SELECT * FROM states WHERE entity_id = 'sensor.dimmuboy_1658' AND attributes NOT LIKE '%\"game\"\:null%' ORDER BY state_id DESC LIMIT 1;"
column: "state"
this returns syntax error found unknown escape character ':'
Error executing query SELECT * FROM states WHERE entity_id = 'sensor.dimmuboy_1658' AND attributes NOT LIKE '%"game":null%' ORDER BY state_id DESC LIMIT 1;: (sqlalchemy.exc.InvalidRequestError) A value is required for bind parameter 'null' [SQL: SELECT * FROM states WHERE entity_id = 'sensor.dimmuboy_1658' AND attributes NOT LIKE '%"game"?%' ORDER BY state_id DESC LIMIT 1;] [parameters: [{}]] (Background on this error at: https://sqlalche.me/e/14/cd3x)
You shouldn’t need to escape a colon or the double quotes as it’s wrapped in the like and actually a part of the attribute data.
Try this:
- platform: sql
queries:
- name: DimmuBoy#1658 Latest
query: "SELECT * FROM states WHERE entity_id = 'sensor.dimmuboy_1658' AND attributes NOT LIKE '%"game":null%' ORDER BY state_id DESC LIMIT 1;"
column: "state"
I’ve tried this with the Xbox Live data, but if you can point me in the direction of the Discord Sensor you’re using happy to play around and help.
Of course, you’re right!
It works with one replaced character but escaped quotes
- platform: sql
queries:
- name: DimmuBoy#1658 Latest
query: "SELECT * FROM states WHERE entity_id = 'sensor.dimmuboy_1658' AND attributes NOT LIKE '%game\"_null%' ORDER BY state_id DESC LIMIT 1;"
column: "state"