Get from_state in a template sensor

Hi,

is there a way to get the from_state in a template sensor?

- platform: template
  sensors:

In automation this is possible with trigger.from_state. This fails in template sensors.

Any ideas?

Would remove the need for something like that:

alias: 'Steuerung: Temperaturänderung Norden'
description: ''
trigger:
  - platform: state
    entity_id: sensor.aussen_norden_temperatur
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.norden_steigt
            state: 'on'
          - condition: template
            value_template: >-
              {{ trigger.to_state.state | float < trigger.from_state.state |
              float }}
        sequence:
          - service: input_boolean.turn_off
            data: {}
            entity_id: input_boolean.norden_steigt
          - service: notify.telegram
            data:
              message: 'Norden fällt {{ trigger.to_state.state }}'
      - conditions:
          - condition: state
            entity_id: input_boolean.norden_steigt
            state: 'off'
          - condition: template
            value_template: >-
              {{ trigger.to_state.state | float > trigger.from_state.state |
              float }}
        sequence:
          - service: input_boolean.turn_on
            data: {}
            entity_id: input_boolean.norden_steigt
          - service: notify.telegram
            data:
              message: 'Norden steigt {{ trigger.to_state.state }}'
    default: []
mode: single

Could be done in a “one-liner” in a template sensor.

It just detects if a temperature is rising or falling.

You could use a derivative sensor, derivative < 0 -> temperature is falling, derivative > 0 temperature is rising.

1 Like

Cool! That looks great. Lemme thing about that.

Ok!

- platform: derivative
  source: sensor.aussen_norden_temperatur
  name: Außen Norden Temperaturänderung
- platform: derivative
  source: sensor.aussen_sueden_temperatur
  name: Außen Süden Temperaturänderung

But now I got an attribute of an entity I want to derive from.

Do I need to create a template sensor for the attribute and to create a derive sensor from that or can I do it in one step?

And is there a way to set the entity name (different from the autocreated name)?

You need to create a template sensor first that extracts the attribute.

Change the name in the sensor config to the name you would like to have for the entity_id. Then change the friendly name through customization.

1 Like