Sensor restful definition using secrets.yaml?

hi,

im trying to write several rest sensors using my Hue Ip and Api key. Obviously i don’t want these exposed in the configuration, but read from the secret.yaml.

Before, i did this by moving the full resource line to the secret file, and that was ok because only 2 lines needed.

For this project, i need several more and i would think it more elegant to have the ip and api called from secrets.

Reading the description of the restful platform this should be possible (i think…) but im not sure how to proceed.

if got this for a working definition right now, with api and ip in the sensor:

- platform: rest
  resource: http://192.168.1.xxx/api/[hue api key}/sensors/22
  value_template: '{{ value_json.state.status }}'
  name: 'Auditorium virtual presence'

and was looking for something along the lines of:

- platform: rest
  resource: http://(!secret hue_ip)/api/(!secret hue_api_key)/sensors/22
  value_template: '{{ value_json.state.status }}'
  name: 'test Auditorium virtual presence'

which obviously wont go, since im writing this here :wink: http://"!secret hue_ip"/api/"!secret hue_api_key"/sensors/22 wont do it either…

please have a look with me to see if this could be done, (with having to write a full python script, since i already have that, and its working alright. Hoped this to be a more elegant and less processor intensive solution )

Cheers,
Marius

Put the whole line in secrets…

- platform: rest
  resource: !secret resource 
  value_template: '{{ value_json.state.status }}'
  name: 'Auditorium virtual presence'

Secrets…

resource:  http://192.168.1.xxx/api/[hue api key}/sensors/22

HI @anon43302295!
Yes, thats what i meant in my post doing earlier, was a good advice. And working fine. But since I now have a lot more, my secrets file would become filled with a lot of full lines, instead of only key and ip address.
Would have hoped for some way to only point to those by way of declaring the rest sensor differently somehow.
Thanks anyways…
cheers,
Marius

It just doesn’t work that way unfortunately, the only other way to prevent it from being ‘public’ is to add the affected files to your gitignore, but that’s fraught with danger tbh.

1 Like

thanks for assuring me in that case, no other way.
btw, since you’re here, might i ask you to have a look another question please?
Marius

If it makes you feel any better, my secrets file has a shed load of these sorts of things in too :grinning:

1 Like

resource: http://192.168.1.xxx/api/[hue api key}/sensors/ "?"

Can I do something like a dir *. * in the network structure?

I have absolutely no idea what you’re asking.

Sorry about that. :smile:

If I have many links with a similar structure, for example:

    http://192.168.1.xxx/api/[hue api key}/sensors/22
    http://192.168.1.xxx/api/[hue api key}/sensors/23
    http://192.168.1.xxx/api/[hue api key}/sensors/24
    http://192.168.1.xxx/api/[hue api key}/sensors/26
    http://192.168.1.xxx/api/[hue api key}/sensors/27
    http://192.168.1.xxx/api/[hue api key}/sensors/28

Is there a universal character for multiple links at the end of the link so as not to write hundreds of times such links in secrets.yaml?

For example:

`resource:  http://192.168.1.xxx/api/[hue api key}/sensors/XX`

where XX applies to all changing directory names at the end of the links.
or
how to simply write in secrets.yaml?

http://192.168.1.xxx/api/[hue api key}/sensors/22
http://192.168.1.xxx/api/[hue api key}/sensors/23
http://192.168.1.xxx/api/[hue api key}/sensors/24
http://192.168.1.xxx/api/[hue api key}/sensors/26
http://192.168.1.xxx/api/[hue api key}/sensors/27
http://192.168.1.xxx/api/[hue api key}/sensors/28

and much more similar links

Thanks

Ah, I get ya :slight_smile:

Unfortunately not. The only way to do it with secrets is to reference the entire line.

1 Like

It seemed so simple …
Thank you and have a nice day. :smiley:

not exactly the same, but still a discovery for me was that we can template these resources in a script to act upon individual Hue entities:

  set_hue_sensitivity_direct:
    alias: Set Hue sensitivity direct
    sequence:
      service: rest_command.set_hue_command
      data_template:
        command: config
        type: sensors
        data: >
          {"sensitivity": {{sensitivity}} }
        id: >
          {{id}}

rest command being:

rest_command:
  set_hue_command: #not named sensitivy because the command can be used to set other entities/attributes also
    url: !secret url_set_hue_command
    method: put
    payload: '{{ data }}'

the secret for the hue command holds your secrets, and a template…

url_set_hue_command: http://[redacted Ip-address]/api/[redacted Api-key]/{{ type }}/{{ id }}/{{ command }}

using the same secret and rest command for this:

  set_hue_threshold:
    alias: Set Hue threshold
    sequence:
      service: rest_command.set_hue_command
      data_template:
        command: config
        type: sensors
        data: >
          {"tholddark": {{states('input_number.slide_hue_threshold')|int}} }
        id: >
          {% set mapper =
            { 'Laundry':'25',
              'Dining table':'53',
              'Auditorium':'45',
              'Frontdoor':'61',
              'Dorm':'57',
              'Corridor':'6',
              'Corridor terrace':'34',
              'Master bedroom':'49',
              'Corridor Office':'29',
              'Control room':'9',
              'Attic':'13' } %}
          {% set state = states('input_select.select_hue_motion_sensor') %}
          {% set id = mapper[state] if state in mapper %}
          {{id}}
4 Likes