High sensor notification blueprint - help request

Hi,

I’m trying to make an (my first) automation to notify users of high sensor values. I intend to use this for CO2, and later also PM2.5/PM10 etc.

I like the idea of automations (to reduce duplicate code), but I have some difficulty with getting sensor properties. Can somebody point me in the right direction?

I currently have the below start, but I need help on generating the message dynamically, the variables don’t work at the moment. I looked for existing blueprints and searched the homeassistant docs (e.g. on sensors), but I couldn’t find the syntax to query entity properties properly.

blueprint:
  name: High sensor value detection
  description: Notify users of high sensor value detection (e.g. CO2, temp, PM2.5)
  domain: automation
  input:
    trigger_sensor:
      name: Sensor
      description: Sensor to trigger on
      selector:
        entity:
          domain: sensor
    trigger_level:
      name: Trigger level
      description: Sensor value to trigger at
      selector:
        input_number:
          box1:
            name: Trigger value
            initial: 1250
            mode: box
    notify_message:
      name: Notification Message
      description: 'Default: " {{ sensor_quantity }} in {{ sensor_location }} is above {{ trigger_level }} {{ sensor_unit }}"'
      default: " {{ sensor_quantity }} in {{ sensor_location }} is above {{ trigger_level }} {{ sensor_unit }} "

alias: High sensor detection
description: Notify users when high sensor values are detected somewhere
trigger:
  - platform: numeric_state
    entity_id: !input "trigger_sensor"
    above: !input "trigger_level"
variables:
  # these don't work yet:
  sensor_quantity: "{{ states[trigger_sensor.entity_id].device_class }}"
  sensor_location: "{{ states[trigger_sensor.entity_id].location }}"
  sensor_unit: "{{ states[trigger_sensor.entity_id].unit_of_measurement }}"
condition: []
action:
  - service: notify.notify
    data:
      message: !input "notify_message"
mode: single

{{ states[…].attributes.device_class }}

{{ states[…].attributes.unit_of_measurement }}

{{ states[…].attributes.location }}

Thanks @pedolsky for the quick reply! With your input and debugging a bit in Developer Tools->Template, I now have the below code :slight_smile:

I still have a few questions I hope somebody can help with:

  1. How can I get the area a sensor is in and the quantity/device_class? I don’t see this in attributes or state fields, but Homeassistant knows. Update: my ESPHome CO2 sensor does not have a .device_class attribute, but Homeassistant knows it has to use a CO2 icon. Where is this info stored?
  2. Nice to have: I now use trigger_sensor_var as intermediate variable, can I refer/use blueprint inputs directly?
blueprint:
  name: High sensor value detection
  description: Notify users of high sensor value detection (e.g. CO2, temp, PM2.5)
  domain: automation
  input:
    trigger_sensor:
      name: Sensor
      description: Sensor to trigger on
      selector:
        entity:
          domain: sensor
    trigger_level:
      name: Trigger level
      description: Sensor value to trigger at
      selector:
        number:
          mode: box
          min: -50
          max: 1250
    notify_message:
      name: Notification Message
      description: 'Default: " {{ sensor_quantity }} in {{ sensor_location }} is above {{ trigger_level_var }} {{ sensor_unit }}"'
      default: " {{ sensor_quantity }} in {{ sensor_location }} is above {{ trigger_level_var }} {{ sensor_unit }} "

alias: High sensor detection
description: Notify users when high sensor values are detected somewhere
trigger:
  - platform: numeric_state
    entity_id: !input "trigger_sensor"
    above: !input "trigger_level"
variables:
  trigger_sensor_var: !input "trigger_sensor"
  trigger_level_var: !input "trigger_level"
  sensor_quantity: "  "
  # {{ state_attr(trigger_sensor, 'unit_of_measurement') }}"
  sensor_location: "debug_sensor_location"
  #{{ states[trigger_sensor_var.entity_id].attributes.location }}"
  sensor_unit: "debug_sensor_unit {{ state_attr(trigger_sensor_var, 'unit_of_measurement') }}"
condition: []
action:
  - service: notify.notify
    data:
      message: !input "notify_message"
mode: single

Next time put help in #configuration:blueprints