Template help needed. Checking if value is NULL

I have some automation based on Kodi play state. But I only want that to kick in when a movie or TV series plays, not when I watch IPTV. Unfortunately content_type always will return ‘video’.

By some trail an error I found out that state.media_player.kodi.attributes.media_content_id returns a value when a video plays, but seems to be empty if TV is playing.

Now I’m trying to evaluate this state via a sensor. Unfortunately below (check if NULL) does not work. I also tried “” , none, None etc.

Does anyone know how to evaluate if media_player.kodi.attributes.media_content_id is empty?

{% if is_state("media_player.kodi.attributes.media_content_id", NULL) %} TV {% else %} Video {%- endif %}

The Jinja docs say you can use:

{% if variable is defined %}
value of variable: {{ variable }}
{% else %}
variable is not defined
{% endif %}

You can also get the media_content_id of your media player as:

Does that help?

Yes! Thanks. Below works;

{% if states.media_player.kodi.attributes.media_content_id is defined %}
{% else %}
{% endif %}

I was trying to make it work as a template trigger for automation in case Danfoss LC13 current_temperature is null to be replaced with the room temperature sensor but I get errors on configuration validation and automation doesn’t works.

I ended up using this:

{% if states.automation.auto_turn_off_mudroom.attributes.last_triggered == none %}True{% else %}False{% endif %}

Works for me. I was using “if defined” but it wasn’t matching correctly in my use-case. I’m willing to accept it was my own lack of understanding (seems straight-forward through) but this ended up working for me.

Something like that shouldn’t cause a configuration validation error unless you placed it into the wrong spot. Can you post your entire config where you placed that?

That sounds weird but I have replaced with == none, restarted and then replaced with the code I’ve posted earlier and have a successful validation and proper update of the current_temperature attribute. By weird I mean that all edits were made with nano through ssh and only the expressions were edited (I know that YAML is tricky with spaces, that wasn’t a case).
Anyway, thanks for help. I hope that z-wave climate will have the desired function to get rid of the dirty automation rules

Update: check for entity defined is probably the approach for using as a condition since I don’t see a reasonable event to trigger the check. (BTW, it’s not working for me, update on current temperature only happens when temperature entity changes, but if there’s no change in state for some time, the current temperature attribute just blanks/becomes null)

I use mine in a condition:

- alias: Auto Turn Off Mudroom
    - platform: state
      entity_id: light.mudroom
      to: 'on'
        minutes: 1
    condition: or
      - condition: template
        value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.auto_turn_off_mudroom.attributes.last_triggered) | int > 60 }}'
      - condition: template
        value_template: '{% if states.automation.auto_turn_off_mudroom.attributes.last_triggered == none %}True{% else %}False{% endif %}'
      - condition: template
        value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.mudroommotion.attributes.last_triggered) | int > 60 }}'
    - service: homeassistant.turn_off
      entity_id: light.mudroom

I had read somewhere here in the forum that trigger templates may not work as expected due to evaluation processes involved (something like that.) I try to keep my triggers simple (things like time, sun, etc) and use conditions to fine tune.

In the case you have for temp == null and replace with current; I’d use a sensor template for something like that (if I understand), for example (not really a close one but it should get you on the right track):

  - platform: template
        friendly_name: Battery
        unit_of_measurement: '%'
        entity_id: zwave.neo_coolcam_battery_powered_pir_sensor
        value_template: >-
          {% if states.zwave.neo_coolcam_battery_powered_pir_sensor %}
            {{ states.zwave.neo_coolcam_battery_powered_pir_sensor.attributes.battery_level }}
          {% else %}
          {% endif %}

Where you could replace ‘0’ with an evaluation/value of current temperature.

Again, this is based entirely that I understand what you’re trying to do :slight_smile: