I have tried creating custum sensors in my config.yaml and am utterly failing.
Currently I have three sensors defined, the first one works but the second and third don’t even show up as an entity. I suspect i’m doing something fundamentally (and probably a bunch of small things too) wrong with the structure of my .yaml but I am at my witts end.
Please help me and tell me what i’m doing wrong, so I can learn. I’m an absolute noob when it comes to coding, so please bear with me.
Thanks in advance!
You need to supply availability criteria and/or defaults for your int and float filter functions.
This is especially true for sensor entities with a unit_of_measurement since they expect a numeric value and will error out if the template does not render one.
# Loads default set of integrations. Do not remove.
default_config:
# Load frontend themes from the themes folder
frontend:
themes: !include_dir_merge_named themes
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
template:
- sensor:
- name: "Technical & Garden | Power"
unit_of_measurement: W
icon: mdi:lightning-bolt
availability: >-
{{ has_value('sensor.shellyproem50_08f9e0e5c190_em0_power') and
has_value('sensor.shellyproem50_08f9e0e5c190_em1_power') }}
state: >-
{{ (states('sensor.shellyproem50_08f9e0e5c190_em0_power') | float(0) -
states('sensor.shellyproem50_08f9e0e5c190_em1_power') | float(0) ) | round(1) }}
- name: "Pellet heating status"
availability: "{{ has_value('sensor.shellypro4pm_08f9e0e9ed64_switch_2_power') }}"
state: >-
{% set power = states('sensor.shellypro4pm_08f9e0e9ed64_switch_2_power') | float(0) %}
{% if power < 3 %}
off
{% elif power < 10 %}
stand by
{% elif power < 65 %}
cooling down
{% elif power < 250 %}
heating
{% elif power > 250 %}
ignition phase
{% endif %}
- name: "Wind Direction TXT"
availability: "{{ has_value('sensor.gw2000a_wind_direction') }}"
state: >-
{% set dir = states('sensor.gw2000a_wind_direction') | int(0) %}
{% set navDir = [ 'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N' ] %}
{{ navDir[ (dir/22.5) | int(0) ] }}
Thank you so much! This looks a lot cleaner already!
So I only have to state “sensor” once and then each “name” creates a new sensor?
I now copy-pasted your yaml and it tells me all is fine, I save and reaload all yaml in dev tools. First sensor still works however the two thereafter still do not show up anywhere. So no improvement unfortunately…
You could clean it up a bit more by removing the defaults on the float and int filters, as you already check on their (numeric) state in the availability template.
So I tried to understand what you did and am now setting up another, similar sensor for an electric Boiler (for when the aforementioned pellet heating is not turned on in summer).
This is what I came up with. The sensor does show up in the UI, however it is “unavailable”. I suppose bringing in another sensor for the “off” state somehow messed things up, but no matter what I try, I’m too coding-retarded to make it work…
- name: "Electric boiler status"
unique_id: electric_boiler_status
availability: >-
{{ has_value('sensor.shellypro4pm_08f9e0e9ed64_switch_3_power') }} and
has_value('switch.shellypro4pm_08f9e0e9ed64_switch_3') }}
state: >-
{% if is_state('switch.shellypro4pm_08f9e0e9ed64_switch_3', 'off') %}
off
{% set power = states('sensor.shellypro4pm_08f9e0e9ed64_switch_3_power') | float(0) %}
{% elif power < 20 %}
standby
{% elif power > 20 %}
heating
{% endif %}