Hi,
I am currently trying to integrate program that controls my PC/Desk led lighting via REST. I am trying to achieve this using a command line switch. Here is the config:
switch:
- platform: command_line
switches:
ambilight:
friendly_name: "Ambilight"
command_on: curl -X POST http://192.168.1.100:6969/profiles/suspend/de7e6390-c10e-4e4f-9a7b-d69c3982f61f -d "suspend=false"
command_off: curl -X POST http://192.168.1.100:6969/profiles/suspend/de7e6390-c10e-4e4f-9a7b-d69c3982f61f -d "suspend=true"
I would like to add a command_state: and value_template: section but I don’t know how to filter the response to make it useable.
Here is the code I’m trying to use:
#This is the command to get the states
curl -X GET http://192.168.1.100:6969/profiles -H 'Content-Type: application/json; charset=utf-8'
#this is the response
[
{
"IsBeingEdited" : false,
"Category" : {
"Id" : "92f465aa-8708-47bf-9262-691cc120978e",
"Name" : "General",
"IsSuspended" : false,
"Order" : 0,
"$id" : "2"
},
"Id" : "de7e6390-c10e-4e4f-9a7b-d69c3982f61f",
"$id" : "1",
"IsActive" : false,
"Order" : 0,
"IsSuspended" : true,
"Name" : "Ambilight",
"IsMissingModule" : false,
"HasActivationCondition" : true,
"ActivationConditionMet" : true
},
{
"IsBeingEdited" : false,
"Category" : {
"Id" : "92f465aa-8708-47bf-9262-691cc120978e",
"Name" : "General",
"IsSuspended" : false,
"Order" : 0,
"$id" : "4"
},
"Id" : "9d841a42-255f-46fa-9494-fada1ec685d1",
"$id" : "3",
"IsActive" : false,
"Order" : 1,
"IsSuspended" : true,
"Name" : "Rainbow - copy",
"IsMissingModule" : false,
"HasActivationCondition" : true,
"ActivationConditionMet" : false
},
{
"IsBeingEdited" : false,
"Category" : {
"Id" : "92f465aa-8708-47bf-9262-691cc120978e",
"Name" : "General",
"IsSuspended" : false,
"Order" : 0,
"$id" : "6"
},
"Id" : "41b207dd-3b5a-4c80-ad59-d0a44a1f40e7",
"$id" : "5",
"IsActive" : false,
"Order" : 2,
"IsSuspended" : true,
"Name" : "Noise",
"IsMissingModule" : false,
"HasActivationCondition" : true,
"ActivationConditionMet" : false
},
{
"IsBeingEdited" : false,
"Category" : {
"Id" : "92f465aa-8708-47bf-9262-691cc120978e",
"Name" : "General",
"IsSuspended" : false,
"Order" : 0,
"$id" : "8"
},
"Id" : "4f5ecfdf-2cd9-4d68-8a2a-648137715779",
"$id" : "7",
"IsActive" : false,
"Order" : 3,
"IsSuspended" : true,
"Name" : "Rainbow",
"IsMissingModule" : false,
"HasActivationCondition" : true,
"ActivationConditionMet" : false
}
]
I am having trouble figuring out to how to filter the response down to the IsActive or IsSuspended value of the first profile.
Edit:
I have figured out how to get the value I want using a value template. It works in the template editor but when I try and use it in a switch or sensor it returns unknown. Here is the code I am working with currently
sensor:
- platform: rest
name: Ambilight
resource: http://192.168.1.100:6969/profiles
scan_interval: 5
value_template: "{{ value_json[0].IsActive }}"
#or
switch:
- platform: command_line
switches:
ambilight:
friendly_name: "Ambilight"
command_on: curl -X POST http://192.168.1.100:6969/profiles/suspend/de7e6390-c10e-4e4f-9a7b-d69c3982f61f -d "suspend=false"
command_off: curl -X POST http://192.168.1.100:6969/profiles/suspend/de7e6390-c10e-4e4f-9a7b-d69c3982f61f -d "suspend=true"
command_state: curl -X GET http://192.168.1.100:6969/profiles
value_template: "{{ value_json[0].IsActive }}"
When I try this ↓ in the template editor I get the correct boolean value. Why doesn’t work in a switch?
{% set value_json = [{"$id":"1","Id":"de7e6390-c10e-4e4f-9a7b-d69c3982f61f","Name":"Ambilight","Order":0,"IsActive":false,"IsSuspended":true,"IsBeingEdited":false,"IsMissingModule":false,"HasActivationCondition":true,"ActivationConditionMet":true,"Category":{"$id":"2","Id":"92f465aa-8708-47bf-9262-691cc120978e","Name":"General","Order":0,"IsSuspended":false}},{"$id":"3","Id":"9d841a42-255f-46fa-9494-fada1ec685d1","Name":"Rainbow - copy","Order":1,"IsActive":false,"IsSuspended":true,"IsBeingEdited":false,"IsMissingModule":false,"HasActivationCondition":true,"ActivationConditionMet":true,"Category":{"$id":"4","Id":"92f465aa-8708-47bf-9262-691cc120978e","Name":"General","Order":0,"IsSuspended":false}},{"$id":"5","Id":"41b207dd-3b5a-4c80-ad59-d0a44a1f40e7","Name":"Noise","Order":2,"IsActive":false,"IsSuspended":true,"IsBeingEdited":false,"IsMissingModule":false,"HasActivationCondition":true,"ActivationConditionMet":false,"Category":{"$id":"6","Id":"92f465aa-8708-47bf-9262-691cc120978e","Name":"General","Order":0,"IsSuspended":false}},{"$id":"7","Id":"4f5ecfdf-2cd9-4d68-8a2a-648137715779","Name":"Rainbow","Order":3,"IsActive":false,"IsSuspended":true,"IsBeingEdited":false,"IsMissingModule":false,"HasActivationCondition":true,"ActivationConditionMet":false,"Category":{"$id":"8","Id":"92f465aa-8708-47bf-9262-691cc120978e","Name":"General","Order":0,"IsSuspended":false}}] %}
{{ value_json[0].IsActive }}
Edit 2:
Checked the logs. Parsing error. Trying to fix.
Error parsing value: 'value_json' is undefined (value: [{"$id":"1","Id":"de7e6390-c10e-4e4f-9a7b-d69c3982f61f","Name":"Ambilight","Order":0,"IsActive":true,"IsSuspended":false,"IsBeingEdited":false,"IsMissingModule":false,"HasActivationCondition":true,"ActivationConditionMet":true,"Category":{"$id":"2","Id":"92f465aa-8708-47bf-9262-691cc120978e","Name":"General","Order":0,"IsSuspended":false}},{"$id":"3","Id":"9d841a42-255f-46fa-9494-fada1ec685d1","Name":"Rainbow - copy","Order":1,"IsActive":false,"IsSuspended":true,"IsBeingEdited":false,"IsMissingModule":false,"HasActivationCondition":true,"ActivationConditionMet":true,"Category":{"$id":"4","Id":"92f465aa-8708-47bf-9262-691cc120978e","Name":"General","Order":0,"IsSuspended":false}},{"$id":"5","Id":"41b207dd-3b5a-4c80-ad59-d0a44a1f40e7","Name":"Noise","Order":2,"IsActive":false,"IsSuspended":true,"IsBeingEdited":false,"IsMissingModule":false,"HasActivationCondition":true,"ActivationConditionMet":false,"Category":{"$id":"6","Id":"92f465aa-8708-47bf-9262-691cc120978e","Name":"General","Order":0,"IsSuspended":false}},{"$id":"7","Id":"4f5ecfdf-2cd9-4d68-8a2a-648137715779","Name":"Rainbow","Order":3,"IsActive":false,"IsSuspended":true,"IsBeingEdited":false,"IsMissingModule":false,"HasActivationCondition":true,"ActivationConditionMet":false,"Category":{"$id":"8","Id":"92f465aa-8708-47bf-9262-691cc120978e","Name":"General","Order":0,"IsSuspended":false}}], template: {{ value_json[0].IsActive }})
Edit 3:
Error is
TemplateSyntaxError: unexpected char '\ufeff' at 20
I suspect this has something to do with utf-8 encoding, I’ve added ↓ to the sensor entry but the error still persists.
Content-Type: application/json;charset=utf-8
Accept-encoding: gzip