Getting value from cookie.txt

I am connecting to my reef controller via API. The controller requires cookie authentication which I have working fine. The problem is that the cookie must be refreshed every couple months. I have now setup 7 terrariums using the same controller. It will now be a headache to manually refresh 8 cookies once they expire.

I will use one of my switches for this example. The switch references my secrets file which contains the cookie for each controller. The format of the cookie must be auth=longlistofcharacters

# Reef Outlets
  - platform: rest
    name: Reef Outlet 1
    resource: http://myip:port/api/equipment/4
    username: !secret reefpi_username
    password: !secret reefpi_password
    body_on: '{"id":"4","name":"LED Power","outlet":"4","on":true'
    body_off: '{"id":"4","name":"LED Power","outlet":"4","on":false'
    is_on_template: '{{ value_json.on }}'
    headers:
        Connection: keep-alive
        content-type: application/json
        Referer: http://myip:port/
        Cookie: !secret reefpi_cookie

A user from the controllers forum has a perl script to auto update the cookie. However, I have not been able to make it work in my setup. I have been able to setup an automation that saves the cookie in a cookie.txt file within home assistant. But the file contains more than what is actually needed. Is there a way to reference the cookie.txt file and use only the portion needed. Below is the results shown in cookie.txt.

mydomain.duckdns.org FALSE / FALSE 1625110757 auth MTYyMjU0MzAxN3xEdi1CQkFFQ180SUFBUkFCRUFBQUpmLUNBQUVHYzNSeWFXNW5EQVlBQkhWelpYSUdjM1J5YVc1bkRBa0FCM0psWldZdGNHaz18uYqXEO0IgwS6W6O0tW8b0m2Xkgn-Fghur=

Here’s how to get it into a sensor — you could do something similar in your automation to only save the cookie part in the first place:

sensor:
  - platform: command_line
    name: "The cookie"
    command: "cat /path/to/cookie.txt"
    value_template: "{{ value.split(' ')[6] }}"

The value_template splits it at each space, and returns the seventh part ([6] is zero-based). I don’t believe headers can use a template, though. You might have to set it up differently.

Your body_on and body_off appear to be missing a closing }.

Thanks for noticing the missing closing. It was working so I looked over it.

I left out the first part of the file thinking that it would not matter and the formatting was not correct in prior post. Below is the full cookie.txt. The spaces for the cookie line appear to be tabs, not spaces. There is an arrow pointing to the right after each word that does not transfer over to this post. If I change to looking for ‘auth’ then I can get the actual cookie, however, there is a space before it. If I try ‘auth+space’ or ‘auth+tab’ it comes back blank.

  - platform: command_line
    name: "Reef Cookie"
    command: "cat /config/cookie.txt"
    value_template: "auth={{ value.split('auth')[1] }}"

`# Netscape HTTP Cookie File

https://curl.haxx.se/docs/http-cookies.html

This file was generated by libcurl! Edit at your own risk.

mydomain.duckdns.org FALSE / FALSE 1625157751 auth MTYyMjU2NTU1OHxEdi1CQkFFQ180SUFBUkFCRUFBQUpmLUNBQUVHYzNSeWFXNW5EQVlBQkhWelpYSUdjM1J5YVc1bkRBa0FCM0psWldZdGNHaz18H15DAesKlqRdcF78pptQ88fS6CDG-NODTZ_7=

Edit: Formatting is still not correct. All spaces between my domain and the end are larger and appear to be tabs.
`

If you need the second character onwards of a string, add [1:] after it:

value_template: "auth={{ value.split('auth')[1][1:] }}"

Thanks. That worked. It now shows correctly. If I used the template editor, I am able to get the correct information. However, the switch is not showing up. To simplify things, I am now trying to get the cookie directly in the switch rather than storing it in my secrets.yaml and then trying to get it in the switch. Once that is working, I will move it over to my secrets. Is the cookie line below correct?

  - platform: rest
    name: Reef Light
    resource: http://myip:port/api/equipment/1
    username: !secret reef_username
    password: !secret reef_password
    body_on: '{"id":"1","name":"Light","outlet":"1","on":true}'
    body_off: '{"id":"1","name":"Light","outlet":"1","on":false}'
    is_on_template: '{{ value_json.on }}'
    headers:
        Connection: keep-alive
        content-type: application/json
        Referer: http://myip:port
        Cookie: states('sensor.reef_cookie')

I have also tried

        Cookie: "{{ states('sensor.reef_cookie') }}"

As I said in my first post, I don’t believe you can use templates in the header declaration.

This might be a job for AppDaemon.

Oh. Thanks. I didn’t understand that part. Now I get it. I’m not very good with this but learning. I will check out AppDaemon.