Using the state of one sensor within another template sensor

(1) What is the difference between
states.sensor.WaterPressureIDTS.state
and
states(‘sensor.WaterPressureIDTS’)

(2) Is there any restrictions on how you can use another sensor state with a template sensor?
(3) I am setting a sensor with now() but when I use that sensor again in another sensor the value is unknown or unavailable. I have cut the code down to this snippet below.
The entity value of Water Pressure TS is 2023-06-07 01:59:07.443554+02:00
but then WaterSTAT is Unavailable and WaterSTAT2 is Unknown
´´´

  - name: Water Pressure TS

    unique_id: WaterPressureIDTS

    state: >

      {{ now() }}

  - name: Water STAT

    unique_id: WaterSTAT

    state: >

      {{ states.sensor.WaterPressureIDTS.state }}

  - name: Water STAT2

    unique_id: WaterSTAT2

    state: >

      {{ states('sensor.WaterPressureIDTS') }}

´´´

If the sensor is valid, both will return the value. But if there’s a problem with the sensor or maybe it loads late, the first one will cause an exception and then the template sensor might not ever load. The second one is the preferred way to get a sensor value, because it will always return something even if the sensor is invalid.

1 Like

Use the states() function whenever possible to avoid errors.

Entity ids are always slugs. They must be all lower case, no spaces, and only _ for punctuation…
{{states('sensor.waterpressureidts') }}

1 Like

Amazing. I simplified it down to this ultra basic code and it works. Thanks. Annoyingly fickle this environment.
´´´
- name: waterpressurets
unique_id: waterpressurets
state: >
{{ now() }}
- name: waterdup
unique_id: waterdup
state: >
{{ states(‘sensor.waterpressurets’) }}
´´´

@Didgeridrew I think I am going crazy. It does not work anymore. I have tried about 50 different ways! waterpressuredup is always unknown. Here is the entire first part of the template include file.
´´´

  • trigger:

    • platform: state

      entity_id: sensor.serial_instruments

    sensor:

    • name: waterpressureidts

      unique_id: waterpressureidts

      state: >

      {{ now() }}

    • name: waterpressuredup

      unique_id: waterpressuredup

      state: >

      {{ states(‘sensor.waterpressureidts’) }}
      ´´´

@pkscout I think I am going crazy. It does not work anymore. I have tried about 50 different ways! waterpressuredup is always unknown. Here is the entire first part of the template include file.
´´´

  • trigger:

    • platform: state

      entity_id: sensor.serial_instruments

    sensor:

    • name: waterpressureidts

      unique_id: waterpressureidts

      state: >

      {{ now() }}

    • name: waterpressuredup

      unique_id: waterpressuredup

      state: >

      {{ states(‘sensor.waterpressureidts’) }}
      ´´´

I don’t see anything glaringly wrong with you configuration, but it’s hard to tell for sure when it isn’t formatted correctly. This is most likely just a quirk of load order… when you chain two sensors like that and have them under the same trigger, you can get strange behavior. If you reload your template entities the second one will likely start working.

  - trigger:
      - platform: state
        entity_id: sensor.serial_instruments
    sensor:
      - name: Water Pressure IDTS
        unique_id: water_pressure_idts
        state: >
          {{ now() }}
      - name: Water Pressured Up
        unique_id: water_pressured_up
        state: >
          {{ states('sensor.water_pressure_idts') }}
1 Like

Thanks. I will try that. What do you mean about reload the template entities? I have stopped and started HA about 50 times!

I just rewrote it and now it works. I would love to know why? There must be something I have wrong

- trigger:
    - platform: state
      entity_id: sensor.serial_instruments
  sensor:
    - name: Water pressure ID TS
      unique_id: water_pressure_id_ts
      state: >
        {{ now().strftime('%s') }}
    - name: Water pressure dup
      unique_id: water_pressure_dup
      state: >
        {{ states('sensor.water_pressure_id_ts') }}

Developer Tools > YAML > Template Entities

1 Like