No valid return value using template switch and philips hue vtoggle

Hey guys,

I have a strange problem and I do not succeed to create a switch for a Philips Hue API action.
It seems like Home Assistant dos not get a valid return value from the value_template, but I don’t know why.
I hope somebody can help me in this case…

Philips Hue Clip Debug Tool output of a virtual toggle
This toggle is able to start a hue automation to dimm through a pre-defined color pallette.

/api/GenericAPIKey/sensors/38

{
	"state": {
		"status": 1,
		"lastupdated": "2019-01-28T18:05:48"
	},
	"config": {
		"on": true,
		"reachable": true
	},
	"name": "Start and stop",
	"type": "CLIPGenericStatus",
	"modelid": "HUELABSVTOGGLE",
	"manufacturername": "Philips",
	"swversion": "2.0",
	"uniqueid": "2:12:53dd-002b-493e-a07d",
	"recycle": true
}

Configuration in switches.yaml

  hue_vtoggle_38_state:
	friendly_name: Hue Labs Relax
	command_on: curl -X PUT -d '{"status":1}' "http://HueBridge.local/api/GenericAPIKey/sensors/38/state"
	command_off: curl -X PUT -d '{"status":0}' "http://HueBridge.local/api/GenericAPIKey/sensors/38/state"
	command_state: curl "http://HueBridge.local/api/GenericAPIKey/sensors/38"
	value_template: '{{value_json.state.status}}'

Result:
command_on and command_off are working fine.
But the value_template is causing a permanent on-state.

Template Development Tool
So I used the template development tool of hass.io to check my template against any syntax misstakes.

{% set value_json=
     {"state":
	    {"status":"1",
		 "lastupdated":"2019-01-28T08:57:05"
		}	}%}

{{value_json.state.status}}

Result:
The review confirms me the correctness of my template and I always got the correct return value of hass.io even if I changed the status value.

In despair I tested more templates in my switches.yaml - without success…

  hue_vtoggle_38_state:
	friendly_name: Hue Labs Relax
	command_on: curl -X PUT -d '{"status":1}' "http://HueBridge.local/api/GenericAPIKey/sensors/38/state"
	command_off: curl -X PUT -d '{"status":0}' "http://HueBridge.local/api/GenericAPIKey/sensors/38/state"
	command_state: curl "http://HueBridge.local/api/GenericAPIKey/sensors/38"
	value_template: >
	  {% if '{{value_json.state.status}}' %} 
		true
	  {% else %}
		false
	  {% endif %}

Result:
The switch always has the state “ON”

  hue_vtoggle_38_state:
	friendly_name: Hue Labs Relax
	command_on: curl -X PUT -d '{"status":1}' "http://HueBridge.local/api/GenericAPIKey/sensors/38/state"
	command_off: curl -X PUT -d '{"status":0}' "http://HueBridge.local/api/GenericAPIKey/sensors/38/state"
	command_state: curl "http://HueBridge.local/api/GenericAPIKey/sensors/38"
	value_template: >
	  {% if '{{value_json.state.status}}' == '1' %} 
		true
	  {% else %}
		false
	  {% endif %}

Result:
The switch always has the state “OFF”

  hue_vtoggle_38_state:
	friendly_name: Hue Labs Relax
	command_on: curl -X PUT -d '{"status":1}' "http://HueBridge.local/api/GenericAPIKey/sensors/38/state"
	command_off: curl -X PUT -d '{"status":0}' "http://HueBridge.local/api/GenericAPIKey/sensors/38/state"
	command_state: curl "http://HueBridge.local/api/GenericAPIKey/sensors/38"
	value_template: >
	  {% if '{{value_json.state.status}}' != '1' %} 
		true
	  {% else %}
		false
	  {% endif %}

Result:
The switch always has the state “ON”

Can anyone help me? I am at a dead end now and i am completely freaking out!..
Maybe there is a way to log the return values of value_template so that i am able to debug it on a better way?

Try:

	value_template: '{{value_json.state.status == 1}}'

Note that value_json.state.status is the number 1, not the string '1' (at least according to the JSON result you showed above.) Also, a command line switch will be ‘on’ if value_template returns ‘true’ (case insensitive.) So you need to test for value_json.state.status being equal to the number 1. The template '{{value_json.state.status == 1}}' will return 'true' or 'false', which will satisfy the command line switch’s value_template.

Fast and accurate - Now I feel stupid! :smiley:
Thank you very much! It worked fine!

1 Like