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!