Rest API and use of a response for a card

Hi all,

I am playing around with the rest api and so far I could use it to get the status of an amplifier. I want to use now the data I received and display this in a card. I am pretty new and could not find the answer yet.

What I did so far:

  1. Create the Rest API request in the yaml
rest_command:
  sonible_get_mute:
    url: "http://10.0.4.51/api/get/system/mute"
  sonible_get_fanmode:
    url: "http://10.0.4.51/api/get/system/fanmode"
  1. Create an automation with a Response Variable
alias: sonible_status

action:
  - service: rest_command.sonible_get_mute
    data: {}
    response_variable: son.mute
  - service: rest_command.sonible_get_fanmode
    data: {}
    response_variable: son.fanmode
mode: single

So far so good. I also see the change of the response variable in the traces.

son.mute:
  content: '0'
  status: 200

and

son.fanmode:
  content: '4'
  status: 200

Now I want to display this on a card.
“0” means “mute”; (variable son.mute)
“4” means “max Fan” (variable son.fanmode)

Any idea how I could display this in a card (As text)?
I already tried with template but did not find an answer.

Thanks for your help and support

S

Use a rest sensor instead of a rest command. Then you can convert the fan mode in the value_template. Assuming the response is json:

sensor:
  - platform: rest
    name: Fan Mode
    icon: mdi:fan
    resource: http://10.0.4.51/api/get/system/fanmode
    method: get
    value_template: >
      {% if value_json.content = 0 %}
        mute
      {% elif value_json.content = 4 %}
        max fan
      {% else %}
        unknown
      {% endif %}

Then you can display this in your dashboard like any other entity.

Thanks Tom

I tried but there is some error in the Syntax and I am not sure where this sits:

Invalid config for 'sensor' from integration 'rest' at configuration.yaml, line 704: invalid template (TemplateSyntaxError: expected token 'end of statement block', got '=') for dictionary value 'value_template', got '{% if value_json.content = 0 %}\n min fan\n{% elif value_json.content = 4 %}\n max fan\n{% else %}\n unknown\n{% endif %}\n', please check the docs at https://www.home-assistant.io/integrations/rest

How do I then add the sensor to the dashboard? Is there a certain card for it?

Thanks S

Sorry, template should be:

    value_template: >
      {% if value_json.content == 0 %}
        mute
      {% elif value_json.content == 4 %}
        max fan
      {% else %}
        unknown
      {% endif %}

= sets one value to another
== is a test for equality.

Thanks Tom - Appreciate your support :slight_smile:

Understood why this result in an error…corected it and now it loads.

How can you add this now to a card? I tried with the sensor card but did not find a new sensor with the values.

I expected something like: sensor.rest… but did not find.
Sorry for aksing again I am still learning.

Thanks S

This makes your sensor entity id: sensor.fan_mode. See if you can find it in Developer Tools → States or Settings → Devices & Services → Entities List (up top of the page).

If it is there you should be able to add it to an Entities Card.

Thanks Tom.

Yes that was the correct name. I find it and also can add it to the Dahsboard. Great thanks. However still does not recognize the values (It just feedbacks the unknown state)

In your post earlier you mentiones that the answer must be json. I looked up in the manual and json is not mentioned there. Is there a possibility to see how the answer is coded?

Thanks again S

Post the exact state of this sensor correctly formatted for the forum when in both modes:

sensor:
  - platform: rest
    name: Fan Mode
    icon: mdi:fan
    resource: http://10.0.4.51/api/get/system/fanmode
    method: get
    value_template: "{{ value }}"