Using input_text in trigger state

Hello.

I have some Xiaomi sensors. And I want to make a custom notificartion about its states. So I did it like this:

configiuration file contains

input_text:
  temperature_low_emoji:
    name: T° low emoji
    initial: 🥶
  temperature_normal_emoji:
    name: T° normal emoji
    initial: 😀
  temperature_high_emoji:
    name: T° high emoji
    initial: 🥵
  outdoor_temperature_low:
    name: T° Outdoor low
    icon: mdi:arrow-collapse-down
  outdoor_temperature_high:
    name: T° Outdoor high
    icon: mdi:arrow-collapse-up

sensors:
  outside_temperature_emoji:
    value_template: >
      {%- if states('sensor.outside_temperature') == 'unavailable' -%}
      {{ states('input_text.unknown_emoji') }}
      {%- elif states('sensor.outside_temperature')|float < states('input_text.outdoor_temperature_low')|float -%}
      {{ states('input_text.temperature_low_emoji') }}
      {%- elif states('sensor.outside_temperature')|float > states('input_text.outdoor_temperature_high')|float -%}
      {{ states('input_text.temperature_high_emoji') }}
      {%- elif states('sensor.outside_temperature')|float >=  states('input_text.outdoor_temperature_low')|float and states('sensor.outside_temperature')|float <= states('input_text.outdoor_temperature_high')|float -%}
      {{ states('input_text.temperature_normal_emoji') }}
      {% endif -%}

automation file contains

- alias: "Notify Outdoor Temperature"
  initial_state: on
  trigger:
    - platform: template
      value_template: "{{ states('sensor.outside_temperature')|float < states('input_text.outdoor_temperature_low')|float }}"
    - platform: template
      value_template: "{{ states('sensor.outside_temperature')|float >= states('input_text.outdoor_temperature_low')|float and states('sensor.outside_temperature')|float <= states('input_text.outdoor_temperature_high')|float }}"
    - platform: template
      value_template: "{{ states('sensor.outside_temperature')|float > states('input_text.outdoor_temperature_high')|float }}"
  action:
    - service: notify.me
      data_template:
        message: >-
          {% set emoji = "sensor."+trigger.to_state.entity_id.split('.')[-1]+"_emoji" -%}
          {{ states(emoji) }} {{ trigger.to_state.name }}: {{ trigger.to_state.state }}{{ state_attr(trigger.to_state.entity_id, 'unit_of_measurement') }}

As You can see I have 2 input_text with some digits. If the temperature go in one of 3 intervals I receive 3 different messages.

That’s works like it shoud. But is I want more than one temperature sensors I need to repeate this code multiple times. this is because You cannot add “entity_id” option in platform: template. It’s not perfect from my point of view.

This looks better but doesn’t work:

- alias: "Notify Indoor Temperature"
  initial_state: on
  trigger:
    - platform: numeric_state
      entity_id:
        - sensor.livingroom_temperature
        - sensor.childroom_temperature
        - sensor.bedroom_temperature
        - sensor.bathroom_temperature
        - sensor.toilet_temperature
      below: "{{ states('input_text.outdoor_temperature_low')|float }}"
  action:
    - service: notify.me
      data_template:
        message: >-
          {% set emoji = "sensor."+trigger.to_state.entity_id.split('.')[-1]+"_emoji" -%}
          {{ states(emoji) }} {{ trigger.to_state.name }}: {{ trigger.to_state.state }}{{ state_attr(trigger.to_state.entity_id, 'unit_of_measurement') }}

You cannot use template in “below/above” option. It’s looks strange because You can use templates in “from” option.

Maybe I don’t see i right way to do it? Please help.

1 Like

You’d have to use a template trigger, for example:

- alias: "Notify Indoor Temperature"
  initial_state: on
  trigger:
    - platform: template
      value_template: "{{ states('sensor.livingroom_temperature')|float < states('input_text.outdoor_temperature_low')|float }}"
    - platform: template
      value_template: "{{ states('sensor.childroom_temperature')|float < states('input_text.outdoor_temperature_low')|float }}"
...

(not sure why you’re using an input_text when input_number would be more logical)

@Tinkerer, good point! Just because didn’t knew about. :wink: Will modify sensor and automation definition. Will it help in my problem?

UPD: Oops. I didn’t read all the post. Thanks, I,ll try trigger template and post results here.

Instead of using a Numeric State Trigger, use a State Trigger with a condition.

- alias: "Notify Indoor Temperature"
  initial_state: on
  trigger:
    - platform: state
      entity_id:
        - sensor.livingroom_temperature
        - sensor.childroom_temperature
        - sensor.bedroom_temperature
        - sensor.bathroom_temperature
        - sensor.toilet_temperature
  condition:
    - condition: template
      value_template: "{{ trigger.to_state.state|float < states('input_text.outdoor_temperature_low')|float }}"
  action:
    - service: notify.me
      data_template:
        message: >-
          {% set emoji = "sensor."+trigger.to_state.entity_id.split('.')[-1]+"_emoji" -%}
          {{ states(emoji) }} {{ trigger.to_state.name }}: {{ trigger.to_state.state }}{{ state_attr(trigger.to_state.entity_id, 'unit_of_measurement') }}
1 Like

Thanks @123.

In Your example, as I see, there is only one condition. But I think i know what to do…

Thanks all!

Need a little time to test it.

It’s based on the example you posted (I converted your below option into a condition). Modify the condition per your requirements.

wont this trigger an awful lot of messages…?

on each state change below the set threshold the automation will fire. You’d have to at least set some extra conditions to not trigger per 0.1 degree, imagine 5 sensors triggering this :wink:

The condition check handles that :wink:

{{ trigger.to_state.state|float < states('input_text.outdoor_temperature_low')|float }}"

would only make sure the trigger is below the input. But if it changes and stays below that threshold, it would constantly trigger on each state (and attribute )change.? Or why am I missing

@Mariusthvdb
I thinked about. But there is no problem with it. I receive a message only when temperature pass from one interval to another.

Ok, you see what’s happening of course. I would at least add something like:

      - condition: template
        value_template: >
          {{ trigger.to_state.state is not none and
             trigger.from_state.state is not none and
             trigger.to_state.state != trigger.from_state.state }}

to prevent it from triggering on all attributes changes.

Just note it, so you can always throw it in :wink:

OK, the results.

  1. Thanks all who shared ideas - You’re awesome!

  2. input_number is perfect. I like how it’s now looks in lovelace.

  3. configuration.yaml

# emojis
input_text:
  unknown_emoji:
    name: Unknown status emoji
    initial: ❔
  temperature_low_emoji:
    name: T° low emoji
    initial: 🥶
  temperature_normal_emoji:
    name: T° normal emoji
    initial: 😀
  temperature_high_emoji:
    name: T° high emoji
    initial: 🥵

# temperature margins
input_number:
  indoor_temperature_low:
    name: T° Indoor low
    min: 7
    max: 20
    icon: mdi:arrow-collapse-down
    unit_of_measurement: °C
  indoor_temperature_high:
    name: T° Indoor high
    min: 20
    max: 40
    icon: mdi:arrow-collapse-up
    unit_of_measurement: °C
  outdoor_temperature_low:
    name: T° Outdoor low
    min: -40
    max: 15
    icon: mdi:arrow-collapse-down
    unit_of_measurement: °C
  outdoor_temperature_high:
    name: T° Outdoor high
    min: 15
    max: 40
    icon: mdi:arrow-collapse-up
    unit_of_measurement: °C


sensor:
  - platform: template
    sensors:
      toilet_temperature_emoji:
        value_template: >
          {%- if states('sensor.toilet_temperature') == 'unavailable' -%}
          {{ states('input_text.unknown_emoji') }}
          {%- elif states('sensor.toilet_temperature')|float < states('input_number.indoor_temperature_low')|float -%}
          {{ states('input_text.temperature_low_emoji') }}
          {%- elif states('sensor.toilet_temperature')|float > states('input_number.indoor_temperature_high')|float -%}
          {{ states('input_text.temperature_high_emoji') }}
          {%- elif states('sensor.toilet_temperature')|float >=  states('input_number.indoor_temperature_low')|float and states('sensor.toilet_temperature')|float <= states('input_number.indoor_temperature_high')|float -%}
          {{ states('input_text.temperature_normal_emoji') }}
          {% endif -%}
      outside_temperature_emoji:
        value_template: >
          {%- if states('sensor.outside_temperature') == 'unavailable' -%}
          {{ states('input_text.unknown_emoji') }}
          {%- elif states('sensor.outside_temperature')|float < states('input_number.outdoor_temperature_low')|float -%}
          {{ states('input_text.temperature_low_emoji') }}
          {%- elif states('sensor.outside_temperature')|float > states('input_number.outdoor_temperature_high')|float -%}
          {{ states('input_text.temperature_high_emoji') }}
          {%- elif states('sensor.outside_temperature')|float >=  states('input_number.outdoor_temperature_low')|float and states('sensor.outside_temperature')|float <= states('input_number.outdoor_temperature_high')|float -%}
          {{ states('input_text.temperature_normal_emoji') }}
          {% endif -%}

automation.yaml

# Notify outdoor temperature sensors changes
- alias: "Notify Outdoor Temperature"
  initial_state: on
  trigger:
    - platform: state
      entity_id:
        - sensor.outside_temperature
  condition:
    condition: or
    conditions:  
      - condition: template
        value_template: "{{ trigger.to_state.state|float < states('input_number.outdoor_temperature_low')|float }}"
      - condition: template
        value_template: "{{ trigger.to_state.state|float >= states('input_number.outdoor_temperature_low')|float and trigger.to_state.state|float <= states('input_number.outdoor_temperature_high')|float }}"
      - condition: template
        value_template: "{{ trigger.to_state.state|float > states('input_number.outdoor_temperature_high')|float }}"
  action:
    - service: notify.me
      data_template:
        message: >-
          {% set emoji = "sensor."+trigger.to_state.entity_id.split('.')[-1]+"_emoji" -%}
          {{ states(emoji) }} {{ trigger.to_state.name }}: {{ trigger.to_state.state }}{{ state_attr(trigger.to_state.entity_id, 'unit_of_measurement') }}

# Notify indoor temperature sensors changes
- alias: "Notify Indoor Temperature"
  initial_state: on
  trigger:
    - platform: state
      entity_id:
        - sensor.livingroom_temperature
        - sensor.childroom_temperature
        - sensor.bedroom_temperature
        - sensor.bathroom_temperature
        - sensor.toilet_temperature
  condition:
    condition: or
    conditions:  
      - condition: template
        value_template: "{{ trigger.to_state.state|float < states('input_number.indoor_temperature_low')|float }}"
      - condition: template
        value_template: "{{ trigger.to_state.state|float >= states('input_number.indoor_temperature_low')|float and trigger.to_state.state|float <= states('input_number.indoor_temperature_high')|float }}"
      - condition: template
        value_template: "{{ trigger.to_state.state|float > states('input_number.indoor_temperature_high')|float }}"
  action:
    - service: notify.me
      data_template:
        message: >-
          {% set emoji = "sensor."+trigger.to_state.entity_id.split('.')[-1]+"_emoji" -%}
          {{ states(emoji) }} {{ trigger.to_state.name }}: {{ trigger.to_state.state }}{{ state_attr(trigger.to_state.entity_id, 'unit_of_measurement') }}
  1. I receive messages only when:
    a. system started and receive sensor information (once per sensor)
    b. sensor state switched from one interval to another
    c. on HA restart I receive unavailable status for all sensors. that’s because deconz is no more connected at this time.
    d. looks like after I’ve asked about sensor states over telegram (i have an automation for that)

There was a strange behavior if I change temperature limit over lovelace. but
a. it ok. just once
b. I’m not sure it fires now. need to test more.

ATM it’s looks perfect for me. Someone know how to disable an automation on shutdown? It’s doesn’t work like this

- alias: 'HA stop'
  initial_state: on
  trigger:
    platform: event
    event_type: homeassistant_stop
  action:
    service: automation.turn_off
    data:
      entity_id:
        - automation.notify_indoor_temperature_alarm
        - automation.notify_outdoor_temperature_alarm
        - automation.smoke_fire_water_alarm
    service: notify.me
    data:
      message: "HA stop at {{ states('sensor.date_time') }}"

I this deconz is already disconnected when this automation starts.

@Mariusthvdb Thanks. Never saw status “none” for those sensors. Just “unavailable” but I use this status.

@Mariusthvdb Too bad, You was right. On previous configuration (W/O conditions) i received messages only when states moved from one intervat to another.

With conditions it fires each time temperature changes. At least for outside. There is no so much changes inside a house…

On first though I thinked to check if Emoji has changed. this is a good point to catch interval changes. But as we automate sensor.outside_temperature not sensor.outside_temperature_emoji I cannot check
{{ triggger.to_state.state != trigger.from_state.state }} for emoji sensor…

yep, as was expected I am afraid.
you could maybe add a range for the change, say 1 degree? That way it wouldn’t trigger on each tenth of degree the sensor measures.
Also add a condition since last_changed > 300 (seconds) ?

tools enough to make it respond less often. Or have a look here https://www.home-assistant.io/components/threshold/

@Mariusthvdb
Well, this is not interesting. Instead of problem with below/above this create a problem with multiple notification. Need other solution.

I have tested this

- condition: template
  value_template: >-
    {% set emoji = "sensor."+trigger.to_state.entity_id.split('.')[-1]+"_emoji" -%}
    {{ trigger.to_state.state|float < states('input_number.indoor_temperature_low')|float and states(emoji) 
 != states('input_text.temperature_low_emoji')}}

Unfortunatelly when trigger runs emoji is already changed…
Maybe I can check if states(emoji) is changed not more than a second before?

Can you explain what you are attempting to achieve with these three conditions? Because as I understand them, they accept any numeric value (and you don’t need three Template Conditions to achieve that).

  condition:
    condition: or
    conditions:  
      - condition: template
        value_template: "{{ trigger.to_state.state|float < states('input_number.outdoor_temperature_low')|float }}"
      - condition: template
        value_template: "{{ trigger.to_state.state|float >= states('input_number.outdoor_temperature_low')|float and trigger.to_state.state|float <= states('input_number.outdoor_temperature_high')|float }}"
      - condition: template
        value_template: "{{ trigger.to_state.state|float > states('input_number.outdoor_temperature_high')|float }}"

There are three Template Conditions where each one checks if the value is within a certain range.

Range 1: value < low
Range 2: value >= low and value <= high
Range 3: value > high

    1    |     2    |    3
---------|----------|---------
         |          |
        low        high

If the value is within any one of the three ranges, then the condition is true. In other words, any numeric value is valid. Removing the entire Condition section will achieve the same result.

@123 Will try. :wink: You’re right, with those 3 conditions I don’t get what I need.

What I need is how it worked w/o conditions and with platform: template.

sensor.outside_temperature = 2
input_number.outdoor_temperature_low = 5

First trigger interval is true and action fired.

sensor.outside_temperature = 3
input_number.outdoor_temperature_low = 5

first trigger is still true and nothing happend.

sensor.outside_temperature = 6
input_number.outdoor_temperature_low = 5

first trigger is false, second trigger is true. action fired.

At leat it work like this (my first post without conditions and triggers platform: template). Resulting message is different because emoji sensor changes (not in this automation) .

When I use conditions I get a message on each state change of the sensor.outside_temperature.

It’s OK to use 3 triggers w/o conditions. But I cannot use that way when I want this automation for many sensors.

Outside sensor is a single entity. Indoor sensors are many. And I don’t want to write 5-10 identical automations with only one difference - entity_id. That’s why I try to find a way for templating this.

@123 's condition should be your main condition. Add 2 extra conditions, ( last_triggered >120, and difference > 1 degree) and my set for not triggering on attributes (the none set)

Also, if you want the automation to be able to use its last_triggered in a condition, you best put the conditions in the action part, and not the condition part.

the last_triggered isn’t set until the automation reaches its action part, and conditions preventing that, will also prevent the use of the condition last_triggered > 120… Sounds a bit cryptic, but thats just the way automations work.

This would get it done probably

@Mariusthvdb, last_triggered >120 will set minimum interval between messages for 120 seconds and difference >1 minimum value changes for 1 degree, right? I can’t see how it helps sending messages only when temperature interval changes. Did I miss it?