Is it possible to autofill entities from a device?

I have this blueprint I created which I require the user to provide the entity for each sensor.

But the Apollo Air Esphome gives you an “Apollo Air” device that has all the entities underneath it.

My question is, can I design the blueprint in such a way, that the user only needs to specify the “Apollo Air” device, and the entities will be automatically grabbed from the device?

I can think of 2 ways I’d want to implement that:

  1. Set the default value of the entity input, for example, the “temperature sensor” default value will be grabbed from the provided device, with the option to change it if the user needs it.
  2. There will be no need to specify an entity for each sensor, and the blueprint will only use the specified “Apollo Air” device. (But then what would happen if the user renamed one of the entities to a custom name?)

Any help would be appreciated!

Hello Tomer Horowitz,

You should be able to use the filtering in the entity or device selector to get just what you want.

Did you figure this out? I’m trying to create a blueprint for MSR-1 where I turn on its rib light based on its reading for CO2 level and it seems weird to have to select entry of the light and then the co2 sensor in separate inputs vs just selecting the sensor (MSR-1) itself only once.

I don’t have the device in question, but it should be possible as long as you can insure that the sensor entities are determinable by either their entity ID or one of their attributes:

variables:
  device_id: !input apollo_device
  entities_list: "{{ device_entities(device_id) }}"
  pm25_sensor: "{{ entities_list | select('match', 'sensor') | select('search', 'pm25' ) | first }}"
  humidity_sensor: |
    {{ entities_list | select('match', 'sensor') | expand | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', 'humidity') | map(attribute='entity_id') | first }}

Thanks! With your example now I understand what the first answer was trying to convey. I still do not know much and trying to learn. I’ve spent over an hour trying to figure out how to use those new variables in the next step and reference the entity_id for the co2_sensor:

variables:
  device_id: !input apollo_device
  entities_list: "{{ device_entities(device_id) }}"
  co2_sensor: "{{ entities_list | select('match', 'sensor') | select('search', 'co2' ) | first }}"
  light: "{{ entities_list | select('match', 'light') | first }}"

triggers:
  - entity_id: 
    - "{{ co2_sensor }}"
    above: 1500
    id: co2_sensor_above
    trigger: numeric_state

This gives me an error that “String does not match the pattern…” I’ve tried diff ways to format “{{ co2_sensor }}” but nothing seems to work.

Numeric State triggers do not accept templates like that, you need to use a Template trigger:

triggers:
  - trigger: template
    value_template: "{{ states(co2_sensor) | float(0) > 1500 }}"
    id: co2_sensor_above

Thanks again! I’m clearly in over my head here. Not sure how to learn all these things without having to ask someone :grimacing:
I implemented your suggestion and then some more, but I made basically some guessing on how to reference the light variable and wanted to try to see if it works or I get some useful error that I can try to figure out but… did not get very far.
I got this error when trying to create the first automation:

Message malformed: Unknown entity '{{ light.entity_id }}'

My best guess on how to use light in places like this:

            sequence:
              - target:
                  entity_id: "{{ light.entity_id }}"
                data:
                  transition: 1
                  rgb_color:
                    - 255
                    - 134
                    - 71
                  brightness: 75
                action: light.turn_on

I did try to search through the community and docs on how to retrieve the entity_id from it but could not find anything that worked, but I did not find a direct example that uses a variable. I’ve tried diff variations on formatting this but nothing worked so far.
This is the full blueprint:

blueprint:
  name: CO2 Warning
  description: Adjsut RGB light based on CO2 levels
  domain: automation
  input:
    apollo_device:
      name: Apolo Sensor
      selector:
        device:
          integration: esphome

variables:
  device_id: !input apollo_device
  entities_list: "{{ device_entities(device_id) }}"
  co2_sensor: "{{ entities_list | select('match', 'sensor') | select('search', 'co2' ) | first }}"
  light: "{{ entities_list | select('match', 'light') | first }}"

triggers:
  - trigger: template
    value_template: "{{ states(co2_sensor) | float(0) > 1500 }}"
    id: co2_sensor_above

  - trigger: template
    value_template: "{{ states(co2_sensor) | float(0) > 1750 }}"
    id: co2_sensor_above_1750

  - trigger: template
    value_template: "{{ states(co2_sensor_above_2000) | float(0) > 2000}}"
    id: co2_sensor_above_2000

  - trigger: template
    value_template: "{{ states(co2_sensor_bellow) | float(0) < 1500 }}"
    id: co2_sensor_bellow

conditions: []
actions:
  - if:
      - condition: time
        after: "07:00:00"
        before: "23:00:00"
        weekday:
          - sun
          - sat
          - fri
          - thu
          - wed
          - tue
          - mon
    then:
      - choose:
          - conditions:
              - condition: trigger
                id:
                  - co2_sensor_above
            sequence:
              - target:
                  entity_id: "{{ light.entity_id }}"
                data:
                  transition: 1
                  rgb_color:
                    - 255
                    - 134
                    - 71
                  brightness: 75
                action: light.turn_on
          - conditions:
              - condition: trigger
                id:
                  - co2_sensor_bellow
            sequence:
              - type: turn_off
                device_id: !input apollo_device
                entity_id: "{{ light.entity_id }}"
                domain: light
          - conditions:
              - condition: trigger
                id:
                  - co2_sensor_above
                  - co2_sensor_above_2000
            sequence:
              - target:
                  entity_id: "{{ light.entity_id }}"
                data:
                  transition: 1
                  rgb_color:
                    - 181
                    - 26
                    - 0
                  brightness: 125
                action: light.turn_on
          - conditions:
              - condition: trigger
                id:
                  - co2_sensor_above_1750
            sequence:
              - target:
                  entity_id: "{{ light.entity_id }}"
                data:
                  transition: 1
                  rgb_color:
                    - 255
                    - 64
                    - 19
                  brightness: 125
                action: light.turn_on
    else:
      - type: turn_off
        device_id: !input apollo_device
        entity_id: "{{ light.entity_id }}"
        domain: light

mode: single

Your variable light is a string. A string has no property/item named entity_id. The template should only be "{{ light }}".

If that explanation isn’t immediately obvious to you, you need to learn about the very basics of programming/crawl before you attempt to run.

I tried your suggestion but now getting error:
Message malformed: Unknown entity '{{ light }}'

I also took your other suggestion and tried to google programming/crawl and all I get is what is and how to make web crawlers. Maybe you have a link to somewhere that is more specific on what you meant there.

Guess I didn’t look close enough at your code. You are using proper light.turn_on actions for turning the light on, but those awful device actions for turning the light off. Never use device actions, their code is barely readable and they do not support templates, hence why you are getting the error. Replace the device actions with proper light.turn_off actions and it should work.

Both instances of this:

- type: turn_off
  device_id: !input apollo_device
  entity_id: "{{ light.entity_id }}"
  domain: light

Should be replaced with this:

- action: light.turn_off
  target:
    entity_id: "{{ light }}"
1 Like

In addition to the change Magnus mentioned, there are a couple other changes that you might want to consider.

  1. Get rid of the Device actions you have for turning the light off. Just use a basic entity-based action. They are less complicated to write and easier to read:
- action: light.turn_off
  target:
    entity_id: "{{ light }}"
  1. As it is currently configured, the light will go from off through your color/brightness combinations stopping at red. It will stay red until the sensor’s value drops below 1500. If that’s what you want, great… otherwise you will need to alter your triggers.

@Mayhem_SWE && @Didgeridrew Thank you both!! I finally was able to create a new automation using this blueprint without error. Learned a lot from your advice! I had those Device actions the way they were created from the automation I made using UI and I copy/pasted that to this blueprint as a base. Wasn’t aware that there was a difference, good to know.
@Didgeridrew thanks for pointing out the 2nd point you mentioned, I’ll need to tweak those triggers. My first goal was to just get it working… now I just have to wait till my office gets stuffy to see if the light goes off :sweat_smile: