Calling values with state_attr('xxx') from customize.yaml to sensors.yaml does not work?

Hi all,

so i defined a test.sun entity with azimuth and elevation (spezifically to test my shading code), and call them with state(‘xxx’) and it works
i also defined fixed value entity in cutomize.yamle, pretty much the same way, but it does not work.

and i cant figure out what i did wrong…

here is the sensor that works (out of my templates.yaml)

# Sun simulation
  - trigger:
      - platform: time_pattern
        # This /1 will update every second
        seconds: /4
    sensor:
      # cycle through 1-360 rinse and repeat
      - name: "test.sun_azimuth"
        unit_of_measurement: "°"
        state_class: measurement
        state: >
          {% set azimuth = states('sensor.test_sun_azimuth') | float(0) %}
          {% if azimuth < 355 -%}
            {{ (azimuth + 6) }}
          {%- else -%}
            {{ "0" }}
          {%- endif %}
  - trigger:
      - platform: time_pattern
        # This /1 will update every second
        seconds: /2
    sensor:
      # cycle through 1-360 rinse and repeat
      - name: "test.sun_elevation"
        unit_of_measurement: "°"
        state_class: measurement
        state: >
          {% set e = 6 %} {#  enter azimuth increments #} 
          {% set b = 65  %} {# enter max elevation #} 
          {% set c = b - 80 %} {# no input here, calculates the lower elevetion#}
          {% set d = 80 / 180 * e %} {# no input here, calculates the elevation per aimuth increment#}
          {% set x = states('sensor.test_sun_azimuth') | float(0) %}
          {% if x < 180 -%}
            {{ ((x*d/e)+c) | round(2) }}
          {%- else -%}
            {{ (((x-(2*(x-180)))*d/e)+c) | round(2) }}
          {%- endif %}

this is the value block that does not work: (out of my customize.yaml)

# values for roller shutter automation

cover.eg_kueche:      # individual window shading settings
  window_direction: 180             # direction the window is facing: default 180 = south
  azimuth_limit_low: 45             # lover deviation from window direction from which shading is required: default 45 (positive value, also its negative of the window!)
  azimuth_limit_high: 45            # upper deviation from window direction up to wich shading is required: default 45
  window_tilt: 0                    # deviation from vertical if e.g. roof window (a wall window is 0° deviation)
  window_height: 1000               # height of window in mm
  window_wall_thickness: 400        # width of wall including inside window sill in mm
  window_sun_strake:  100           # acceptable width of sun streaks (AWS) in mm
  window_min_elevation_shading_active: "15" # from which elevation onward shading is required
  roller_shutter_depth: 50          # depth of the shutter from outside wall
  roller_shutter_start_shading: 98  # start shading in % of roller shutter
  roller_shutter_full_shading: 5    # fully shaded in % of roller shutter
  roller_shutter_catdoor: 5         # shutter max close setting for cat door or to prevent lock out from terrace door
  roller_shutter_catdoor_time:      # fixed offset time to close the shutter (catch up with the others), if "" then it will never fully close, except by manual operation.
  roller_shutter_catdoor_active: "false" # if true, catdoor function is active, if not, normal operation will be executed.
  roller_shutter_max: 68            # formula needs to be build. from which elevation shading is no longer required because the wall itself provides shading
  window_awning: "false"                   # awing available for this window (prefer awning is a global setting), if true, roller shutter will not do any shading work.
  shading_paused: "false"           # can be true, false and time, time reactivates after the time of automation_pause_time
  open_on_window_open: "false"               # false will prevent automatic window opening during closed roller shutter, true will rise the roller shutter to slotted if window is opened (false will not on manual windows).

and this is out of my sensors.yaml and should call the values.

azimuth and elevation work, all others are just 0.0, i tried every combination i could think off with _ and . for the names.

# roller-shutter EG-Küche 
- platform: template
  sensors:
    cover_eg_kueche_shading_pos_1:
      value_template: >-
        {% set azimuth = states('sensor.test_sun_azimuth') | float(0) %}
        {% set elevation = states('sensor.test_sun_elevation') | float(0) %}
        {% set window_direction = states('sensor.cover_eg_kueche.window_direction') | float(0) %}
        {% set azimuth_limit_low = states('sensor.cover_eg_kueche.azimuth_limit_low') | float(0) %}
        {% set azimuth_limit_high = states('sensor.cover_eg_kueche.azimuth_limit_high') | float(0) %}
        {% set window_tilt = states('sensor.cover_eg_kueche.window_tilt') | float(0) %}
        {% set window_height = states('sensor.cover_eg_kueche.window_height') | float(0) %}
        {% set window_wall_thickness = states('sensor.cover_eg_kueche.window_wall_thickness') | float(0) %}
        {% set window_sun_strake = states('sensor.cover_eg_kueche.window_sun_strake') | float(0) %}
        {% set window_depth_roller_shutter = states('sensor.cover_eg_kueche.window_depth_roller_shutter') | float(0) %}
        {% set roller_shutter_start_shading = states('sensor.cover_eg_kueche.roller_shutter_start_shading') | float(0) %}
        {% set roller_shutter_full_shading = states('sensor.cover_eg_kueche.roller_shutter_full_shading') | float(0) %}
        {% set roller_shutter_catdoor = states('sensor.cover_eg_kueche.roller_shutter_catdoor') | float(0) %}
        {% set roller_shutter_max = states('sensor.cover_eg_kueche.roller_shutter_max') | float(0) %}
        {% set window_min_elevaiton_shading_active = states('sensor.cover_eg_kueche_window_min_elevaiton_shading_active') | float(0) %}

        {% if (window_direction - azimuth_limit_low) < azimuth < (window_direction + azimuth_limit_high) and elevation < roller_shutter_max %}
          {% set shading = "true" %}
        {% else %}
          {% set shading = "false" %}
        {% endif %}
        {% if shading == "true" and elevation > window_min_elevaiton_shading_active %}
          {{ ((window_height + window_wall_thickness + window_sun_strake)*(tan((elevation - window_tilt) * pi / 180) / window_height * 10) | float(0)) | round(0) }}
        {% elif shading != "true" and elevation > window_min_elevaiton_shading_active %}
          {{ window_min_elevaiton_shading_active | float(0)}}
        {% elif elevation < -6 %}
          {{ '0' | float(0) }} 
        {% elif -6 < elevation < 0 %}
          {{ '10' | float(0) }} 
        {% else %}
          {{ '100' | float(0) }} 
        {% endif %}

this is the result, it basically jumps to 0.0 when ever i print any of the variables i want to pull from customize.yaml

In this code it should not be 0.0 but 15

image

there are two ways you can do it.

the first is the recommended way but the second will also work:

state_attr('sensor.cover_eg_kueche', 'window_direction')

or

states.sensor.cover_eg_kueche.attributes.window_direction
1 Like

unfortunately that did not solve my problem. I did try that too.

if i eliminate all | float from code, i get status unknown

I still think something with my customize.yaml is not working. the sensor cover_eg_kueche with its attributes is not created, for what ever reasons.

I tried shortening it into just sensor.cover_eg and direction, no luck either.

my customize.yaml looks like petros example and i copied the code to include it from the docs.

this is my configuration.yaml


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
#tts:
#  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# https://opensource.com/article/21/2/home-assistant-custom-sensors
sensor: !include sensors.yaml

# https://www.home-assistant.io/integrations/generic/
camera: !include cameras.yaml

# Sensor for stats roller_shutters
template: !include templates.yaml

homeassistant:
  customize: !include customize.yaml

# https://github.com/mampfes/hacs_waste_collection_schedule
waste_collection_schedule:
  sources:
    - name: ics
      args:
        file: "2022.ics"

OK strike that.

I see you are customizing the cover not the sensor

state_attr('cover.eg_kueche', 'window_direction')

no dice

this code gives the azimuth as expected.

# roller-shutter EG-Küche 
- platform: template
  sensors:
    cover_eg_kueche_shading_pos_1:
      value_template: >-
        {% set azimuth = states('sensor.test_sun_azimuth') | float(0) %}
        {% set elevation = states('sensor.test_sun_elevation') | float(0) %}
        {% set direction = state_attr('cover.eg', 'direction') %}
        {{ azimuth }}

this code gives unknown (not expected)

# roller-shutter EG-Küche 
- platform: template
  sensors:
    cover_eg_kueche_shading_pos_1:
      value_template: >-
        {% set azimuth = states('sensor.test_sun_azimuth') | float(0) %}
        {% set elevation = states('sensor.test_sun_elevation') | float(0) %}
        {% set direction = state_attr('cover.eg', 'direction') %}
        {{ direction }}

this is the customize.yaml (I did deleted everything (in sensors.yaml and customize.yaml), just to make sure i have no interference from any stupid thing down the file)

# values for roller shutter script

cover.eg:      # individual window shading settings
  direction: 180             # direction the window is facing: default 180 = south

what I actually want to do is set attribute values. in this case of cover.eg and its attribute direction

I think something is wrong with the customize.yaml code, the sensor does not show up in the UI like other sensors i have created in the past (but other places in the config)

How ever for all my searching, I cant finde an official documentation for customize.yaml that creates sensors the way I do. So I guess I’m wrong. I copied the example of @petro.

I got it from here: click

but maybe I did misinterpreted something?

if you look at the dev tools → states for the cover.eg does it show the “direction” as an attribute there with a good value?

And you are either reloading “core” or, better, restarting HA after making changes to customize.yaml?

Take a screenshot of your cover in the area that @finity mentioned. Developer Tools → States. Filter down to just the entity in question and make sure to include the attributes in the screenshot.

since i hade the problems, i was always restarting the whole HA, not just reloading locations and templates, just to be sure

it looks like your cover doesn’t exist.

how did you create the cover?

i placed it into the customize.yaml

do you mean i would have to create it in an other place too?

like automation,where the UI gets all mixed up if you just place yaml code into the file?

on an afterhtought:

well that would explain my problems :slight_smile: petro did mention that he had the impression i was a special case for community assistance, nailing the hard stuff and failing the easy stuff. LOL

I kind of already thought you were going to say that.

You can’t just create any entity in customize.yaml.

the entity has to already exist and then you can customize it afterward.

I think your best bet would be to create a template sensor with a fixed state value and add the attributes to that sensor.

I’m not sure you would be able to update it on the fly tho so if that’s your goal then I’m not sure what you can do.

most of the stuff is one time set up. the window size and wall thickness does not change, in my house, since 40, and in my other since 124 years

its mainly, you set up one window after each other, and the automation code just exists once and cycles through each window and sets the position, and then the automation move all shutters to new pos every say 4 mins or what ever is convenient for the user and WAF

dont want to multiply my calculations for each window shutter and with all those values inside the calculations, its so easy to get mixed up when setting up and nothing works like expected.

getting mixed up inside the formulas, without speaking name variables even happens to me an the formula is way away from being complete. the complex stuff if then still has to be integrated, this is just the basic pos calculation.

this whole setup is, so folks who dont know how to do the calculations could just copy past, and use default values where they dont have em. etc.pp.

You’re going to need to set up a template sensor with all of the attributes for each window that has a unique set of values.

if you have 10 windows then you will need 10 sensors to carry all of the values for the attributes you need for each one.

it won’t be any different than what you were trying to do in customize but instead you’ll be creating a template sensor for each one

in the sensors.yaml then?

with code like this:

- platform: template
  sensors:
    cover.eg_kueche:
      friendly_name: "Werte Fenster EG Küche"
# no unit
# no value

and then in customize.yaml i give them the attributes like “window_direction: value” like this

# values for roller shutter script

cover.eg_kueche:      # individual window shading settings
  direction: 180             # direction the window is facing: default 180 = south

right?

No.

the sensor has to have a value.

And “cover.eg_kueche” isn’t a valid sensor name.

So you will need to do it something like this with no customization involved:

template:
  - sensor:
      - name: "Werte Fenster EG Küche"
        state: "{{ some value you want for the state }}"
        attributes:
          window_direction: "{{180}}"
          azimuth_limit_low: "{{45}}"
          azimuth_limit_high: "{{45}}"
          window_tilt: "{{0}}"
          etc...

the above is using the new template syntax and not the legacy syntax. You can use the old style if you want but you’ll need to modify the syntax per the docs for that style.

I haven’t tested it but I think it should work.

ah yes, i figured that value thing out, but still tried to set the attributes by customize.yaml

so thanks for pulling me out of that rabbit hole.

I should have taken petros first recommendation including sample, stupid me.

This just suc*s, what ever I try, I end up with config error or a all green config and then log entries that look like this:

Source: config.py:454
First occurred: 23:17:34 (4 occurrences)
Last logged: 23:18:47

Invalid config for [template]: invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'attributes') for dictionary value @ data['sensor'][0]['state']. Got '{{ see attributes }}'. (See /config/templates.yaml, line 69).

My HA setup is now 15 days old, on day 5 i joind here, because I could not solve a issue i had by reading the documentation, according to data missing in my history for a entity I set up right at the start I have 827 system restarts to load a new config… Im so sick of copying a code from the docs or from very experienced folks here and then getting log entries like above right from that 1:1 code.

Im really tempted to dump HA and go back to my working FHEM server. The 15 days Im down the HA route with almost full time days, I could have done a nice FHEM front end in about 10% of the time.

But I just cant believe this is how HA should work or is intended to work, and I have seen much more complicated stuff in here, than what I want to do.

So WTF am I doing wrong? Sorry Im mainly frustrated with my inability to warp my head around the funny stuff of HA. Yes indeed, all systems have their funny stuff, either because someone just changed a major thing or because of old stuff and legacy support or what ever.

What i am trying to accomplish is a easy thing:

I have a set of variables (about 35) that should control one single shutter position during the full 24h of a day. every day of the year, as long as a input switch for “automation” is active.

This I got 27 times over, its 27 windows with shutters that need controlling of their shutter position.

I dont give a f* if they move all at once or one after the other or mixed up, really, i just want them to move the right way at the right time to the right position. (And Im not done yet, it will get way more complicated, as the windows do have motors to open and close, and sensors to detect if “fully opened” or just “tilted” and motors to rotate the handel from locked to full open to tilt etc.)

Then: I have a set of conditions and calculations that should take each set of “how ever many” variables, process them and punch out the position for that related single shutter as a result, probably just give the move order to the shutter and forget about that window and move on to the next. repeat this for all 27 windows with their 35 variables and then start over again.

I have the code to sort out the result from the variables more or less finished, but I cant get this code into HA, for various reasons.

I would need a database table, where I have a column for each of the 35 variables and a row for each of my 27 windows (or vice versa).

then i want a place to put some code that pulls the table contend row after row and just process it in combination with input from sun.sun azimuth and elevation and cloud_level and forecast_temp etc. pp. and the result would be a service_call to move a specified shutter.

How the heck do i do this in HA? Is it really so complicated as I have to become to belive?

Thanks
Manne

post what you have now for your template sensor. It’s probably just an indentation error.

probably.

What you are attempting to do isn’t trivial.

And I’ll say that yaml likely makes the process trickier. But I’m sure it can be done. You already seem to have the hard part worked out.

if you know python then maybe you could write a python script to do what you want and controlled by HA?