Problems by setting a value

I have an automation as follows:


          sequence:
            - service: scene.turn_on
              target:
                entity_id: scene.fenster_offen_kalt
            - service: script.notification_fenster_auf_kalt
              data:
                message: >-
                  {% set oid = trigger.to_state.object_id %}
                  {% set ausloeser = 'binary_sensor.{{ oid }}' %}
                  {% set zeit_offen = as_timestamp(now()) - as_timestamp(states.{{ ausloeser }}.last_changed) %}
                  {% set fenster_raum = 'die Balkontür im Wohnzimmer' if 'wohnzimmer' in oid else
                    'das Fenster im Büro' if 'buro' in oid %}
                  {% set temp_outside = states('sensor.temperature_balkon') %}
                  Es ist kalt (auf dem Balkon aktuell {{ temp_outside }}°C) und {{ fenster_raum }} ist seit {{ zeit_offen }} Minuten auf!

Nearly everthing works fine but the one line produces errors:

{% set zeit_offen = as_timestamp(now()) - as_timestamp(states.{{ ausloeser }}.last_changed) %}

When I do it like above I get the error, that it need to be a string. But when I do

{% set zeit_offen = ‘as_timestamp(now()) - as_timestamp(states.{{ ausloeser }}.last_changed)‘ %}

I get not the time but the function as string.

Where is my problem?

try this:

{% set oid = trigger.to_state.object_id %}
{% set ausloeser = 'binary_sensor.' ~ oid %}
{% set zeit_offen = as_timestamp(now()) - as_timestamp(states.('' ~ ausloeser ~'').last_changed) %}
{% set fenster_raum = 'die Balkontür im Wohnzimmer' if 'wohnzimmer' in oid else 'das Fenster im Büro' if 'buro' in oid %}
{% set temp_outside = states('sensor.temperature_balkon') %}
  Es ist kalt (auf dem Balkon aktuell {{ temp_outside }}°C) und {{ fenster_raum }} ist seit {{ zeit_offen }} Minuten auf!
1 Like

Unfortunately the same problem: template value should be a string for dictionary value

post your script (script.notification_fenster_auf_kalt), the issue isn’t in that automation (finity’s template should be used too btw)

You mean after my changes? or are you saying the original is OK?

{% set zeit_offen = as_timestamp(now()) - as_timestamp(states[ausloeser].last_changed) %}

2 Likes

yah, your changes should work, but they do look odd so see my last post. Either way, the phrase

ususually indicates that a string wasn’t returned for message. However scripts don’t care about variable types, so the error is coming from the notification inside that script. But this template could in theory be the cause. I just want to see the script to make sure.

That was the only part that I wasn’t sure how to rewrite correctly.

Here’s the correspondind script:


notification_fenster_auf_kalt:
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id:
          - media_player.2021_22_philips_uhd_android_tv_2
        state: 'on'
      sequence:
        - service: notify.tv_wohnzimmer
          data:
            title: "Fenster ist noch auf"
            message: "{{ message }}"
            data:
              duration: 5
              position: "center"
              fontsize: "large"
              transparency: "25%"
              color: "indigo"
              interrupt: 0
  - service: notify.Office_LaMetric
    data:
      message: "{{ message }}" 
      data:
        priority: "info"
        cycles: 3
        sound: "notification"
        icon: "i141"
  - service: notify.telegram_marco
    data:
      message: "{{ message }}" 

well, that looks fine so it’s the template

What about this:

{% set fenster_raum = 'die Balkontür im Wohnzimmer' if 'wohnzimmer' in oid else 'das Fenster im Büro' if 'buro' in oid %}

if neither is true then there is no else option.

I think that may cause an issue too.

I don’t know how to use an “if/elif/else” in a single line without an else.

yeah it could

I’ve never done it, I would have to test

What is the difference between

{% set zeit_offen = as_timestamp(now()) - as_timestamp(states.(’’ ~ ausloeser ~’’).last_changed) %}
and
{% set zeit_offen = as_timestamp(now()) - as_timestamp(states[ausloeser].last_changed) %}
???

Btw. What is the meaning/usage of ~?

Yah, it is bad but it is not a problem.
The error is caused by that one line:


{% set zeit_offen = as_timestamp(now()) - as_timestamp(states.{{ ausloeser }}.last_changed) %}

When I delete that line, everything works fine.

I already told ya the solution for that line a couple of posts up

I read it and I tried the lines from @finity but it didn’t work :frowning:

I made the post, read my responses…

you can’t put {{…}} inside of {%…%}.

change that one line in the template I gave you to the one petro gave you and it should work.

~ means to concatenate two things together to create 1 string

1 Like

Ah. Now I got you :slight_smile: Thank you @finity and @petro !!!
So, this part works now.

Now, I ran into the next problem when trying to add a custom timestamp. I tried as follows but it obviously is (a bit) wrong.


{% set zeit_offen = (as_timestamp(now()) - as_timestamp(states[ausloeser].last_changed)) | timestamp_custom("%M"), false %}

This is the result:

Es ist kalt (auf dem Balkon aktuell 4.1°C) und die Balkontür im Wohnzimmer ist seit (‘01’, False) Minuten auf!