Input_number.set_value can not get value with value template

The main goal is to use mqtt data set ( already as entity with json format) with input_select dropdown selection to fill up the other entities with respective data.

The Data set (the sensor.plant entity is as follow)

no: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34
plantname: Tere, Karnabahar, Marul, Maydanoz, Bezelye, Bezelye (tatlı), Kekik, Fesleğen, Lavanta, Limon Otu, Adaçayı, Çilek, Çin Lahanası, Karpuz, Salatalık, Fasulye, Bakla, Dolmalık Biber (renkli), Kereviz, Frenk Soğanı, Bal Kabağı, Ispanak, Su Kabağı, İsviçre Pazı, Kabak, Kavun, Nane, Dolmalık Biber, Biber Acı, Domates, Bürüksel Lahanası, Lahana, Patlıcan, Brokoli
plantname_en: Watercress, Cauliflower, Lettuce, Parsley, Pea, Peas (Sugar), Thyme, Basil, Lavender, Lemon Balm, Sage, Strawberries, Pak-choi, Watermelon, Cucumber, Bean (Common), Bean (Broad), Bell Peppers, Celery, Chives, Pumpkin, Spinach, Squash, Swiss Chard, Zucchini, Melon, Mint, Peppers (Bell), Peppers (Hot), Tomato, Brussell Sprouts, Cabbage, Eggplant, Broccoli
plant_ec_low: 0.4, 0.5, 0.8, 0.8, 0.8, 0.8, 0.8, 1, 1, 1, 1, 1, 1.5, 1.5, 1.7, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 2, 2, 2, 2, 2, 2.5, 2.5, 2.5, 2.8
plant_ec_high: 1.8, 2, 1.2, 1.8, 1.8, 1.9, 1.6, 1.6, 1.4, 1.6, 1.6, 1.4, 2, 2.4, 2.5, 2.4, 2.4, 2.8, 2.4, 2.4, 2.4, 2.3, 2.4, 2.3, 2.4, 2.5, 2.4, 2.5, 3.5, 5, 3, 3, 3.5, 3.5
plant_ph_low: 6.5, 6, 5.5, 6, 6, 6, 5.5, 5.5, 6.4, 5.5, 5.5, 6, 5.5, 5.08, 5.8, 6, 6, 6, 6.3, 6, 5.5, 5, 5.5, 5.5, 6, 5.5, 5.5, 6, 5.5, 6.5, 6.5, 6.5, 5.5, 6
plant_ph_high: 6.8, 7, 6.5, 7, 6.8, 6.5, 6.5, 6.5, 6.8, 6.5, 6.6, 6.5, 6, 5.08, 6, 6, 6.5, 6.7, 6.7, 6.5, 6.5, 6.5, 6.5, 7, 6, 6, 6, 6.5, 7.5, 6.8, 7.5, 7, 6.5, 6.5
plant_tds_low: 280, 1050, 560, 560, 580, 580, 560, 700, 700, 700, 700, 500, 1050, 1050, 1190, 1400, 1400, 1400, 1260, 1260, 1260, 1260, 1260, 1260, 1260, 1400, 1400, 1400, 1400, 1400, 1750, 1750, 1750, 1960
plant_tds_high: 1260, 1400, 840, 1260, 1260, 1261, 1120, 1120, 980, 1120, 1120, 700, 1400, 1680, 1750, 2800, 1800, 2000, 1680, 1680, 1680, 1610, 1680, 1610, 1680, 1750, 1680, 1750, 2450, 3500, 2100, 2100, 2450, 2450
plant_temp_low: , , 20, , , , , 18, , , , 20, , , 18, , , , , , , 18, , , , , 18, 20, 20, 18, , , , 
plant_temp_high: , , 23, , , , , 25, , , , 23, , , 22, , , , , , , 24, , , , , 24, 23, 23, 25, , , , 
plant_harvest_target: , , 30, , , , , 30, , , , 60, , , 70, , , , , , , 45, , , , , 30, 90, 90, 70, , , , 
plant_daylight_target: , , 16, , , , , 16, , , , 16, , , 14, , , , , , , 14, , , , , 10, 16, 16, 16, , , , 
friendly_name: Plant

The input_select.plantname works perfect and getting option value from above data set:

The mission I need to achieve is to fill up other input data as soon as input_select.plantname has changed.

Accordingly I have created an automationas follow:

- id: '1615792052600'

  alias: Set input_number.plant_ec_low

  description: ''

  trigger:

  - platform: state

    entity_id: input_select.plantname

    

  condition: []

  action:

  - service: input_number.set_value

    entity_id: input_number.plant_ec_low

    data_template:

      value: "(states.sensor.plant.attributes.plant_ec_low[state_attr(trigger.entity_id,'options').index(states(trigger.entity_id))]) | float"

  - service: input_number.set_value

    entity_id: input_number.plant_ec_high

    data_template:

      value: "(states.sensor.plant.attributes.plant_ec_high[state_attr(trigger.entity_id,'options').index(states(trigger.entity_id))]) | float"

  mode: single

The values are works good in Developper Tools > Template as normal in following structure:

{{ states.sensor.plant.attributes.plant_ec_low[state_attr('input_select.plantname','options').index(states('input_select.plantname'))]}}
{{ states.sensor.plant.attributes.plant_ec_high[state_attr('input_select.plantname','options').index(states('input_select.plantname'))]}}

But, after all try I couldn’t get successful to get the value from automation. Automation does not give any error as above, the only difference with template complier request me to convert dictionary values to the float. That’s why I’ve added | float to the end. However if I add the | float in developer tools > template; values printing as 0,00

I will be grateful for any advise to solve template in automation

You are missing the two curly brackets around your templates.

And data_template can be replaced with just data since some versions ago.

Try this (add the “{{ }}” and replace “value” by “value_template”, not sure about the last one as “data_template” is now “data”):

value_template: “{{ (states.sensor.plant.attributes.plant_ec_high[state_attr(trigger.entity_id,‘options’).index(states(trigger.entity_id))]) | float }}”

This will almost certainly not work. The service expects the key value and not value_template.

I’ve got 2 errors after your advise:

Logger: homeassistant.components.automation.set_input_number_plant_ec_low
Source: components/automation/__init__.py:416
Integration: Automation ([documentation](https://www.home-assistant.io/integrations/automation), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+automation%22))
First occurred: 6:59:02 PM (1 occurrences)
Last logged: 6:59:02 PM

Error while executing automation automation.set_input_number_plant_ec_low: extra keys not allowed @ data['value_template']
Logger: homeassistant.components.automation.set_input_number_plant_ec_low
Source: helpers/script.py:1156
Integration: Automation ([documentation](https://www.home-assistant.io/integrations/automation), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+automation%22))
First occurred: 6:59:02 PM (1 occurrences)
Last logged: 6:59:02 PM

Set input_number.plant_ec_low: Error executing script. Invalid data for call_service at pos 1: extra keys not allowed @ data['value_template']

my latest script was like this:

- id: '1615741710537'

  alias: Plant Option

  description: Set Plant Name

  trigger:

  - platform: mqtt

    topic: /plant

  condition: []

  action:

  - service: input_select.set_options

    data:

      options: '{{ states.sensor.plant.attributes.plantname | list }}

        '

    target:

      entity_id: input_select.plantname

  mode: single

- id: '1615792052600'

  alias: Set input_number.plant_ec_low

  description: ''

  trigger:

  - platform: state

    entity_id: input_select.plantname

    

  condition: []

  action:

  - service: input_number.set_value

    entity_id: input_number.plant_ec_low

    data:

      value_template: "{{(states.sensor.plant.attributes.plant_ec_low[state_attr(trigger.entity_id,'options').index(states(trigger.entity_id))]) | float}}"

  - service: input_number.set_value

    entity_id: input_number.plant_ec_high

    data:

      value_template: "{{(states.sensor.plant.attributes.plant_ec_high[state_attr(trigger.entity_id,'options').index(states(trigger.entity_id))]) | float}}"

  mode: single

That’s exactly what I said in my previous post that value_template won’t work and you need to keep it with value.

I never said to change value to value_template, only adding the {{.

2 Likes

Yes I added {{}} and it is not giving any error. However the result is not working.

Here is the final shape:

action:

  - service: input_number.set_value

    entity_id: input_number.plant_ec_low

    data_template:

      value: "{{(states.sensor.plant.attributes.plant_ec_low[state_attr(trigger.entity_id,'options').index(states(trigger.entity_id))]) | float}}"

  - service: input_number.set_value

    entity_id: input_number.plant_ec_high

    data_template:

      value: "{{(states.sensor.plant.attributes.plant_ec_high[state_attr(trigger.entity_id,'options').index(states(trigger.entity_id))]) | float}}"

Please try adding another service

- service: persistent_notification.create
  data:
    message: "{{(states.sensor.plant.attributes.plant_ec_low[state_attr(trigger.entity_id,'options').index(states(trigger.entity_id))]) | float}}"

And see if you get the expected result in the persistent notification.

No luck! The automation still does not work and I’ve got following error:

Logger: homeassistant.config
Source: config.py:424
First occurred: 7:25:15 PM (1 occurrences)
Last logged: 7:25:15 PM

Invalid config for [automation]: expected dict for dictionary value @ data['action'][2]['data']. Got None extra keys not allowed @ data['action'][2]['message']. Got None. (See /config/configuration.yaml, line 13).

Just to clarify, you have it like this:

action:
  - service: input_number.set_value
    entity_id: input_number.plant_ec_low
    data_template:
      value: "{{(states.sensor.plant.attributes.plant_ec_low[state_attr(trigger.entity_id,'options').index(states(trigger.entity_id))]) | float}}"
  - service: input_number.set_value
    entity_id: input_number.plant_ec_high
    data_template:
      value: "{{(states.sensor.plant.attributes.plant_ec_high[state_attr(trigger.entity_id,'options').index(states(trigger.entity_id))]) | float}}"
  - service: persistent_notification.create
    data:
      message: "{{(states.sensor.plant.attributes.plant_ec_low[state_attr(trigger.entity_id,'options').index(states(trigger.entity_id))]) | float}}"

Yes exactly, I have 3 services and I’ve got error which !'ve sent above

Why the template works in developer tools > template correctly and not in the automation?

The one works, as follow:

{{ states.sensor.plant.attributes.plant_ec_low[state_attr('input_select.plantname','options').index(states('input_select.plantname'))]}}
{{ states.sensor.plant.attributes.plant_ec_high[state_attr('input_select.plantname','options').index(states('input_select.plantname'))]}}

Are you triggering the automation manually or do you change the input select?

Each and every time trying with changing input select.
Manual triger doesnt make sense and not activate the action

Yeah, I know, that’s why I wanted to be sure that you didn’t try to do it manually.

Finally I’ve got it work as follow. Hope it may helpful for anyone
p.s.= float does not work if data set decimal values with comma, should be converted to “.”

  action:
  - service: input_number.set_value
    entity_id: input_number.plant_ec_low
    data_template:
      value: "{{ states.sensor.plant.attributes.plant_ec_low[state_attr('input_select.plantname','options').index(states('input_select.plantname'))] | float}}"
  - service: input_number.set_value
    entity_id: input_number.plant_ec_high
    data_template:
      value: "{{ states.sensor.plant.attributes.plant_ec_high[state_attr('input_select.plantname','options').index(states('input_select.plantname'))] | float}}"