Rest Sensor - won't login but works in Postman

I have the following Rest Sensor set which does show up in HA. The issue is that it won’t login to provide me with the info I need (accessToken) and the ‘Success’ attribute always shows as False. I have run the info through Postman and it returns True with all other attributes. Any ideas?

  - platform: rest
    name: kaluza_login
    resource_template: https://identity.gridsvc.net/login
    force_update: true
    headers:
      content_type: "application/json"
      Accept: "application/json"
      Accept-Language: "en-gb"
      Origin: https://cev.kaluza.com
      Referer: https://cev.kaluza.com/
      Host: "identity.gridsvc.net"
      Connection: "keep-alive"
    method: POST
#    payload: !secret kaluza_login
    payload: '{ "username": "[email protected]","password": "xxxxx","ignoreChallenges": true }'
    value_template: "{{ value_json.accessToken }}"
    json_attributes:
      - success
      - accessToken
      - refreshToken
      - idToken
      - expires

Postman details are here:

{
			"name": "Login Only (Not needed - Example only)",
			"event": [
				{
					"listen": "test",
					"script": {
						"type": "text/javascript",
						"exec": [
							"const responseJson = pm.response.json();\r",
							"\r",
							"pm.collectionVariables.set('access_token', responseJson[\"idToken\"]);\r",
							""
						]
					}
				}
			],
			"request": {
				"method": "POST",
				"header": [
					{
						"key": "Host",
						"value": "identity.gridsvc.net"
					},
					{
						"key": "Content-Type",
						"value": "application/json"
					},
					{
						"key": "Accept",
						"value": "application/json"
					},
					{
						"key": "Accept-Language",
						"value": "en-gb"
					},
					{
						"key": "Origin",
						"value": "https://cev.kaluza.com",
						"type": "text"
					},
					{
						"key": "Referer",
						"value": "https://cev.kaluza.com/",
						"type": "text"
					}
				],
				"body": {
					"mode": "raw",
					"raw": "{\r\n    \"username\": \"{{emailId}}\",\"password\": \"{{passwordId}}\",\"ignoreChallenges\": true\r\n    }"
				},
				"url": "https://identity.gridsvc.net/login"
			},
			"response": []
		}

Content-Type versus content_type perhaps?

And does Postman send a User-Agent string? If you can log in with a browser, perhaps pull that user agent string into your headers too.

Thanks for this, however, tried both suggestions and still no luck…

Log file shows:

File “/usr/src/homeassistant/homeassistant/core.py”, line 1255, in init

raise InvalidStateError(

homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity ID: sensor.kaluza_login. State max length is 255 characters.

Manage to crack it though - changed the value_template to ‘1’ as dummy value to overcome the max length error and used the Json attributes to get my accessToken

1 Like

That’s a big access token…

1 Like

Yep, big access token that changes every 30 seconds or so!