The following works in Development Tool / Templates but the sensor in configuration.yaml has an error

This gives me exactly what I am looking for but will not translate in configuration.yaml, I am sure it could be more efficient and neater, but I need to understand before making it pretty. I don’t want to use powercalc, I want to understand the syntax.

{% set solinp =  states('sensor.sma_power') | float(0) %}
{% set nitfig =  states('sensor.pconsume') | float(0) %}
{% set suptogrid = states('sensor.sma_energy_meter_psupply') | float(0) %}
{{solinp}}
{{suptogrid}}
{{nitfig}}
{% if solinp < 0 and suptogrid == 0 %}
{{nitfig}}
{% elif solinp > 0 %}
{{solinp + nitfig }}
{% else %}
{{solinp - suptogrid}}
{% endif %}

the following template sensor does not pass syntax and i can find no answer to what I am doing wrong (again) Fails with or without the “-” in the if etc’s.

Error

      - name: current useage
        unique_id: current_useage
        unit_of_measurement: "W"
#        state_class: measurement
        device_class: power
        value_template >
          {% set solinp =  states('sensor.sma_power') | float(0) %}
          {% set nitfig =  states('sensor.pconsume') | float(0) %}
          {% set suptogrid = states('sensor.sma_energy_meter_psupply') | float(0) %}
 
          {% if solinp < 0 and suptogrid == 0 -%}
            {{nitfig}}
          {%- elif solinp > 0 -%}
            {{solinp}}
          {%- else -%}
            {{solinp}}
          {%- endif %}

This results in unknown

      - name: current useage
        unique_id: current_useage
        unit_of_measurement: "W"
#        state_class: measurement
        device_class: power
        state: >
           {% set solinp =  states('sensor.sma_power') | float(0) %}
           {% set nitfig =  states('sensor.pconsume') | float(0) %}
           {% set suptogrid = states('sensor.sma_energy_meter_psupply') | float(0) %}
 
           {% if solinp < 0 and suptogrid == 0 %}
             nitfig
           {% elif solinp > 0 %}
             solinp
           {% else %}
             solinp
           {% endif %}

value_Template missing the :

as i pasted in VS I Got this

then

What file are you putting this in and what integration key is it under?

What you’ve presented is a mash-up of legacy and current Template sensor syntax, so we need to know where you’re putting it to tell you which parts are right and wrong.

In the original post I said configuration.yaml

What integration key is it under?

Template

believe it or not I didnt have a space between : and > in the value_template line.
This stuff drives me totally fricking bananas.

Except now it is complaining it doesn’t have “state” ffs

put state: > and it is back to unknown

template:
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.k3ll3r # change to your weather entity in this line
        response_variable: daily
    sensor:
      - name: Weather Forecast Daily
        unique_id: weather_forecast_daily
        state: "{{ now().isoformat() }}"
        attributes:
          forecast: "{{ daily['weather.k3ll3r'].forecast }}" # change to your weather entity in this line
          
      - name: current useage
        unique_id: current_useage
        unit_of_measurement: "W"
#        state_class: measurement
        device_class: power
        state: >
           {% set solinp =  states('sensor.sma_power') | float(0) %}
           {% set nitfig =  states('sensor.pconsume') | float(0) %}
           {% set suptogrid = states('sensor.sma_energy_meter_psupply') | float(0) %}
 
           {% if solinp < 0 and suptogrid == 0 %}
             {{nitfig}}
           {% elif solinp > 0 %}
             {{solinp}}
           {% else %}
             {{solinp}}
           {% endif %}
#        state: >
#          {% set ssp = states('sensor.sma_power') | float(0) %}
#          {% set sems = states('sensor.sma_energy_meter_psupply') | float(0) %}
#
#          {% set dteu = states('sensor.sma_power') | float(0) - states('sensor.sma_energy_meter_psupply') | float(0) %}
#          {% set nteu = states('sensor.sma_power') | float(0) + states('sensor.sma_energy_meter_pconsume') | float(0) %}
#             {% if (dteu | float) > 0 %}
#                 dteu
#               {% elif nteu > 0 %}
#                  nteu
#               {% endif %}  
#             {% else %}
#                dteu
#             {% endif %}

      
      - name: "Day Power"
        unique_id: power_during_the_day
        device_class: power
        unit_of_measurement: 'W'
        state_class: measurement
        icon: mdi:alpha-a
        state: >
          {% set home_generated = states('sensor.sma_power') |float(0) %}
          {% set total_to_grid = states('sensor.sma_energy_meter_psupply') |float(0) %}
          {{ ((home_generated - total_to_grid) ) |round(1, default=0) }}

Oh boy do I hate File Editor, but VS is so OTT.

The problem … the “trigger” had the “-” before it so all I had was being treated as Trigger. (Hello Dave!)

for those who would like to know the syntax and structure!!


template:
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.k3ll3r # change to your weather entity in this line
        response_variable: daily
  - sensor:
      - name: Weather Forecast Daily
        unique_id: weather_forecast_daily
        state: "{{ now().isoformat() }}"
        attributes:
          forecast: "{{ daily['weather.k3ll3r'].forecast }}" # change to your weather entity in this line
          
      - name: current useage
        unique_id: current_useage
        unit_of_measurement: "W"
#        state_class: measurement
        device_class: power
        state: >
           {% set solinp =  states('sensor.sma_power') | float(0) %}
           {% set nitfig =  states('sensor.pconsume') | float(0) %}
           {% set suptogrid = states('sensor.sma_energy_meter_psupply') | float(0) %}
 
           {% if solinp < 0 and suptogrid == 0 %}
             {{nitfig}}
           {% elif solinp > 0 %}
             {{solinp}}
           {% else %}
             {{solinp}}
           {% endif %}

Thank you Myle, although the problem was actually somewhere else. (See below)

Was it the - for trigger or sensor?

I ask so others can use your resolution in the future. Sensor had no dash in your posted code, but your solution has - sensor:

Trigger has the - in both iterations of posted code.

This will break your weather forecast sensor, as it relies on the data of the trigger. You need to do this:

template:
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.k3ll3r # change to your weather entity in this line
        response_variable: daily
    sensor:
      - name: Weather Forecast Daily
        unique_id: weather_forecast_daily
        state: "{{ now().isoformat() }}"
        attributes:
          forecast: "{{ daily['weather.k3ll3r'].forecast }}" # change to your weather entity in this line
  - sensor:
      - name: current useage
        unique_id: current_useage
        unit_of_measurement: "W"
#        state_class: measurement
        device_class: power
        state: >
           {% set solinp =  states('sensor.sma_power') | float(0) %}
           {% set nitfig =  states('sensor.pconsume') | float(0) %}
           {% set suptogrid = states('sensor.sma_energy_meter_psupply') | float(0) %}
 
           {% if solinp < 0 and suptogrid == 0 %}
             {{nitfig}}
           {% elif solinp > 0 %}
             {{solinp}}
           {% else %}
             {{solinp}}
           {% endif %}

The sensor beloning to the trigger should be part of the trigger list item.
The sensors which are not trigger based, and not depending on that trigger should be under their own sensor list item.

Simplified example using multiple types and multiple triggers:

template:
  - trigger:
      - some trigger
    sensor:
      - name: a sensor based on the trigger
    binary_sensor:
      - name: a binary sensor based on the same trigger
  - trigger:
      - some other trigger used for other sensors
    sensor:
      - name: a sensor based on this other trigger
      - name: another sensor based on this other trigger
  - sensor:
      - name: a sensor which is state based, and not trigger based
      - name: another state based sensor
  - binary_sensor:
      - name: a state based binary sensor
1 Like