Problems with scope

I have the following code in a templates.yaml file, but my variables aren’t going through to the sensors part of the yaml:

- trigger:
    platform: time_pattern
    minutes: "/1"
    variables:
      forecast_entity: >
        {{ 'weather.home' }}
  action:
    - service: weather.get_forecasts
      target:
          entity_id: weather.home
      data:
          type: hourly
      response_variable: weather_home_hourly
    - service: weather.get_forecasts
      target:
          entity_id: weather.home
      data:
          type: daily
      response_variable: weather_home_daily  

# Templates for Actual Powerflow transfer charts (APF - Actual PowerFlow)
#
# For the math to add up a new Real House Load must be calculated and used, witch includes
# the inverter consumption and excludes rounding errors and corrects inaccurate power readings.
#
# It never made sense that inbound power sometimes does not equal outbound power. This fixes it!
#
# Developed by AviadorLP modified for powerwall by purcell-lab
# Correctly sets battery2grid & grid2battery flows
#
        # grid sensor must be negative when importing and positive when exporting
- sensor:
    - name: Weatherman Data Tag
      state: "{{ now().isoformat() }}"
      attributes:
          moon_phase: >
            {% set cond_moon = states('sensor.moon_phase') %}
            {% if cond_moon == 'new_moon' %}
            moon-new
            {% elif cond_moon == 'full_moon' %}
            moon-full
            {% else %}
            {{ "moon-" + cond_moon | replace("_", "-") }}
            {%endif%}
          sun_next_rising: >
            {{ as_timestamp(state_attr("sun.sun", "next_rising")) | timestamp_custom('%-I:%M %p') }}
          sun_next_setting: >
            {{ as_timestamp(state_attr("sun.sun", "next_setting")) | timestamp_custom('%-I:%M %p') }}  
          temperature_unit: "{{ state_attr('weather.home','temperature_unit') }}"
          wind_speed_unit: "{{ state_attr('weather.home','wind_speed_unit') }}"
          precipitation_unit: "{{ state_attr('weather.home','precipitation_unit') }}"  
          pressure_unit: "{{ state_attr('weather.home','pressure_unit') }}"
          wm_cond_now: >
            {% set cond_now = states('weather.home') %}
            {% if cond_now == 'partlycloudy' %}{% set cond_now = 'partly-cloudy' %}{% endif %}
            {% if cond_now == 'clear-night' %}{% set cond_now = 'night' %}{% endif %}
            {% if states('sun.sun') == 'below_horizon' %}
                {% if cond_now == 'sunny' %} night {% elif cond_now == 'partly-cloudy' %} night-partly-cloudy {% else %} {{ cond_now }} {% endif %}
            {% else %}
                {{ cond_now }}
            {% endif %}
          wm_temp_now: >
            {{ state_attr('weather.home','temperature') }}  
          wm_wind_speed_now: >
            {{ state_attr('weather.home','wind_speed') | round }}
          wm_wind_dir_now: >
            {{ state_attr('weather.home','wind_bearing') | round }}    
          wm_dew_point_now: >
            {{ state_attr('weather.home','dew_point') }}
          wm_humidity_now: >
            {{ state_attr('weather.home','humidity') }}
          wm_cloud_coverage_now: >
            {{ state_attr('weather.home','cloud_coverage') }}
          wm_pressure_now: >
            {{ state_attr('weather.home','pressure') }}  
          wm_cond_0: >
            {% set cond0 = weather_home_hourly['weather.home']['forecast'][0].condition %}
            {% if cond0 == 'partlycloudy' %}{% set cond0 = 'partly-cloudy' %}{% endif %}
            {% if cond0 == 'clear-night' %}{% set cond0 = 'night' %}{% endif %}
            {% set next_setting = as_timestamp(state_attr('sun.sun', 'next_setting')) %}
            {% set next_rising = as_timestamp(state_attr('sun.sun', 'next_rising')) %}
            {% set cond0_time = as_timestamp(weather_home_hourly['weather.home']['forecast'][0].datetime) %}
            {% if states('sun.sun') == 'above_horizon' and cond0_time > next_setting %}
                {% if cond0 == 'sunny' %} night {% elif cond0 == 'partly-cloudy' %} night-partly-cloudy {% else %} {{ cond0 }} {% endif %}
            {% elif states('sun.sun') == 'below_horizon' and cond0_time < next_rising %}
                {% if cond0 == 'sunny' %} night {% elif cond0 == 'partly-cloudy' %} night-partly-cloudy {% else %} {{ cond0 }} {% endif %}    
            {% else %}
                {{ cond0 }}
            {% endif %}
          wm_temp_0: >
            {{ weather_home_hourly['weather.home']['forecast'][0].temperature | round }} 


The above is only the first part of the template file -is there any way I can get the scope of the response variables to include the sensor?

Your trigger-based forecast query has no sensor key… which is Required. The best thing to do would be to set up a sensor for each of the values you need from the forecast, then retrieve those states in your Weatherman Data Tag sensor where needed.

Im not sure I understand - shouldn’t the response variable contain the values that the weatherman sensor extracts as in the code above. My problem is the response variables aren’t accessible in the sensor block.

You don’t have a sensor block in the first configured item… each hyphen is a separate item in the list.

Ah, yes. I see. (Thanks for the fast response btw!)

How then can I add other sensors in the templates.yaml file which are unrelated to the trigger-based template?

The same way you have for “Weatherman Data Tag”, by separating them with hyphens.

If that trigger is supposed to be what initiates “Weatherman Data Tag”, then you need to remove the hyphen in front of sensor.

FWIW, I wouldn’t recommend that… there’s no need to query values every minute when they only change once an hour.

Thanks, Yes - did that and it worked! - yes, I’ll change the frequency - I moved it to a minute for a test.
So other sensors in the templates.yaml file should initially start with:

    - name: APF Grid Entity
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: "{{ (0 - states('sensor.powerwall_site_now')|float(0)*1000)|int(0) }}"
    
    # sensor must always be 0 or positive (i think they always are)
    - name: APF House Entity
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: "{{ (states('sensor.powerwall_load_now')|float(0)*1000)|int(0) }}"
    
    # sensor must always be 0 or positive (i think they always are)
    - name: APF Generation Entity
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: "{{ (states('sensor.powerwall_solar_now')|float(0)*1000)|int(0) }}"
    
    # battery sensor must be positive when charging and negative when discharging
    - name: APF Battery Entity
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: "{{ (0 - states('sensor.powerwall_battery_now')|float(0)*1000)|int(0) }}"
    
    
    
    
    # Required to reduce code later on
    - name: APF Grid Import
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: >
        {% if states('sensor.apf_grid_entity')|int(default=0) < 0 %}
          {{ states('sensor.apf_grid_entity')|int(default=0)|abs }}
        {% else %}
          0
        {% endif %}
    
    #   Inverter consumption and power losses due to Inverter transfers and power conversions (AC/DC)
    #   excludes rounding errors made worst by the fact that some inverters round all sensors readings to INT
    #   Occasionally this might be negative probably due to cumulative errors in not so accurate power readings.
    - name: APF Inverter Power Consumption
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: "{{ states('sensor.apf_generation_entity')|int(default=0) - states('sensor.apf_battery_entity')|int(default=0) - states('sensor.apf_house_entity')|int(default=0) - states('sensor.apf_grid_entity')|int(default=0) }}"
    
    # Real House Load Includes Inverter consumption and transfer conversions and losses and rounding errors.
    # It never made sense that inbound power sometimes does not equal outbound power. This fixes it!
    - name: APF Real House Load
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: "{{ states('sensor.apf_house_entity')|int(default=0) + states('sensor.apf_inverter_power_consumption')|int(default=0) }}"
      icon: mdi:home-lightning-bolt
    
    - name: APF Grid2House
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state:  >
        {% if states('sensor.apf_grid_import')|int(default=0) > states('sensor.apf_real_house_load')|int(default=0) %}
          {{ states('sensor.apf_real_house_load')|int(default=0) }}
        {% else %}
          {{ states('sensor.apf_grid_import')|int(default=0) }}
        {% endif %}
    
    - name: APF Grid2Batt
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: >
        {% if states('sensor.apf_grid_import')|int(default=0) > states('sensor.apf_real_house_load')|int(default=0) %}
          {{ states('sensor.apf_grid_import')|int(default=0) - states('sensor.apf_real_house_load')|int(default=0) }}
        {% else %}
          0
        {% endif %}
    
    - name: APF Batt2House
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: >
        {% if states('sensor.apf_battery_entity')|int(default=0) < 0 %}
          {% if states('sensor.apf_battery_entity')|int(default=0)|abs > states('sensor.apf_real_house_load')|int(default=0) %}
            {{ states('sensor.apf_real_house_load')|int(default=0) }}
          {% else %}
            {{ states('sensor.apf_battery_entity')|int(default=0)|abs }}
          {% endif %}
        {% else %}
          0
        {% endif %}
    
    # This might be called house to grid, and can happen in rare circumstances,
    # like when the inverter is not able to do a precise adjustment of power fast enough
    # or when you want to force a discharge of the battery or something...
    # But it only happens with battery or other power generator users.
    - name: APF Batt2Grid
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: >
        {% if states('sensor.apf_battery_entity')|int(default=0) < 0 %}
          {% if states('sensor.apf_battery_entity')|int(default=0)|abs > states('sensor.apf_real_house_load')|int(default=0) %}
            {{ states('sensor.apf_battery_entity')|int(default=0)|abs - states('sensor.apf_real_house_load')|int(default=0) }}
          {% else %}
            0
          {% endif %}
        {% else %}
          0
        {% endif %}
    
    - name: APF Solar2Grid
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: >
        {% if states('sensor.apf_grid_entity')|int(default=0) > states('sensor.apf_batt2grid')|int(default=0) %}
          {{ states('sensor.apf_grid_entity')|int(default=0) - states('sensor.apf_batt2grid')|int(default=0) }}
        {% else %}
          0
        {% endif %}
    
    - name: APF Solar2House
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: >
        {% if states('sensor.apf_generation_entity')|int(default=0) > 0 and states('sensor.apf_real_house_load')|int(default=0) > states('sensor.apf_batt2house')|int(default=0) + states('sensor.apf_grid_import')|int(default=0) %}
          {% if states('sensor.apf_generation_entity')|int(default=0) > states('sensor.apf_real_house_load')|int(default=0) - states('sensor.apf_batt2house')|int(default=0) - states('sensor.apf_grid2house')|int(default=0) %}
            {{ states('sensor.apf_real_house_load')|int(default=0) - states('sensor.apf_batt2house')|int(default=0) - states('sensor.apf_grid2house')|int(default=0) }}
          {% else %}
            {{ states('sensor.apf_generation_entity')|int(default=0) }}
          {% endif %}
        {% else %}
          0
        {% endif %}
    
    - name: APF Solar2Batt
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: >
        {% if states('sensor.apf_generation_entity')|int(default=0) > 0 and states('sensor.apf_battery_entity')|int(default=0) > 0 %}
          {% if states('sensor.apf_battery_entity')|int(default=0) > states('sensor.apf_grid2batt')|int(default=0) %}
            {% if states('sensor.apf_generation_entity')|int(default=0) - states('sensor.apf_solar2house')|int(default=0) > states('sensor.apf_battery_entity')|int(default=0) - states('sensor.apf_grid2batt')|int(default=0) %}
              {{ states('sensor.apf_battery_entity')|int(default=0) - states('sensor.apf_grid2batt')|int(default=0) }}
            {% else %}
              {{ states('sensor.apf_generation_entity')|int(default=0) - states('sensor.apf_solar2house')|int(default=0) - states('sensor.apf_solar2grid')|int(default=0) }}
            {% endif %}
          {% else %}
            0
          {% endif %}
        {% else %}
          0
        {% endif %}
        
    - name: Last Alexa
      state: |-
        {{ expand(integration_entities('alexa_media') | select('search', 'media_player'))
        | selectattr('attributes.last_called', 'eq', True) | map(attribute='entity_id') | first }}
      availability: |-
        {{ expand(integration_entities('alexa_media') | select('search', 'media_player'))
        | selectattr('attributes.last_called','eq',True) | first is defined }}

Got it ! Thanks again