Use {{ entity_id }} into a - entity: line inside a card

Hi All,

Is this even possible. I have a input_select.
I want a lovelance card that show me the info based in what the input_select is selected.

tried many things, but nothing was working.

input_select:

crypto:
  name: "Crypto"
  options:
    - "ADA"
    - "BNB"
    - "ETH"
    - "XLM"
    - "XLR"
    - "NLG"

card:

type: 'custom:apexcharts-card'
header:
  title: ETH / EUR - Last 24 hours
series:
  - entity: '{{ input_select.crypto }}'
    name: Sell
    show:
      extremas: true
    float_precision: 2
    unit: EUR
    group_by:
      func: avg
      duration: 5min
1 Like

I would suggest trying a conditional entities card that shows sensors based on the state of the input

I have a JSON Api from my crypto broker:

https://api.bitladon.com/markets/

This a part of it. total 225 inside results.

{
	"apistatus": 1,
	"error": false,
	"msg": "API online",
	"all": {
		"volume24h": "25610625.51"
	},
	"result": [
		{
			"name": "0x",
			"ticker": "ZRX",
			"buy": "1.30718",
			"sell": "1.25090",
			"last": "1.30629",
			"change24h": "-3.68",
			"change72h": "-2.04",
			"change168h": "-16.99",
			"volume24h": "14084.9312",
			"logo": "https://www.bitladon.com/img/currency/ZRX.png",
			"eurovolume24h": "18411.54",
			"graph": "https://www.bitladon.com/img/graphs/ZRXblue.png?v=1613896092"
		},
		{
			"name": "1inch",
			"ticker": "1INCH",
			"buy": "4.31477",
			"sell": "4.12266",
			"last": "4.32569",
			"change24h": "4.33",
			"change72h": "1.26",
			"change168h": "-9.04",
			"volume24h": "14445.6432",
			"logo": "https://www.bitladon.com/img/currency/1INCH.png",
			"eurovolume24h": "62329.63",
			"graph": "https://www.bitladon.com/img/graphs/1INCHblue.png?v=1613896092"
		},
		{
			"name": "Zilliqa",
			"ticker": "ZIL",
			"buy": "0.11346",
			"sell": "0.10835",
			"last": "0.11341",
			"change24h": "-9.75",
			"change72h": "-2.17",
			"change168h": "-7.51",
			"volume24h": "740283.0069",
			"logo": "https://www.bitladon.com/img/currency/ZIL.png",
			"eurovolume24h": "83992.51",
			"graph": "https://www.bitladon.com/img/graphs/ZILblue.png?v=1613896092"
		}
	]
}

Its a no go to create for every crypto a REST Sensor.
So im looking for a option that makes it easier.

Something with input_select with all the crypto in a list.
When I select a crypto then it will take the info from 1 REST Sensor where this whole JSON is stored:
The attribute have this information now:

This is a part of it.

result: 
- name: 0x
  ticker: ZRX
  buy: '1.34213'
  sell: '1.28434'
  last: '1.30629'
  change24h: '-4.94'
  change72h: '0.47'
  change168h: '-15.03'
  volume24h: '13865.5294'
  logo: 'https://www.bitladon.com/img/currency/ZRX.png'
  eurovolume24h: '18609.34'
  graph: 'https://www.bitladon.com/img/graphs/ZRXblue.png?v=1613899946'
- name: 1inch
  ticker: 1INCH
  buy: '4.41059'
  sell: '4.21810'
  last: '4.38181'
  change24h: '-0.74'
  change72h: '3.43'
  change168h: '-7.13'
  volume24h: '14617.7241'
  logo: 'https://www.bitladon.com/img/currency/1INCH.png'
  eurovolume24h: '64472.79'
  graph: 'https://www.bitladon.com/img/graphs/1INCHblue.png?v=1613899946'
- name: Aave
  ticker: AAVE
  buy: '373.332'
  sell: '357.131'
  last: '371.482'
  change24h: '-9.80'
  change72h: '-5.67'
  change168h: '-10.18'
  volume24h: '113.3879'
  logo: 'https://www.bitladon.com/img/currency/AAVE.png'
  eurovolume24h: '42331.33'
  graph: 'https://www.bitladon.com/img/graphs/AAVEblue.png?v=1613899946'

So a automation can do this maybe and put the value’s into a sensor that can be changes by automation.
Then I can use this sensor into the card as entities with the values.

Instead of creating multiple rest sensors you could also use template sensors or attributes to the one test sensor.

You something like this:

binary_sensor:
  - platform: template
    sensors:
      my_device:
        value_template: "OK"
        attribute_templates:
          sellprice: >-
            {% if is_state('input_select.crypto','ADA') %}
              A TEMPLATE TO EXTACT FROM THE TEST SENSOR THAT HAVE EVERYTHING
            {% elif  is_state('input_select.crypto','BNB') %}
              A TEMPLATE TO EXTACT FROM THE TEST SENSOR THAT HAVE EVERYTHING
            {% elif  is_state('input_select.crypto','NLG') %}
              A TEMPLATE TO EXTACT FROM THE TEST SENSOR THAT HAVE EVERYTHING
            {% endif %}
          buyprice: >-
            {% if is_state('input_select.crypto','ADA') %}
              A TEMPLATE TO EXTACT FROM THE TEST SENSOR THAT HAVE EVERYTHING
            {% elif  is_state('input_select.crypto','BNB') %}
              A TEMPLATE TO EXTACT FROM THE TEST SENSOR THAT HAVE EVERYTHING
            {% elif  is_state('input_select.crypto','NLG') %}
              A TEMPLATE TO EXTACT FROM THE TEST SENSOR THAT HAVE EVERYTHING
            {% endif %}

Now difficult thing is grab the info from:

result: 
- name: 0x
  ticker: ZRX
  buy: '1.34213'
  sell: '1.28434'
  last: '1.30629'
  change24h: '-4.94'
  change72h: '0.47'
  change168h: '-15.03'
  volume24h: '13865.5294'
  logo: 'https://www.bitladon.com/img/currency/ZRX.png'
  eurovolume24h: '18609.34'
  graph: 'https://www.bitladon.com/img/graphs/ZRXblue.png?v=1613899946'
- name: 1inch
  ticker: 1INCH
  buy: '4.41059'
  sell: '4.21810'
  last: '4.38181'
  change24h: '-0.74'
  change72h: '3.43'
  change168h: '-7.13'
  volume24h: '14617.7241'
  logo: 'https://www.bitladon.com/img/currency/1INCH.png'
  eurovolume24h: '64472.79'
  graph: 'https://www.bitladon.com/img/graphs/1INCHblue.png?v=1613899946'
- name: Aave
  ticker: AAVE
  buy: '373.332'
  sell: '357.131'
  last: '371.482'
  change24h: '-9.80'
  change72h: '-5.67'
  change168h: '-10.18'
  volume24h: '113.3879'
  logo: 'https://www.bitladon.com/img/currency/AAVE.png'
  eurovolume24h: '42331.33'
  graph: 'https://www.bitladon.com/img/graphs/AAVEblue.png?v=1613899946'

Because I don’t know how to select for example 1INCH and get the info thats belong to it.

I’m no templating hero myself but you could try using the developer tools to extract date from the full json.

im also not a templating hero too… I will figure it out… Holiday until 12-03 haha

If you search the community for “json template” you’ll find some useful examples.

found already some information. Thanks

{% set json_data = 
{
	"apistatus": 1,
	"error": false,
	"msg": "API online",
	"all": {
		"volume24h": "25610625.51"
	},
	"result": [
		{
			"name": "0x",
			"ticker": "ZRX",
			"buy": "1.51530",
			"sell": "1.25090",
			"last": "1.30629",
			"change24h": "-3.68",
			"change72h": "-2.04",
			"change168h": "-16.99",
			"volume24h": "14084.9312",
			"logo": "https://www.bitladon.com/img/currency/ZRX.png",
			"eurovolume24h": "18411.54",
			"graph": "https://www.bitladon.com/img/graphs/ZRXblue.png?v=1613896092"
		}
	]
}
%}
{% set json_data_2 = state_attr("sensor.bitladon_all","result") %}
{{ json_data['result'][0].ticker }}
{{ json_data['result'][0].buy }}
{{ json_data_2[0].buy }}

This is working :smiley: so im close!!

input_select:

crypto:
  name: "Crypto"
  options:
    - 0
    - 1
    - 2

sensor

- platform: template
  sensors:
    crytpo_sell:
      friendly_name: "Crypto Sell Price"
      value_template: >-
        {% if is_state('input_select.crypto', '0') %} 
          "{{ (state_attr('sensor.bitladon_all', 'result')[0]['sell']) }}"
        {% elif is_state('input_select.crypto', '1') %}
          "{{ (state_attr('sensor.bitladon_all', 'result')[1]['sell']) }}"
        {% elif is_state('input_select.crypto', '2') %}
          "{{ (state_attr('sensor.bitladon_all', 'result')[2]['sell']) }}"
        {% endif -%}

    crytpo_buy:
      friendly_name: "Crypto Buy Price"
      value_template: >-
        {% if is_state('input_select.crypto', '0') %} 
          "{{ (state_attr('sensor.bitladon_all', 'result')[0]['buy']) }}"
        {% elif is_state('input_select.crypto', '1') %}
          "{{ (state_attr('sensor.bitladon_all', 'result')[1]['buy']) }}"
        {% elif is_state('input_select.crypto', '2') %}
          "{{ (state_attr('sensor.bitladon_all', 'result')[2]['buy']) }}"
        {% endif -%}

This is also working.

But when I want add them all then the if elif will be LONG… 220 different crypto’s

Instead of using al lot of elifs you might consider usings lists and/or loops. You can find some nice examples/howtos in the community.

{% set item =  state_attr('x.y', 'result') | selectattr('name', 'eq', '1inch') | first %}
{{ item.buy }}
{{ item.sell }}

Thanks. But now confused… Where do I use this? In the template sensor?

that would be in your template sensor. I’m just showing you how to select an item without requiring you to use [0], or [1]. It makes it not care about which index it is and lets you grab the information regardless of the items index.

Ah ok. thanks! So I can change it that the part
selectattr('name', 'eq', '1inch')
can be a input_selector or input_text
selectattr('name', 'eq', states('input_text.crypto'))

yep, exactly like that. Then you won’t need an automation.

However, you could use an automation to change an input_select list based on the current information inside the rest sensor.

First let me understand this… I tried this:

- platform: template
  sensors:
    crypto_selected:
      friendly_name: "Crypto Buy Price"
      value_template: >-
        {% set item =  state_attr('sensor.bitladon_all', 'result') | selectattr('name', 'eq', states('input_text.crypto_name')) | first %}
         {{ item.name }}
      attribute_templates:
        bye: >-
          {{ item.buy }}
        sell: >- 
          {{ item.sell }}

The value_tamplate show the right information.
But the buy and sell I see a error in log:

2021-02-22 15:00:08 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'item' is undefined') while processing template 'Template("{{ item.buy }}")' for attribute 'bye' in entity 'sensor.crypto_selected'
2021-02-22 15:00:08 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'item' is undefined') while processing template 'Template("{{ item.sell }}")' for attribute 'sell' in entity 'sensor.crypto_selected'

you can’t do that, you have to put the selector in every attribute.

I tried that as well but didn’t work. Sorry for my stupid questions. But sometimes I lost the simple things to understand haha…

- platform: template
  sensors:
    crypto_selected:
      friendly_name: "Crypto Buy Price"
      value_template: >-
        {% set item =  state_attr('sensor.bitladon_all', 'result') | selectattr('name', 'eq', states('input_text.crypto_name')) | first %}
          {{ item.name }}
      attribute_templates:
        bye: >-
          {% set item =  state_attr('sensor.bitladon_all', 'result') | selectattr('name', 'eq', states('input_text.crypto_name')) | first %}
            {{ item.buy }}
        sell: >-
          {% set item =  state_attr('sensor.bitladon_all', 'result') | selectattr('name', 'eq', states('input_text.crypto_name')) | first %}
            {{ item.sell }}
2021-02-22 15:06:48 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: No first item, sequence was empty.') while processing template 'Template("{% set item =  state_attr('sensor.bitladon_all', 'result') | selectattr('name', 'eq', states('input_text.crypto_name')) | first %}
 {{ item.name }}")' for attribute '_state' in entity 'sensor.crypto_selected'
2021-02-22 15:06:48 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: No first item, sequence was empty.') while processing template 'Template("{% set item =  state_attr('sensor.bitladon_all', 'result') | selectattr('name', 'eq', states('input_text.crypto_name')) | first %}
  {{ item.buy }}")' for attribute 'bye' in entity 'sensor.crypto_selected'
2021-02-22 15:06:48 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: No first item, sequence was empty.') while processing template 'Template("{% set item =  state_attr('sensor.bitladon_all', 'result') | selectattr('name', 'eq', states('input_text.crypto_name')) | first %}
  {{ item.sell }}")' for attribute 'sell' in entity 'sensor.crypto_selected'

Or so I have different set names? now all are ‘item’